error using realloc
code fragment:
Code:
typedef struct ssubcase ssubcase; Code:
isubcase->value = realloc(isubcase->value, sizeof(int ) *(isubcase->ncards)); a.out: malloc.c:2842: mremap_chunk: Assertion `((size + offset) & (_rtld_global_ro._dl_pagesize - 1)) == 0' failed. I am clueless as to what is happening. It makes no sense to me why that line fails after the line two lines previously, which is essentially identical, fails. |
You might have forgotten to initialize your variables.
Exeample: Code:
ssubcase sc, *psc; |
Quote:
Code:
isubcase->type = realloc(isubcase->type, sizeof(int ) *(++isubcase->ncards)); The correct way to use realloc is: Code:
char* tmp = realloc (my_buffer, my_new_size); The best way to get help, and to help yourself, with a programming problem is to post something that will compile. What you've posted isn't valid C. |
Simplified to essentials, here it is again:
Code:
int *type=NULL, *value = NULL, n=0, i; The following line crashes the program with the error message: a.out: malloc.c:2842: mremap_chunk: Assertion `((size + offset) & (_rtld_global_ro._dl_pagesize - 1)) == 0' failed. I.e. the program does not reach "you are here (2)" I agree that to be rigorous, I should probably use: Code:
if((value = realloc(value, sizeof(int) *n)) == NULL){ |
For what it's worth, if I compile and run that "simplified to essentials" code with gcc 4.9.2, it doesn't crash.
The output is, of course: Code:
you are here (1) n=6 |
Basically, without a complete (compilable!) example this question cannot be answered. (And even if it were, the OP seems to be in write/only mode.)
|
Yes, as NemenTeve says, the problem lies elsewhere. The error message is an assertion failure within the malloc that is called by realloc, which indicates that something has already corrupted memory at that point. One way forward is to use valgrind to track down where the corruption is actually happening.
|
I did a test with an included malloc(103) (i.e (sizeof(char*) + 103) subtracted from the heap for each loop) and looped up to 20000 iterations. If realloc was going to crap out due to crossing a page boundary, it should have shown up with that test. It ran fine.
I havent'f found the error in my program yet, but apparently the realloc isn't the problem. I am marking this thread as "solved" Thaks one and all for your support. |
All times are GMT -5. The time now is 01:26 PM. |