Is there any difference in c++ when freeing memory between the free method, or the delete keyword? Thanks.
LevyDee 2 Posting Whiz in Training
Recommended Answers
Jump to PostYes. Memory allocated with new must be released with delete or delete[]. That's because delete calls the destructor while free() does not.
Jump to PostYes, use delete when new is used to allocate that memory, and forgot about malloc and free if you are in C++ world. Check here for more info. http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.3
Jump to PostThe 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 …
Jump to Postcall 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");
Jump to PostYou 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 …
All 17 Replies
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
mrnutty 761 Senior Poster
LevyDee 2 Posting Whiz in Training
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
mrnutty 761 Senior Poster
LevyDee 2 Posting Whiz in Training
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
LevyDee 2 Posting Whiz in Training
mrnutty 761 Senior Poster
LevyDee 2 Posting Whiz in Training
mrnutty 761 Senior Poster
LevyDee 2 Posting Whiz in Training
rbross 0 Newbie Poster
mrnutty 761 Senior Poster
arkoenig 340 Practically a Master Poster
LevyDee 2 Posting Whiz in Training
nndung179 0 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.