error reallocating 2d array using realloc in c -
I am getting an error in a huge application, when I try to re-specify the 2d int array. While trying to reduce the problem, I only prepared a small code with a reshuffle error:
a.out: malloc.c: 3574: mremap_chunk: objection ((size + Offset) & amp; (mp_.pagesize-1)) == 0 'Failed
Aborted.
The relevant code:
int main () {int ** am = null, size = 0, ans = 0, i; Four ** name = null; While (ans! = 7) {printf ("\ n \ n \ t1. Add a point \ n \ n \ t2.Exit \ n \ n \ t \ tFor your choice:"); Scanf ("% d", & ans); Switch (AN) {Case 1: name = reel (name, (size + 1) * size (four *)); Name [size] = realloc (name [size], 100 * sizeof (four)); Printf ("Enter the name of the point:"); Scanf ("% s", name [size]); Am = realloc (am, (size + 1) * sizeof (int *)); [Size] am = realloc (am [shape], (size +1) * sizeof (int)); `` If (size> 0 & amp; amp; amp; and == faucet) {printf ("Error: Can not allocate memory!"); break; } For (i = 0; i & lt; = size; i ++) {if (i! = Size) {am [size] [i] = INFINITE; At [i] [size] = infinite; } And is [ii] [size] = 0; } Size ++; break; Case 2: Exit (0);
Code> name = reel (name, (size + 1) * size (four *)); Name [size] = realloc (name [size], 100 * sizeof (four)); Memory returned by reloc (zero) is not started
does not mean that you can specify a random indication from the other realloc ()
Are passing. Instead, use malloc ()
The same code goes to realloc ()
calls.
Another problem here:
(i = 0; i You are reaching every am [i]
for [code] am [i] [size] , but only the last one is real There will be many elements in it, allocated amount will be incremented during the while
loop in every round. You have to think again on your logic.
Comments
Post a Comment