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, 05:54 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
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(" ]");
}

Reply With Quote
  #2  
Old May 30th, 2003, 06:18 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,143 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 54 m 32 sec
Reputation Power: 1974
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.

Reply With Quote
  #3  
Old May 30th, 2003, 10:04 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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

Reply With Quote
  #4  
Old June 1st, 2003, 01:14 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,143 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 54 m 32 sec
Reputation Power: 1974
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."

Reply With Quote
  #5  
Old June 1st, 2003, 03:04 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
Thanks

Thanks, dwise1_aol, for ur help....I need to make some major changes to my code :S

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > ONE more question about Pointer math --> 3dfxMM

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