array realloc() segmentation fault

Reply

Join Date: Dec 2008
Posts: 4
Reputation: simone.marras is an unknown quantity at this point 
Solved Threads: 0
simone.marras simone.marras is offline Offline
Newbie Poster

array realloc() segmentation fault

 
0
  #1
Dec 21st, 2008
Hi there, I have a very simple code made of a main.c and a function defined in its own .c and .h files.

Now, within the function I need to increase the size of an array according to the loop index in use.
The problem comes when I allocate the array even before useing the realloc. If I use malloc initially to allocate a multiple of 1000 elements, although the total elements that will be actually used to fill in the array are only about 40, I obtain a segmentation fault. The same happens if I use a realloc trhough the loop.

I hope I have explained myself right and to get some help from any of u!
I report the part of my code where this is done (function PSUP.c).

thank you

  1. /******************************************************************
  2. * PSUP(connectivity_file, nnodes)
  3. *
  4. * Points Surrounding Point (PSUP):
  5. *
  6. * Input:
  7. * - connectivity_file: text file of the connectivity matrix.
  8. * It must contain the conn matrix in the format:
  9. * conn(1:nelem, 1:elnnode)
  10. * where nelem is the mesh number of elements and
  11. * elnnode is the max number of nnode that any element may have.
  12. * ex:
  13. * 2 5 3
  14. * 6 3 1
  15. * 3 1 8
  16. * 9 8 5
  17. * ...
  18. *
  19. * NOTE that the first column does NOT contain the numbering of the elements!!!
  20. *
  21. * - nnodet: Number of points in the mesh
  22. *
  23. * Output:
  24. * - max_psup is the output: maximum number of nodal contacts inside the mesh
  25. *
  26. *
  27. * REFERENCES:
  28. * [1] Lohner,R. (2008), Applied computational fluid dynamics techniques",ed.II, John Wiley and Sons
  29. *
  30. ******************************************************************/
  31.  
  32. #include<stdio.h>
  33. #include<stdlib.h>
  34. #include<math.h>
  35.  
  36. int PSUP(int CONN[][3], int nnode, int nelem, int max_nnode, unsigned int array_numb)
  37. //int PSUP(int **CONN, int nnode, int nelem, int max_nnode, unsigned int array_numb)
  38. {
  39. int _ipoin, _inode, _ielem, _ipoi1, _ipoi2, _istor;
  40.  
  41. printf("\n #Elements surrounding points#\n");
  42.  
  43. //Initialize _esup2:
  44. int *_esup1;
  45. // int _esup1[nnode*1000];
  46. int _esup2[nnode+1];
  47. int _nesup[nnode+1];
  48.  
  49. _esup1 = malloc((sizeof(int))*nnode*1000);
  50.  
  51. /****************************/
  52. IN THIS PART THERE ARE SOME LINES
  53. WHERE THE ARRAYS USED NEXT ARE ASSIGNED, AND IT WORKS CORRECTLY.
  54. iT IS A LONG PART, SO i DONT PUT IT FOR THE SAKE OF READABILITY.
  55. /****************************/
  56.  
  57.  
  58. for(_ielem=array_numb; _ielem<=nelem-1+array_numb; _ielem++){
  59. for(_inode=array_numb; _inode<=max_nnode-1+array_numb; _inode++){
  60. //Update storage counter, storing in _esup1:
  61. _ipoin = CONN[_ielem][_inode];
  62. _istor = _esup2[_ipoin] + 1;
  63.  
  64. _esup2[_ipoin] = _istor;
  65. // _esup1 = realloc(_esup1,(_istor+1)*sizeof(int));
  66. _esup1[_istor] = _ielem;
  67.  
  68. }
  69. }
  70.  
  71. free(_esup1);
  72.  
  73. return; //*_nesup;
  74. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: array realloc() segmentation fault

 
0
  #2
Dec 21st, 2008
> int PSUP(int CONN[][3], int nnode, int nelem, int max_nnode, unsigned int array_numb)
> //int PSUP(int **CONN, int nnode, int nelem, int max_nnode, unsigned int array_numb)
OK, so which is it?
Unlike single dimensional arrays, where you can switch the notation from int [] to int* at will, the same is NOT true for higher dimensions.
You can't simply change the declaration without altering what the caller passes to the function.

If you're getting any kind of warnings about calling this function, you really need to fix them, because the code is broken.

Which brings me to the next point.
Even though there is nothing obviously wrong(*) with the malloc/realloc calls, dynamic memory allocation in particular has a problem where the true cause of the problem is often nowhere near where a problem is observed.

Try for yourself, and copy/paste this function into a MINIMAL support program (just a main, and essential declarations), and try to call it. If it crashes, then you know the problem is here. If not, then the problem is somewhere else.

(*)Except for
a) not checking for a NULL result to begin with
b) not allowing for the possibility of realloc returning NULL, thus trashing your pointer to the memory you still have. See previous posts for how/why.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: simone.marras is an unknown quantity at this point 
Solved Threads: 0
simone.marras simone.marras is offline Offline
Newbie Poster

