1

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

3
  • 1
    What is the value of cntEdges just before the failing call to malloc? Commented Oct 23, 2012 at 17:53
  • 2
    Use a debugger and look at the values of what you're manipulating. Or trace memory usage with a tool like valgrind. Commented Oct 23, 2012 at 17:53
  • The values like cntEdges are correct values. I uploaded the whole source code so you can check it if you want. Commented Oct 23, 2012 at 17:55

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, found the bug with valgrind :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.