why we never delete a pointer twice


 
Thread Tools Search this Thread
Top Forums Programming why we never delete a pointer twice
# 1  
Old 08-13-2007
why we never delete a pointer twice

can u tell me the reson that why we should not delete a pointer twice.?
if we delete ponter twice then what happen and why this happen
Regards,
Amit
# 2  
Old 08-13-2007
If you buy a car, can you you then sell it twice? No, because the 2nd time you no longer own it.

This is the same with memory, you allocate it, then release it. Once you have released the memory goes back into the free pool ready to be given out again.

If you release the pointer twice, then that memory may have been allocated between those two deletes to somebody else. You have then just freed some memory that another part of the code was trying to use.

In the words of the Fat Controller, you will cause chaos and confusion.
# 3  
Old 08-13-2007
Quote:
Originally Posted by amitpansuria
can u tell me the reson that why we should not delete a pointer twice.?
if we delete ponter twice then what happen and why this happen
Regards,
Amit
And may i add, if you just dive a little deeper into the malloc.c code (which i doubt you will Smilie ...)it will mark 'something' according to the structure you give as paramater to free(), if the memory has already been freed then it will 'access' some invalid memory and will inevitably segfault ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines? I have talked to one of the curators of the forum, but I am still not quite clear. Here... (1 Reply)
Discussion started by: yifangt
1 Replies

2. Programming

pointer conversion in c

hi there fellos could someone teach me how to convert a character pointer into an integer using c much love (1 Reply)
Discussion started by: surubi_abada
1 Replies

3. Programming

C dynamic pointer

Hi, Can anyone tell me how i can declare and allocate dynamically an array of pointers to structured type?? Is declaration something like this:? struct_name ** array; (1 Reply)
Discussion started by: littleboyblu
1 Replies

4. Programming

matrix pointer

Can anyone tell me what the following statements do? float (*tab); tab=(float (*)) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float)); (3 Replies)
Discussion started by: littleboyblu
3 Replies

5. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

6. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

7. Programming

address of pointer

Hi i'm new to c programming and i'm trying to change the address of a pointer/variable but i can't seem to get it right, I have this char heap; char *firstFree = heap; char *allocMem( int size ) { void *malloc(size_t sizeofint); /*allocate space for an array with size... (19 Replies)
Discussion started by: Poison Ivy
19 Replies

8. Programming

Regarding char Pointer

Hi, char *s="yamaha"; cout<<s<<endl; int *p; int i=10; p=&i; cout<<p<<endl; 1) For the 1st "cout" we will get "yamaha" as output. That is we are getting "content of the address" for cout<<s. 2) But for integer "cout<<p" we are getting the "address only". Please clarify how we are... (2 Replies)
Discussion started by: sweta
2 Replies

9. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies

10. Programming

Will we get SEGV if we try to “delete []” un-initialized integer pointer variable.

I have a class with an integer pointer, which I have not initialized to NULL in the constructor. For example: class myclass { private: char * name; int *site; } myclass:: myclass(....) : name(NULL) { ..... } other member function “delete “ the variable before... (2 Replies)
Discussion started by: sureshreddi_ps
2 Replies
Login or Register to Ask a Question