| | |
free() = delete or delete[]?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
To Dave: Thanks a lot. I've figured out why it sometimes would work and other times it would crash! When a pointer is declared in main(), it has a residual adress to a memory block. Therefore, it can be used BUT if more such pointers exist(uninitialized) the result might be that one overwrites the other etc. etc. or it may actually get the text or variables entered in the console before (C gets all information from a place in the PC's memory where all keys are stored->there is a pointer to that mem. block) So, NEVER EVER use a pointer to a mem. block BEFORE allocating it :mad:
•
•
Join Date: Aug 2009
Posts: 1
Reputation:
Solved Threads: 0
free() works similar to both delete and delete[] according to the sittuation it is used.
for example consider:
int *p=(int*)malloc(sizeof(int));
free(p); //de-allocates memory of one integer size {i.e, sizeof(int);}
// works similar to delete p
int *p=(int*)malloc(sizeof(int*15)); //here 15 is an arbirtrary size
free(p); //de-allocates memory of 15 integers size {i.e, sizeof(int*15);}
//works similar to delete [] p;
for example consider:
int *p=(int*)malloc(sizeof(int));
free(p); //de-allocates memory of one integer size {i.e, sizeof(int);}
// works similar to delete p
int *p=(int*)malloc(sizeof(int*15)); //here 15 is an arbirtrary size
free(p); //de-allocates memory of 15 integers size {i.e, sizeof(int*15);}
//works similar to delete [] p;
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#14 Oct 16th, 2009
I hope this does more to help than to confuse, but here goes another answer. The original question was; which is the same as free(), delete or delete[]? It turns out that none of the three are equal. free() simply "returns" the block of memory back to the pool of available memory while delete "knows" if a destructor needs to be called first. delete[] will call the destructor for each object in an array if needed, before "returning" the memory to the pool of available memory. It is also important to match calls of malloc() with free(), new with delete and new[] with delete[].
Just to point some thing s out, posting #3 is filled with unpredictable behavior. The fact that it's unpredictable is why the different compilers don't react to it the same.
1. a could start out pointing to any memory address (in the stack, in the heap, anywhere).
2. The code will then print out the address of n, not the value. (forgot the semicolon )
3. n could be any integer value, there's no telling how many times this thing is going to loop for.
4. now "a[i] = i" is just writing to some location in memory. Hopefully it doesn't write over anything important like a return address.
5. It's a long shot, but possible that "a[i] = i" wrote over the value of n. This may loop a different number of times from before.
6. Most of the time, this will print out the numbers 0 1 2 3... for however many n now holds the value of.
This code also "works" for strings because strings are just arrays of 1 or 2 byte integers (char or wchar_t respectively) in c/c++. It has little chance of actually working with the desired string length in any way though.
Hopefully this sheds some light on what this code is doing.
Just to point some thing s out, posting #3 is filled with unpredictable behavior. The fact that it's unpredictable is why the different compilers don't react to it the same.
1. a could start out pointing to any memory address (in the stack, in the heap, anywhere).
2. The code will then print out the address of n, not the value. (forgot the semicolon )
3. n could be any integer value, there's no telling how many times this thing is going to loop for.
4. now "a[i] = i" is just writing to some location in memory. Hopefully it doesn't write over anything important like a return address.
5. It's a long shot, but possible that "a[i] = i" wrote over the value of n. This may loop a different number of times from before.
6. Most of the time, this will print out the numbers 0 1 2 3... for however many n now holds the value of.
This code also "works" for strings because strings are just arrays of 1 or 2 byte integers (char or wchar_t respectively) in c/c++. It has little chance of actually working with the desired string length in any way though.
Hopefully this sheds some light on what this code is doing.
![]() |
Similar Threads
- Cant delete avi movie file! HELP (Windows NT / 2000 / XP)
- delete computer history (Windows NT / 2000 / XP)
- dubble delete (C++)
Other Threads in the C++ Forum
- Previous Thread: Problem with dispalying the output...
- Next Thread: C++ vector erase
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets




. But that's the message i got
. So, wait till I find someone else worthy of some thumbs up.:cheesy: 
