Is there any difference in c++ when freeing memory between the free method, or the delete keyword? Thanks.

Yes. Memory allocated with new must be released with delete or delete[]. That's because delete calls the destructor while free() does not.

Thanks for the quick responses. I also have one more question.

char *error = new char[sizeof("test")];
		error = "test";

		ErrorMessage(error);

		delete [] error;

this is throwing a runtime error when it tries to release the memory. Could you explain why? Thanks

Edited 5 Years Ago by LevyDee: n/a

The second line of the code you posted destroyed the pointer to the allocated memory. Pointers can not be changed after allocating memory for the very reason you discovered. When delete[] is called the value of the pointer error is no longer the same as it was then new was called.

ErrorMessage is just a private function to let the dev(me) know when something goes wrong and what it was.

void ServerApp::ErrorMessage(char *message)
{
	MessageBox(NULL, message, "Error", NULL);
}

@AncientDragon

So then how, after I declare my char[] to I fill it with something? Do I need to loop through and assign each element? Is there know what to initialize a dynamic array?

call C language's strcpy() function that is prototyped in string.h.

char *error = new char[sizeof("test")];
strcpy(error,"test");
ErrorMessage(error);
delete [] error;

or just simply do this and no memory allocation is necessary

char *error = "test";
ErrorMessage(error);

or this

ErrorMessage("test");

Edited 5 Years Ago by Ancient Dragon: n/a

I tried the second example there and I get the same runtime error.

My runtime error is:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

I could and it would greatly simplify all of this, but I want to practice more with using pointers and memory allocation and releasing.

if(!_mainWnd)
{
	ErrorMessage("test");		
}

Thats all there is, and the error message function is the same as i posted earlier.

I can live with doing it like this, but will this leave behind memory leaks?

You are mistaking a pointer that is unallocated with one that has allocated memory.

What I mean is that when you put:

char *pStr = "error";

pStr points to the actual storage location of the constant string "error", so if "error" is located at 0x12345678, then pStr has been assigned 0x12345678.

On the other hand, when you use new (or malloc) like this:

char *pStr = new char[128];

You have now allocated 128 bytes of memory from the heap, and the system returns to you the address of that 128 bytes. Let's say the address is 0x87654321. At this point, those 128 bytes are uninitialized, but reserved for your use. So you can copy data over that section of memory. In your original code you did something similar to this:

char *pStr = new char[128];
pStr = "error";
delete [] pStr;

Using my examples above, here is why you got an error. The line the allocated the memory with "new" returned a pointer to 0x87654321. In the next line, you assigned the constant "error" to the same pointer (0x12345678). The pointer to the allocated memory was lost, and replaced by a pointer to the constant. Then when you used "delete" to release the memory, you were trying to release memory to the constant and you got the error. Not only that, but the memory you allocated will not be reclaimed until the program terminates, because you no longer have a pointer to deallocate.

If you are not confused, perhaps this will do it ;-) This is completely valid, because I am saving the pointer values and using them properly. A pointer is exactly what the work says; it simply points to an address in memory.

#define STRING_SIZE 128
char *pStr = new char[STRING_SIZE];
strncpy(pStr, "Not an error", STRING_SIZE - 1);
// Print the pointer value and it's contents.
// %X treats it as a pointer value, and %s gets the string contents
printf("Allocated pointer: %X value: %s\n", pStr, pStr);
char *pConstantStr = "error";
// Print the pointer value and it's contents.
// %X treats it as a pointer value, and %s gets the string contents
printf("Constant pointer: %X value: %s\n", pConstantStr, pConstantStr);
char *pSwap = pStr;
pStr = pConstantStr;
pConstantStr = pSwap;
// Now pStr points to the constant "error", pConstantStr points to the allocated mem
printf("Allocated pointer reassigned constant pointer: %X value: %s\n", pStr, pStr);
printf("Constant pointer reassigned allocated pointer: %X value: %s\n", pConstantStr, pConstantStr);

// So this is now valid
delete[] pConstantStr;

Just to further explain, when you have something like this char *err = "fail" or in your case ErrorMessage("fail") , you are creating the variable on stack on not on heap, so you don't have to delete it. The compiler does it for you. Remember, you only use delete on variables you used new with.

I tried the second example there and I get the same runtime error.

My runtime error is:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

I'll bet you tried to execute

delete [] error;

but because error isn't pointing to memory that was allocated with new , you mustn't delete it.

If you allocate the memory with malloc, realloc, calloc , you can use the free to release the memory

If you allocate the memory with New , you can use delete[] to release the memory

This article has been dead for over six months. Start a new discussion instead.