开发者

Does this generate a memory leak?

开发者 https://www.devze.com 2023-01-15 03:19 出处:网络
void aFunction_2() { char* c = new char[10]; c = \"abcefgh\"; } Questions: Will the: c = \"abdefgh\" be stored in the new char[10]?
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.


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, deleteing 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!


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

精彩评论

暂无评论...
验证码 换一张
取 消