0

My code (or the example from the book) seems to not work. I know it's the realloc function causing this, since I will allocate some memory to a set amount at the beginning. If I surpass the original amount I allocated, I will realloc() to increase the allocation.

The allocated memory is for a bunch of pointers, the more pointers I need, the more memory I need. These pointers will ultimately point to a string from an input. Regardless, the crashing occurs whenever I try to access the data at the string during a specific situation.

I set my COUNT (the amount I allocate originally) to let's say 5. It will crash after the sixth input. But, if I only have 5, no crashing. Thus, I believe this has something to do with realloc().

#define COUNT 5 // Initial number of strings
//code in between here

// ...
char** psTemp = NULL; // Temporary pointer to pointer to char
char* pTemp = NULL; // Temporary pointer to char
size_t str_count = 0; // Current string count
size_t capacity = COUNT; // Current maximum number of strings
// ...

if (str_count == capacity)
    {
        capacity += capacity / 4; // Increase capacity by 25%
        if (!(psTemp = realloc(pS, capacity))) return 1;
        pS = psTemp;
    }

Above you can see my COUNT and realloc() function. I will update the allocation whenever I reach limit, when my current number of input (str_count) reaches the current allocation (capacity).

I've tried this in Visual Studio and Pelles C and both give me similar issues.

In Wisual Studio '13, it will crash after my 6th input, since my allocation is originally set to five.

In Pelles C, it will crash after my 8th input, why? I have no clue.

I ran it through the debugger, the realloc() works, and the updated allocation works, yet it crashes.

I'm trying to self teach my self C but this book has too many of these weird errors!

Here is my code, I have realloc() occur only once in my code: http://ideone.com/bY6CXk.

unwind
  • 391,730
  • 64
  • 469
  • 606

1 Answers1

1

You're not scaling the size of the allocation by the type of the data. This means you're just allocating capacity bytes, when you probably need capacity * sizeof *psTemp. This assumes the type of psTemp is proper, i.e. not void *. If you had shown more code it would be easier to help.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I shared with you my entire code, please click on the ideone link! Thanks though, it worked! Why DIDNT THE GUY WHO WROTE THE BOOK DO THAT? – Ziping Liu Aug 20 '14 at 07:27
  • 1
    @LivePresently Many authors don't know C very well, yet write books about it. Stick with K&R. – unwind Aug 20 '14 at 07:36
  • @LivePresently We can't possibly answer that since we don't know what book you're talking about. It could well be that the book is fine and that you didn't adapt it correctly. Perhaps the book allocates char arrays, where the element size is 1 so no multiply is required. – Jim Balter Aug 20 '14 at 07:44
  • I copied the code. I did not alter it in any way. This was one purely from the book it's this one :http://www.amazon.com/gp/aw/d/1430248815?pc_redir=1408425121&robot_redir=1 – Ziping Liu Aug 20 '14 at 08:02
  • Unfortunately, software textbooks are notorious for non-working code. – Martin James Aug 20 '14 at 09:09