0

I'm having trouble implementing realloc in a very basic way.

I'm trying to expand the region of memory at **ret, which is pointing to an array of structs with ret = realloc(ret, newsize); and based on my debug strings I know newsize is correctly increasing over the course of the loop (going from the original size of 4 to 8 to 12 etc.), but when I do sizeof(ptr) it's still returning the original size of 4, and the things I'm trying to place into the newly allocated space can't be found (I think I've narrowed it down to realloc() which is why I'm formatting the question like this)

I can post the function in it's entirety if the problem isn't immediately evident to you, I'm just trying to not "cheat" with my homework too much (the code is kind of messy right now anyway, with heavy use of printf() for debug).

[EDIT] Alright, so based on your answers I'm failing at debugging my code, so I guess I'll post the whole function so you can tell me more about what I'm doing wrong.

(You can ignore the printf()'s since most of that is debug that isn't even working)

 Booking **bookingSelectPaid(Booking **booking)  {
     Booking **ret = malloc(sizeof(Booking*));
     printf("Initial address of ret = %p\n", ret);
     size_t i = 0;
     int numOfPaid = 0;
     while (booking[i] != NULL)
     {
         if (booking[i]->paid == 1)
         {
             printf("Paying customer! sizeof(Booking*) = %d\n", (int)sizeof(Booking*));
             ++numOfPaid;
             size_t newsize = sizeof(Booking*) * (numOfPaid + 1);
             printf("Newsize = %d\n", (int)newsize);
             Booking **temp = realloc(NULL, (size_t)newsize);
             if (temp != NULL)
                 printf("Expansion success! => %p sizeof(new pointer) = %d ret = %p\n", temp, (int)sizeof(temp), ret);
             ret = realloc(ret, newsize);
             ret[i] = booking[i];
             ret[i+1] = NULL;
         }
         ++i;
         printf("Sizeof(ret) = %d numOfPaid = %d\n", (int)sizeof(ret), numOfPaid);
     }
     return ret; }

[EDIT2] --> http://pastebin.com/xjzUBmPg

[EDIT3] Just to be clear, the printf's, the temp pointer and things of that nature are debug, and not part of the intended functionality. The line that is puzzling me is either the one with realloc(ret, newsize); or ret[i] = booking[i]

Basically I know for sure that booking contains a table of structs that ends in NULL, and I'm trying to bring the ones that have a specific value set to 1 (paid) onto the new table, which is what my main() is trying to get from this function... So where am I going wrong?

PLB
  • 881
  • 7
  • 20
  • What exactly is the question here? (I don't see a question-mark anywhere...) – Oliver Charlesworth Jun 24 '12 at 16:45
  • I guess the question is: "What am I doing wrong?" Basically, I've allocated '**ptr = malloc(sizeof(my_structure*))' without knowing how many instances of the structure will be in my array, and then based on a loop, I'm incrementing a size variable and using that in 'ptr = realloc(ptr, newsize)' but I'm not able to place any more structs into the array... I can post the whole code even at the risk of failing my course if I'm being too ambiguous, haha. – user1478361 Jun 24 '12 at 16:47
  • @user1478361 : Next time, use 'code sample' for code instead of 'blockquote' (it's in the editing toolbar). I fixed it for you for now. – Tim Jun 24 '12 at 17:04

3 Answers3

2

I think the problem here is that your sizeof(ptr) only returns the size of the pointer, which will depend on your architecture (you say 4, so that would mean you're running a 32-bit system).

If you allocate memory dynamically, you have to keep track of its size yourself.

Gustav Larsson
  • 8,199
  • 3
  • 31
  • 51
  • I've updated my initial post to include the code from the problem area. (And since I don't understand how to bring the control characters with me onto stackoverflow, I posted it on pastebin as well) [EDIT] Hm... And now it's properly formatted all of a sudden -- oh well, you don't need to look at pastebin now I guess – user1478361 Jun 24 '12 at 17:01
  • It looks like your `i` could get bigger than `numOfPaid`, which means that `ret[i]` and `ret[i+1]` could run over. – Gustav Larsson Jun 24 '12 at 17:09
  • Even when the table provided by main only has 4 entries, including the NULL pointer? (I can post a screencap of the stdout print in the console if that would give more insight -- even though my debug is partly false) – user1478361 Jun 24 '12 at 17:15
  • Well.. I just tried modifying the malloc() directly to `4 * sizeof(Booking*)` and even then it's not printing more than the first entry, so clearly it's not going wrong where I thought it was... I guess that means I've mis-titled the whole question – user1478361 Jun 24 '12 at 17:23
  • so here's the function without debug crap in it: http://pastebin.com/SACkET8M and this is the main() calling it: http://pastebin.com/sfcBCTWT – user1478361 Jun 24 '12 at 17:27
  • The main() is provided by the assignment so that's correct... I was tasked with implementing the rest of the functionality myself, including the definition of the structure Actually.. considering I've probably already failed this course anyway, I'll just post the bulk of this assignment... I just want to know what I'm doing wrong so I can finally get some sleep. main: http://pastebin.com/TzabvU0V header file: http://pastebin.com/m1mAuXsc my implementations: http://pastebin.com/MFg0KyJi – user1478361 Jun 24 '12 at 17:37
  • As I said, you should replace `ret[i]` with something like `ret[numOfPaid-1]` and `ret[i+1]` with `ret[numOfPaid]`. Even better, pre-iterate bookings to figure out how much memory you need (also much faster). – Gustav Larsson Jun 24 '12 at 17:38
  • So obviously I've made at least one idiotic mistake somewhere (lack of understanding + sleep deprivation => the above), and right now I'd be incredibly relieved to at least understand this one thing. Apologies for making everyone debug bad code, but I've done all I can to the best of my understanding based on the materials available to me currently. =S – user1478361 Jun 24 '12 at 17:39
  • Oh, I see... Was the indexing the only thing really badly wrong? That already fixed it but I guess I'll try and do the pre-iteration as well... (I can't believe how long I've spent on this already) Thanks, though. – user1478361 Jun 24 '12 at 17:44
0

Because sizeof(ptr) returns the size of the pointer, not the allocated size

Dan
  • 1,466
  • 1
  • 13
  • 27
0

Yep, sizeof(ptr) is a constant. As the other answer says, depends on the architecture. On a 32 bit architecture it will be 4 and on a 64 bit architecture it will be 8. If you need more help with questions like that this homework help web site can be great for you.

Good luck.