LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2015, 01:02 PM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 271

Rep: Reputation: Disabled
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.
 
Old 03-02-2015, 02:25 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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));
 
Old 03-02-2015, 03:28 PM   #3
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by piobair View Post
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.
 
Old 03-02-2015, 07:58 PM   #4
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 271

Original Poster
Rep: Reputation: Disabled
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?

Last edited by piobair; 03-02-2015 at 08:09 PM. Reason: neglected the * in the value declaration; forgot the ( for the third printf(
 
Old 03-03-2015, 04:18 AM   #5
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
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.
 
Old 03-03-2015, 04:21 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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.)
 
Old 03-03-2015, 05:15 AM   #7
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
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.
 
Old 03-03-2015, 08:57 AM   #8
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 271

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
next size error in realloc() orion9114 Programming 4 11-07-2011 05:12 PM
realloc error on Topas kennynoah AIX 3 08-19-2008 04:33 PM
Error during cimserver start : *** glibc detected *** realloc(): invalid pointer MaverickLikesMustang Linux - Software 0 02-14-2008 06:42 AM
Error (IN NEW FORM) : *** glibc detected *** realloc() topworld Programming 6 04-29-2006 08:24 AM
Error::*** glibc detected *** realloc(): topworld Programming 2 04-26-2006 01:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration