News for hackers. Stuff that matters.
Re:Guide to Advanced Programming in C by vinkelhake on Thursday February 06, 2014 @07:15PM (#7193871)
You suggest using 'calloc' to avoid calculating the size of the allocation (which would be necessary with 'malloc'). This is fine, and you store the number of elements in the 'size' field. But later, in the grow function, you double 'size' and then use it as-is in the call to 'realloc' which expects size to be in bytes, not elements.
Re:Guide to Advanced Programming in C by pfacka on Friday February 07, 2014 @04:43AM (#7195516)
That is because to my knowledge we does not have something like "crealloc" to grow allocated memory effectively. Naive implementation of such function will lead always to coping memory from old to new area and effective would far more difficult to implement (thus more error prone IMHO).
Re:Guide to Advanced Programming in C by vinkelhake on Friday February 07, 2014 @10:31AM (#7196640)
Hi, my point is that you have a bug in the code. Your 'newsize' is the number of elements. When you call realloc (which expects a byte size) you have to take this into account and multiply with the element size. What you have in the code:

    newptr = realloc(vc->data, newsize);
should be changed to:

    newptr = realloc(vc->data, newsize * sizeof(int));