-1

I am getting an sYSMALLOc error message on C program, I believe this error message is linked to mallocs I have used, and also I found this out by putting printf statements around a malloc, and it is causing the issue. I cannot see anything wrong with it. Any solutions? Please help, the code is as bellow. Also, it runs fine for some time, then it breaks. with the error message, as follows.

program: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr)
(((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof 
(struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) 
(old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk,
fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1)))
&& ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

node * new(char c)
{   
    NODE *node = (NODE *)malloc(sizeof(NODE));  

    node->c = c;

    return node;    
}

EDIT: I just found out that the following code might be what is causing the error to occur. Can anyone see anything wrong in it?

void move(char *string)
{
 int length = strlen(string);               
 node->s = (char *)malloc(length*sizeof(char));         

 strcpy(node->s, string);
}

this function copies the string parameter to node->s

Jordan
  • 327
  • 8
  • 23
  • 2
    Your code (the part we can't see) most likely writes to parts outside the malloced memory and trashes internal malloc data. In other words, there's a bug. – Jens Apr 05 '14 at 20:49
  • makes sense, just trying to figure it out how I will fix it. There are other two places where I use malloc on my program – Jordan Apr 05 '14 at 20:58

2 Answers2

2
node * new(char c) {   
    NODE *node = malloc(sizeof(NODE));
    if(node) {  
     node->c = c;
    }
    return node;    
}

Your above code is correct. But the problem is you have assumed that following line is causing the problem as some printf statements before this line was executed. This is not correct.

The above error message indicates that your prior logic of your program(somewhere else) has corrupted the heap memory.This exception/error message is just after effect and not the real problem. You should try to use some dynamic tool(Valgrind) on Linux and WinDBG/PageHeap on Windows to identify the root cause of memory corruption in your program.

EDIT You may want to check my previous post on how to use Valgrind and GDB for debugging your program: https://stackoverflow.com/a/22658693/2724703

For Simple usage, you can use the following command(if your program is a.out)

$valgrind ./a.out

Community
  • 1
  • 1
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
  • I will try to use Valgrind, trying to figure it out how to use it, as I never used it before. There are other two places where I use malloc. So would you say I should look closely on those mallocs too!! – Jordan Apr 05 '14 at 20:58
  • @Jordan: Please check out the edited post which has the information about how to use Valgrind. Regarding your 2nd point, yes you should verify your malloc calls and check out for any overflow scenario...however potentially any module(3rd party/library) which is loaded within program can corrupt the memory so best way to find out such error is to use dynamic tool(valgrind). – Mantosh Kumar Apr 05 '14 at 21:07
  • Thanks, trying to install Valgrind, and now this is becoming a second issue, dont know how to install – Jordan Apr 05 '14 at 21:19
2

A string is terminated by a zero byte. You need one more byte than its length (don't use sizeof(char) it is always 1). So code

void move(char *string) {
  size_t length = strlen(string);               
  node->s = malloc(1+length); 
  if (!node->s) 
    { perror("malloc string"); exit(EXIT_FAILURE); };        
  strcpy(node->s, string);
}

or simply using strdup(3)

void move(char *string) {
  node->s = strdup(string);
  if (!node->s)
     { perror("move strdup"); exit(EXIT_FAILURE); }
}

Don't forget that malloc(3) can fail (On Linux, you could lower some limit with ulimit shell builtin to test that,i.e. to get a malloc failure for testing purposes.).

Use also valgrind (and of course your gdb debugger).

BTW, this old question is very similar to yours!

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you so much, I think adding one at the end is helping the program a lot. However, using valgrind I found out there is 46,669,439 bytes in 1,264,549 blocks still reachable, so how do I clear this? and other options in valgring were all 0. Do you know why? – Jordan Apr 05 '14 at 22:23
  • @Jordan: I would suggest you to post a different question to track this problem.This looks different than your current problem. – Mantosh Kumar Apr 06 '14 at 05:22