C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 30th, 2003, 10:51 AM
kavi_s kavi_s is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 24 kavi_s User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
realloc and pointers are driving me crazy!!

I have this code, and the problem is that every time I add a new element to my big list, i keep overwriting what I had before, and the tail never moves to the end of the list...........does anyone know what I am doing wrong?? Please help because I have looked at this thing too long, and I can't figure it out...it might be really simple.......thanks very much in advance...............


#include <stdlib.h>

typedef struct{
int num;
int val;
}blah;

void printBig(blah * start, blah * end, int size); //prints a list of blah variables
void printOne(blah T); //prints one blah varaible

int main()
{
blah * big=NULL; //head of list of blahs
blah * bigTail; //tail of list of blahs
blah * bigptr; //another pointer
int i=0;
int bigsize=0; //total size of big
int onesize=sizeof(blah); //size of one blah
blah temp;

while(i<3)
{
temp.num = i+10;
temp.val = i+10;
bigsize += onesize; //recalculate size of big
big = realloc(big, bigsize); //allocate that much space for the big

bigTail = big + bigsize - onesize; //make tail point to end of list - size of one
//blah
memcpy(bigTail, &temp, onesize); //copy this blah to list
bigTail += onesize; //adjust tail
i++;
}

printBig(big, bigTail, onesize); //print list
return 0;
}

void printBig(blah * start, blah * end, int size)
{
int i;
blah * head = start;
blah * tail = end;

printf("{ \n");
while(start!=end)
{
printOne(*start);
start+=size;
}

printf(" }\n");
}

void printOne(blah T)
{
int i;
printf("(%d, %d) ", T.num, T.val);
}

Reply With Quote
  #2  
Old May 30th, 2003, 01:06 PM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 272 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 6 Days 17 h 57 m 24 sec
Reputation Power: 17
The problem lies in your pointer math. In the following line, you are actually adding (bigsize * sizeof (blah)) and then subtracting (onesize * sizeof (blah)). The compiler knows that you are working with a pointer to a blah so it assumes that increments and decrements of that pointer are in sizeof (blah) units.

instead of
Code:
bigTail = big + bigsize - onesize; //make tail point to end of list - size of one 

try

Code:
bigtail = big + (bigsize/onesize) - 1; //make tail point to end of list - size of one

You will also need to make a similar change to the following line:

change
Code:
bigtail += onesize;  // adjust tail

to
Code:
bigtail++;

I almost forgot one. You don't need to pass the size into the printBig function. You can just change

Code:
start += size;

to

Code:
start++;

Reply With Quote
  #3  
Old May 30th, 2003, 02:53 PM
kavi_s kavi_s is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 24 kavi_s User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you soooo much

I don't know how to thank you enough.......Your explanation was wonderful! Hehehe, and I hate pointers to death.....thanks again, you have helped so much!

Kavi

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > realloc and pointers are driving me crazy!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap