The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
ONE more question about Pointer math --> 3dfxMM
Discuss ONE more question about Pointer math --> 3dfxMM in the C Programming forum on Dev Shed. ONE more question about Pointer math --> 3dfxMM C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 30th, 2003, 05:54 PM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
ONE more question about Pointer math --> 3dfxMM
Sorry this is another post about my last question titled "realloc and pointers are driving me crazy"
3dfxMM, thanks for ur help the first time, can you help me again please?
This time, 'fool' has an array of 'blahs' and the program results in 3 copies of the last 'fool' generated...sorry about my variable names, I was just getting a little frustrated.....
The problem is that I cannot increment a pointer to fool properly because a 'fool' has as a memeber, an array that needs to be dynamically allocated. If i fix the size of blahAr as 2, then my code works perfectly...BUT my actual design is such that I have to make it dynamic. . Does anyone know how I can make it such that I can increment my pointer properly??
thanks in advance for any input...
Code:
#include <stdlib.h>
typedef struct{
int num;
int val;
}blah;
typedef struct{
int nonsense;
blah * blahAr;
}fool;
void printBig(fool * start, fool * end);
void printOne(fool T);
int main()
{
fool * big=NULL;
fool * bigTail;
fool * bigptr;
int i=0,j=0;
int bigsize=0;
int onesize;
blah temp;
fool foolish;
foolish.blahAr = malloc(sizeof(blah)*2);
onesize = sizeof(blah)*2 + sizeof(int);
while(i<3)
{
temp.num = i+15;
temp.val = i+15;
foolish.nonsense=i;
for(j=0; j<2; j++)
{
temp.num++;
temp.val++;
foolish.blahAr[j] = temp;
}
bigsize += onesize; //recalculate size of big
big = realloc(big, bigsize);
bigTail = big + bigsize/onesize - 1;
memcpy(bigTail, &foolish, onesize);
bigTail++; //adjust tail
i++;
printBig(big, bigTail); //print list
}
printBig(big, bigTail); //print list
return 0;
}
void printBig(fool * start, fool * end, int size)
{
int i;
fool * head = start;
fool * tail = end;
printf("{ \n");
while(start!=end)
{
printOne(*start);
start++;
}
printf(" }\n");
}
void printOne(fool T)
{
int i=0;
printf("[ ");
for(i=0;i<2;i++)
printf("(%d, %d) ", T.blahAr[i].num, T.blahAr[i].val);
printf(" ]");
}
|

May 30th, 2003, 06:18 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Pointers only come in one size, regardless of the size of what they are pointing at[*]. The pointer, blahAr, is contained within the struct, fool, and is of size sizeof(blah*) [ which == sizeof(void*), which == sizeof(char*), which == sizeof(double*), etc]. So the size of the struct, fool, will be the same regardless of what blahAr points to.
Remember, when you dynamically allocate space for blahAr[], that block of memory gets allocated out in the heap somewhere; it does not get allocated inside of fool.
Footnote:[*] Strictly speaking that is only true as long as you stick exclusively to the same kind of pointer; e.g. near pointers vs far pointers. In your case, you are sticking with near pointers, so the rule applies.
Last edited by dwise1_aol : May 30th, 2003 at 06:25 PM.
|

May 30th, 2003, 10:04 PM
|
 |
not a fan of fascism (n00b)
|
|
Join Date: Feb 2003
Location: ct
|
|
|
Re: ONE more question about Pointer math --> 3dfxMM
Quote: Originally posted by kavi_s
.sorry about my variable names, I was just getting a little frustrated..... |
heh, i know what u mean! LOL! i give my functions/variables all sorts of derogatory names, and often my comments will be littered with my outbursts of frustration 
|

June 1st, 2003, 01:14 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Re: Re: ONE more question about Pointer math --> 3dfxMM
Quote: Originally posted by infamous41md
heh, i know what u mean! LOL! i give my functions/variables all sorts of derogatory names, and often my comments will be littered with my outbursts of frustration |
As do many others. Some of it survives in error messages. Just yesterday, for the first time in Red Hat 7 I messed up on the tar command for creating an archive and failed to give it any files to place into the archive. The error message was something like "tar cowardly refuses to create an empty archive."
|

June 1st, 2003, 03:04 PM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Thanks
Thanks, dwise1_aol, for ur help....I need to make some major changes to my code :S
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|