![]() |
| | #1 |
| Registered User Join Date: Jun 2008
Posts: 45
| Trouble freeing a pointer What can be the reasoning behind this? Has anyone ever had this problem before? Thanks. |
| magda3227 is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| free() does not change the pointer passed in. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Jun 2008
Posts: 45
| So how is that freeing memory if the values stored within the pointer are not changed? Doesn't it take memory to keep those values there? In another area of my program, when I free a pointer, the values are not the same. |
| magda3227 is offline | |
| | #4 | |
| The larch Join Date: May 2006
Posts: 3,082
| The memory is released, meaning you shouldn't access this memory through the pointer again. It may retain the same memory contents until this memory is reused for some other allocation.
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #5 |
| Registered User Join Date: Apr 2007 Location: Sydney, Australia
Posts: 217
| I suppose the values are still stored there, but to the OS that area is considered free. It's like formatting your hard drive. The same values are still there but it's all considered free space that may be replaced later on. Theres no point in wasting CPU by nulling the bytes. |
| 39ster is offline | |
| | #6 |
| Registered User Join Date: Jun 2008
Posts: 45
| I see. I now also found out I'm getting segmentation violation when I try to free some of my other pointers, even though I no longer use them. I actually allocated them within in a switch statement. I would assume that it does not make much of a difference from allocating the memory before the switch. Is this a bad idea to allocate the memory in a switch statement? |
| magda3227 is offline | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #8 |
| Registered User Join Date: Jun 2008
Posts: 45
| I recently came across something that has been giving me seg. viol. If I want to create a 2-D array of size 2x6, when I allocate memory for my pointer, I would assume that it needs to be done as such... Code: int row=2; int col=6; int **pointer=malloc(row*sizeof(int*)); for(i=0;i<col; i++) pointer[i] = malloc(col*sizeof(int)); But for some reason, the pointer will work, but I can't free that pointer...it won't work. I get seg. faults. In order to be able to free the pointer I created, it makes me allocate a 6x6 block of memory. Why is this so? Am I missing some basic concept about pointers and memory allocation? |
| magda3227 is offline | |
| | #9 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Because you are perhaps confusing your colums and rows? Based on the above code you would have an array corresponding to int array[2][6]. If you ever go pointer[2] .. = then you are outside the memory you allocated. If you can fix the problem by using array[6][6], then you are almost certainly likely accessing outside your array. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #10 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > for(i=0;i<col; i++) This should be row, not col
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #11 |
| Registered User Join Date: Jun 2008
Posts: 45
| |
| magda3227 is offline | |
![]() |
| Tags |
| free, malloc, memory allocation, pointer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic array of structures containing yet another dynamic array of structures | innqubus | C Programming | 2 | 07-11-2008 07:39 AM |
| Having trouble using the keyword new inside of a function when passing a pointer in. | klanglie | C++ Programming | 4 | 06-15-2005 08:30 PM |
| Could somebody please help me with this C program | brett73 | C Programming | 6 | 11-25-2004 02:19 AM |
| ascii and pointer trouble!!! | algi | C++ Programming | 1 | 11-16-2004 03:00 PM |
| Struct *** initialization | Saravanan | C Programming | 20 | 10-09-2003 12:04 PM |