简体   繁体   中英

Does this generate a memory leak?

void aFunction_2()
{
    char* c = new char[10];
    c = "abcefgh";
}

Questions:

  1. Will the: c = "abdefgh" be stored in the new char[10] ?

  2. If the c = "abcdefgh" is another memory area should I dealloc it?

  3. If I wanted to save info into the char[10] would I use a function like strcpy to put the info into the char[10] ?

Yes that is a memory leak.

Yes, you would use strcpy to put a string into an allocated char array.

Since this is C++ code you would do neither one though. You would use std::string.

void aFunction_2()
{
    char* c = new char[10]; //OK
    c = "abcefgh";          //Error, use strcpy or preferably use std::string
}

1- Will the: c = "abdefgh" be allocated inner the new char[10]?

no, you are changing the pointer from previously pointing to a memory location of 10 bytes to point to the new constant string causing a memory leak of ten bytes.

2- If the c = "abcdefgh" is another memory area should I dealloc it?

no, it hasn't been allocated on heap, its in read-only memory

3- If I wanted to save info inner the char[10] I would use a function like strcpy to put the info inner the char[10]?

not sure what you mean with 'inner' here. when you allocate using new the memory is allocated in the heap and in normal circumstances can be accessed from any part of your program if you provide the pointer to the memory block.

  1. No (pointer reassignment)
  2. No
  3. Yes, you can use strcpy() , see for instance :

Your answer already has been answered multiple times, but I think all answers are missing one important bit (that you did not ask for excplicitly): While you allocated memory for ten characters and then overwrote the only pointer you have referencing this area of memory, you are created a memory leak that you can not fix anymore. To do it right, you would std::strcpy() the memory from the pre-allocated, pre-initialized constant part of the memory where the content of your string-literal has been stored into your dynamically allocated 10 characters.

And here comes the important part:

When you are done with dealing with these 10 characters, you deallocate them using delete[] . The [] are important here. Everything that you allocate using new x[] has to be deallocated with delete[] . Neither the compiler nor the runtime warn you when use a normal delete instead, so it's important to memorize this rule.

  1. No, that is only pointer reassignment;
  2. No, delete ing something that didn't come from new will often crash; and
  3. Yes, strcpy will do the job… but it's not usually used in C++.

Since nobody has answered with code, std::uninitialized_copy_n (or just std::copy_n , it really doesn't make a difference here) is more C++ than strcpy :

#include <memory>

static char const abcs[] = "abcdefgh"; // define string (static in local scope)
char *c = new char[10]; // allocate
std::copy_n( abcs, sizeof abcs, c ); // initialize (no need for strlen)

// when you're done with c:
delete[] c; // don't forget []

Of course, std::string is what you should use instead:

#include <string>
std::string c( "abcdefgh" ); // does allocate and copy for you
// no need for delete when finished, that is automatic too!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM