1
votes

I have to create program, which is able to generate a GML file.

For this purpose I implemented a function GetEdges, this functions should returns three arrays (call by reference).

The signature of the function looks like:

bool GetEdges(DG_NODE_ID **sourceIds, DG_NODE_ID **destIds, int **weights, int *count)

Within the function I want to malloc space:

*sourceIds = (DG_NODE_ID *) malloc(cntEdges * sizeof (DG_NODE_ID));

As soon as I use 4 nodes i get following output:

graph: malloc.c:2451: 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.

I tried everything and I do not have any clue why this fails after I use 4 nodes.

I uploaded the whole source code: Download - Source code

2
What is the value of cntEdges just before the failing call to malloc? - Greg Bacon
Use a debugger and look at the values of what you're manipulating. Or trace memory usage with a tool like valgrind. - Alex Reynolds
The values like cntEdges are correct values. I uploaded the whole source code so you can check it if you want. - MeJ

2 Answers

0
votes

It most probably means you are abusing the memory that is allocated by:

  1. Writing before the start of what you were allocated (uncommon).
  2. Writing after the end of what you were allocated (common).
  3. Freeing something that wasn't previously allocated (uncommon).
  4. Writing to previously allocated space after it was freed (common).

Do you have valgrind? If so, use it. If not, get it and use it if at all possible. (It's available for many Unix-like systems; it is not available for Windows AFAIK.)

0
votes

Have you verified that cntEdges is a reasonable value and not some uninitialized (and very large number) value?