473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Problem in Free() on 2D array

72
Hi,

Please see the below code snipet, its giving segmentation fault while freeying array[i]. Anybody help in whats going wrong?

Expand|Select|Wrap|Line Numbers
  1. void deallocate2D(int** array, int nrows) {
  2.  
  3.      /*  deallocate each row  */
  4.      int i;
  5.      for(i = 0; i < nrows; i++) {
  6.           printf("deallocate each row %d\n",i);
  7.           free(array[i]);
  8.      }
  9.  
  10.      /*  deallocate array of pointers  */
  11.      printf("deallocating array of pointers\n");
  12.      free(array);
  13.  
  14. }
Thanks in adavance

Manjunath
Apr 21 '10 #1

✓ answered by Banfa

You have not returned the value of pointer allocated in allocate2D to main, the pointer arr in main remains uninitialised and then you pass it to deallocate2D which tries to free the uninitialised pointer that is passed to it.

I suggest you return the pointer allocated in allocate2D via the return value of the function. Alternatively you need to pass the first parameter of the function allocate2D as int***. Remember C is pass by value, if you aren't dereferencing a pointer passed into a function you are only working on local data.

5 8433
Banfa
9,065 Expert Mod 8TB
For what value of i?

Did you actually allocate the memory in the way you are trying to free it?
Have you called deallocate2D twice by accident?
Have you call deallocate2D with the correct value for nrows?

free generally only fails in that way if the pointer given is not a valid heap pointer i.e. a pointer to somewhere other than the heap or a pointer into the heap that has already been freed or 0.
Apr 21 '10 #2
manjuks
72
Hi Bafna,

Thanks for the reply...
Please have alook into the below full program.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void allocate2D(int** array, int nrows, int ncols) {
  5.  
  6.      /*  allocate array of pointers  */
  7.      printf("Allocating memory for array of pointers\n");
  8.      array = ( int** )malloc( nrows*sizeof( int* ) );
  9.  
  10.      /*  allocate each row  */
  11.      int i;
  12.      for(i = 0; i < nrows; i++) {
  13.           printf("Allocating memory for each row %d\n",i);
  14.           array[i] = ( int* )malloc( ncols*sizeof( int ) );
  15.      }
  16.  
  17. }
  18.  
  19. void deallocate2D(int** array, int nrows) {
  20.  
  21.      /*  deallocate each row  */
  22.      int i;
  23.      for(i = 0; i < nrows; i++) {
  24.           printf("deallocate each row %d\n",i);
  25.           free(array[i]);
  26.      }
  27.  
  28.      /*  deallocate array of pointers  */
  29.      printf("deallocating array of pointers\n");
  30.      free(array);
  31.  
  32. }
  33.  
  34. int main()
  35. {
  36.   int **arr;
  37.   allocate2D(arr,2,2);
  38.   deallocate2D(arr,2);
  39.   return 0;
  40. }
I am not able to understand where its going wrong. Please help me in understnding the problem.
Apr 21 '10 #3
Banfa
9,065 Expert Mod 8TB
You have not returned the value of pointer allocated in allocate2D to main, the pointer arr in main remains uninitialised and then you pass it to deallocate2D which tries to free the uninitialised pointer that is passed to it.

I suggest you return the pointer allocated in allocate2D via the return value of the function. Alternatively you need to pass the first parameter of the function allocate2D as int***. Remember C is pass by value, if you aren't dereferencing a pointer passed into a function you are only working on local data.
Apr 21 '10 #4
manjuks
72
Thanks a lot Bafna. Now I understood whats the problem, and solved it.
Thank you very much.

Thanks
Manjunath
Apr 21 '10 #5
donbock
2,426 Expert 2GB
Furthermore, allocate2D should be testing the return value from malloc each time. If your computer runs out of memory in the middle of allocate2D execution then you end up with a partially allocated 2D array, which will cause all kinds of problems for you.

If allocate2D gets an error from malloc it should deallocate the partially allocated 2D array and return NULL. Your main program should test the return value of allocate2D.

deallocate2D can be used to deallocate a partially allocated 2D array if you test each pointer for NULL before calling free.
Apr 21 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
13
by: Bob | last post by:
I have been working on the following program. The goal is to have a tokenizing routine that avoids some of the problems of strtok(), the comments should explain the features. This runs fine on...
15
by: syang8 | last post by:
hi, folks, I use Kdevelop to build some scientific simulation on Linux. If I set the size of an array N = 8000, the program works fine. However, if I set the array N some number greater than...
1
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.