Does char a new char a abc leak memory?

Updated on technology 2024-04-12
14 answers
  1. Anonymous users2024-02-07

    char *a=new char;a="abc";Will memory be leaked?

    No, because you're defining a one-dimensional array, but you're wrong about it.

    char *a="new",char;a="abc";That way, you won't make a mistake!

    You use char* a=new, you use char and then a pointer, obviously, you are defining a pointer to an array of strings, and the correct expression is: char *a="new",char;a="abc";

    If I misunderstand the second expression, what you mean is to say: the string"abc"There's nothing wrong with giving the string variable a, it's the correct expression, and it won't leak memory!

    The subscripts of one-dimensional arrays can be omitted, but the column subscripts of two-dimensional arrays cannot be omitted, e.g. char a[10] is correct, char a[ is wrong, only two-dimensional arrays will leak memory, because it cannot omit column marks.

    char *a=new char;a="abc";You don't have a fixed buffer above, and the one-dimensional array line marker can be saved, so no matter how many strings you define, as long as the line mark is omitted, there will be no memory leakage!

  2. Anonymous users2024-02-06

    Yes, after you originally allocated a space, modify its value again, so that the original allocated memory is out of control and cannot be allocated.

    After modifying the pointer that dynamically allocates memory, remember to do so first if you need to modify it.

    char* a=new char;delete a;a="abc";

  3. Anonymous users2024-02-05

    Please write your application at the time of new char.

    new char[size], the compiler does not infer how much memory is needed from later or at runtime.

    I didn't find what happens to the compiler when the memory size is not written, it looks like it should report an error, if it can be compiled, I guess it may be 0 or 1 or some default value, in short, the requested memory may not be able to store the string received from cin.

    Even if you don't fill in the amount of memory you want to apply, it will work normally, but the piece of memory pointed to by char *a just happens to be unused, and if the string input from cin is long enough, it will most likely report an error.

  4. Anonymous users2024-02-04

    char a= This is an array of characters, taking up 3 bytes. char a="abc"It's different, it's a string, and there's another one at the end'\0'Terminator, which occupies 4 bytes.

  5. Anonymous users2024-02-03

    Strings have an ending (0), so strlen should be 4 (space + a + b + c), but in memory, with an ending, it should be 5

  6. Anonymous users2024-02-02

    string char a="abc"; The number of bytes occupied in memory is (b 4).

  7. Anonymous users2024-02-01

    In other words, what is stored in the variable s is an address, which is the first address of the string "abc", that is, the character'a'address.

    abc"It is a string constant, which is stored continuously, and the compilation system manages the storage space it stores, in fact, there is a 0 (string terminator) after it.

    s+1) is b because s points to a and s+1 points to b (i.e., s+1 is'b'the address of this character). The value of *s+1 is also b because *s+1='a'+1, so yes'b'Character.

  8. Anonymous users2024-01-31

    First there is a static string "abc". And then you point this pointer s to that string.

    S+1 is indeed B. But you didn't use this example very well. If the string is "CBA" in your example, *s+1 is d, and *(s+1) is b. So that you can understand better.

  9. Anonymous users2024-01-30

    There should be one at the end of the string'\0'As a string ending flag, so you should request one more :

    char*temp_name=new char[y+1];

    After the for(){ loop, add temp name[i]='\0';That's it.

  10. Anonymous users2024-01-29

    Only memory addresses are compared.

    The correct way to compare strings is to use the strcmp(char *s1,char *s2) function to compare, and you need to include

    When strcmp() returns 0, then s1 and s2 are equal.

  11. Anonymous users2024-01-28

    Store characters in single quotes, not double quotes.

  12. Anonymous users2024-01-27

    Equivalent to char a[4]="abc"。

    There is also a repeated, automatically added character string terminator.

    This is an array initialization that naturally allocates memory, as it is used at initialization""quotation marks, so 0 is automatically added.

    A char is 8 bits, which is one byte, so you need to make a space of four characters on the stack.

    There is one exception.

    char *const p = "abc";

    The string is assigned to the character constant area. Returns a pointer to p. No new on-stack space will be opened.

    The reason why you have to add const is because he points to a static constant and cannot be modified. Of course, you can change the address to which this p points to.

  13. Anonymous users2024-01-26

    It's best to test a question like this, and the following is zhic, c++ test dao,, If you don't move your hands, you can't learn things.

    #include

    #include

    using namespace std;

    char ss="abc";

    int main()

    Results 4313088 ABC

  14. Anonymous users2024-01-25

    1.Yes, the system is initialized as a string"abc" allocated memory.

    2.This "ABC"is the first address of array a.

Related questions