dynamic-variable-sized array
Hi there everyone,
I am writing a function within which a 1D array increases in size (increasing number of elements) inside a loop.
In order to achieve this I used the malloc() and realloc() statements, but neither one seems to allocate the memory correctly, producing an error in execution.
Here is my code; should it not be clear enough, please let me know and Ill give you more information.
thank you in advance,
S.M.
Code:
int *_esup1;
int *_esup2;
int _nesup[nnode+1];
int _szesup1 = 1;
int CONN[][3] = {{1,2,6},{2,7,6},{2,3,7},{3,8,7},{3,4,8},{4,9,8},{4,5,9},{5,10,9},{7,8,13},{8,12,13},{8,9,12},{9,11,12},{9,10,11}};
_szesup1 = _esup2[nnode];
_esup1 = malloc((sizeof(int))*(_szesup1);
//Element pass 2: store the elements in _esup1:
for(_ielem=0; _ielem<=12; _ielem++){
for(_inode=0; _inode<=2; _inode++){
//Update storage counter, storing in _esup1:
_ipoin = CONN[_ielem][_inode];
_istor = _esup2[_ipoin-1] + 1;
_esup2[_ipoin-1] = _istor;
_esup1 = malloc((sizeof(int))*(_istor));
printf("_istor %d\t size(ESUP1) %d\n",_istor, sizeof(_esup1));
_esup1[_istor] = _ielem;
}
}