Re: array realloc() segmentation fault

 
0
  #3
Dec 21st, 2008
Originally Posted by Salem View Post
> int PSUP(int CONN[][3], int nnode, int nelem, int max_nnode, unsigned int array_numb)
> //int PSUP(int **CONN, int nnode, int nelem, int max_nnode, unsigned int array_numb)
OK, so which is it?
Unlike single dimensional arrays, where you can switch the notation from int [] to int* at will, the same is NOT true for higher dimensions.
You can't simply change the declaration without altering what the caller passes to the function.

If you're getting any kind of warnings about calling this function, you really need to fix them, because the code is broken.

Which brings me to the next point.
Even though there is nothing obviously wrong(*) with the malloc/realloc calls, dynamic memory allocation in particular has a problem where the true cause of the problem is often nowhere near where a problem is observed.

Try for yourself, and copy/paste this function into a MINIMAL support program (just a main, and essential declarations), and try to call it. If it crashes, then you know the problem is here. If not, then the problem is somewhere else.

(*)Except for
a) not checking for a NULL result to begin with
b) not allowing for the possibility of realloc returning NULL, thus trashing your pointer to the memory you still have. See previous posts for how/why.

1) Hi there Salem, thanks so much for your reply; to first reply about the declariation of the function, the way it is set for the time being (the uncommented version) is the correct one because I am testing this function first inside a main.c where arrays are not pointers. However, the commented version of the function declaration will be used once this function works correctly and I can port it into my real code elsewhere. So, no worry about the declaration for the time being.

2) During compilation I have in fact no warnings (or errors) of any sort, and if furthermore, instead of using malloc to allocate *_esup1, I declare _esup1 as a local array with a fixed size of 1000 elements, the code executes correctly without any fault.

That is what I dont really explain: why I cannot use a malloc declaration inside this function.

Thank you again
Best
S
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: array realloc() segmentation fault

 
0
  #4
Dec 21st, 2008
> for(_inode=array_numb; _inode<=max_nnode-1+array_numb; _inode++)
So is this always 0 to 3 ?
To match your CONN array.

Checking the array indices would be a start, say
assert( i >= 0 && i < 3 );

If you compile a debug build, and run the code in the debugger, you should automatically break at the first assert which fails.
You can then examine the variables and your logic.

> That is what I dont really explain: why I cannot use a malloc declaration inside this function.
In a correct program, there would be no problem.

Despite the fact that your static array 'works', I would still suspect that you're running off the ends in some way. It's just that with a static array you get away with it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: simone.marras is an unknown quantity at this point 
Solved Threads: 0
simone.marras simone.marras is offline Offline
Newbie Poster

Re: array realloc() segmentation fault

 
0
  #5
Dec 21st, 2008
Hi again, thanks for replying; I have never used assert(), how would you use it inside this type of code? would that be inside a loop in the CONN array columns?

thanks again
All best
s.

Originally Posted by Salem View Post
> for(_inode=array_numb; _inode<=max_nnode-1+array_numb; _inode++)
So is this always 0 to 3 ?
To match your CONN array.

Checking the array indices would be a start, say
assert( i >= 0 && i < 3 );

If you compile a debug build, and run the code in the debugger, you should automatically break at the first assert which fails.
You can then examine the variables and your logic.

> That is what I dont really explain: why I cannot use a malloc declaration inside this function.
In a correct program, there would be no problem.

Despite the fact that your static array 'works', I would still suspect that you're running off the ends in some way. It's just that with a static array you get away with it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: array realloc() segmentation fault

 
0
  #6
Dec 21st, 2008
How is like I showed you.

Where is anywhere you like where you wish to assert that something "must" be true at this point in the code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: simone.marras is an unknown quantity at this point 
Solved Threads: 0
simone.marras simone.marras is offline Offline
Newbie Poster

Re: array realloc() segmentation fault

 
0
  #7
Dec 21st, 2008
Hi Salem, thanks a lot for all the help; I'll figure it out and get the problem solved one way or another

All the best and merry xmas!
s
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: array realloc() segmentation fault

 
0
  #8
Dec 21st, 2008
You can use the reference if you aren't sure of how the assert function works. I think in your program, you've to "assert" that the value of the variable _inode stays within 0 to 3 and this has to be done in the for loop. If the value of that variable is anything other than 0-3, then an error is logged and the program is terminated instead of giving you a segmentation fault.
Last edited by devnar; Dec 21st, 2008 at 4:55 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC