LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error using realloc (https://www.linuxquestions.org/questions/programming-9/error-using-realloc-4175535552/)

piobair 03-02-2015 01:02 PM

error using realloc
 
code fragment:
Code:

typedef struct ssubcase ssubcase;
struct ssubcase {ssubcase *next; char *subtitle; int id, ncards, *type, *value;}newsubcase, *subcase0 = NULL, *subcase_n = NULL;
int i, j, nunc_cont;
char line[80] **unc_cont;
const unsigned NAI = -1; // translates as "not an integer"

      i = 0;
      isubcase->type = realloc(isubcase->type, sizeof(int ) *(++isubcase->ncards));
      isubcase->type[isubcase->ncards] = NAI;
      isubcase->value = realloc(isubcase->value, sizeof(int ) *(isubcase->ncards));
      isubcase->value[isubcase->ncards] = nunk_cont++;
      unk_cont = realloc(unk_cont, sizeof(char**) *nunk_cont);
      while(line[i] != 0)i++;
      i++;
      unk_cont[nunk_cont -1] = malloc(sizeof(char *) *i);
      for(j=0; j < i; j++)unk_cont[nunk_cont -1][j] = line[j];

This fragment is executed five times successfully. The sixth time, the line:
Code:

      isubcase->value = realloc(isubcase->value, sizeof(int ) *(isubcase->ncards));
fails to execute, with the following error message:
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.

NevemTeve 03-02-2015 02:25 PM

You might have forgotten to initialize your variables.

Exeample:
Code:

ssubcase sc, *psc;

memset (&sc, 0, sizeof (sc));

psc= malloc (sizeof (*sc));
memset (psc, 0, sizeof (*psc));


SoftSprocket 03-02-2015 03:28 PM

Quote:

Originally Posted by piobair (Post 5325760)
code fragment:
Code:

typedef struct ssubcase ssubcase;
struct ssubcase {ssubcase *next; char *subtitle; int id, ncards, *type, *value;}newsubcase, *subcase0 = NULL, *subcase_n = NULL;
int i, j, nunc_cont;
char line[80] **unc_cont;
const unsigned NAI = -1; // translates as "not an integer"

      i = 0;
      isubcase->type = realloc(isubcase->type, sizeof(int ) *(++isubcase->ncards));
      isubcase->type[isubcase->ncards] = NAI;
      isubcase->value = realloc(isubcase->value, sizeof(int ) *(isubcase->ncards));
      isubcase->value[isubcase->ncards] = nunk_cont++;
      unk_cont = realloc(unk_cont, sizeof(char**) *nunk_cont);
      while(line[i] != 0)i++;
      i++;
      unk_cont[nunk_cont -1] = malloc(sizeof(char *) *i);
      for(j=0; j < i; j++)unk_cont[nunk_cont -1][j] = line[j];

This fragment is executed five times successfully. The sixth time, the line:
Code:

      isubcase->value = realloc(isubcase->value, sizeof(int ) *(isubcase->ncards));
fails to execute, with the following error message:
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.

There is some seriously ugly stuff in there. It's a hard to read, incomplete code segment which makes it hard to help with but in ...
Code:

isubcase->type = realloc(isubcase->type, sizeof(int ) *(++isubcase->ncards));
You really should increment the value of isubcase->ncards before using it to be sure that what happens is what you expect.

The correct way to use realloc is:
Code:

char* tmp = realloc (my_buffer, my_new_size);
if (tmp == NULL) {
    // do something to deal with the problem
}
my_buffer = tmp;
// safe to use my_buffer

I'm not going to untangle your types so swap your type for char*. If realloc fails it won't screw up your original pointer, which you can free.

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.

piobair 03-02-2015 07:58 PM

Simplified to essentials, here it is again:
Code:

int *type=NULL, *value = NULL, n=0, i;
for(i = 0; i< 5; i++){
  type = realloc(type, sizeof(int) * ++n);
  value = realloc(value, sizeof(int) *n);
}

type = realloc(type, sizeof(int) * ++n);
printf("you are here (1) n=%d\n", n);
value = realloc(value, sizeof(int) *n);
printf("you are here (2) n=%d\n", n);

"you are here (1)" reports that n = 6
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){
  printf("Oops"\n");
  exit(-1);
}

Am I getting a page fault? How do I ovoid it?

Dr_P_Ross 03-03-2015 04:18 AM

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
you are here (2) n=6

Something else is going on that relates to other parts of your code.

NevemTeve 03-03-2015 04:21 AM

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.)

Dr_P_Ross 03-03-2015 05:15 AM

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.

piobair 03-03-2015 08:57 AM

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.