Advertisement

structs in structs in structs in structs in structs in ...

Started by June 13, 2000 10:04 PM
3 comments, last by BriarLoDeran 24 years, 6 months ago
For demonstrational purposes, let's say we have the following struct:
            
struct Happy
{
  int X;
  int Y;
  int Z;
  RECT AppleSauce;
}

 
Now let's say we define a PowerKeg as follows:

struct PowerKeg
{
  Happy DeathlyIll;
  int count;
}

// and then:

PowerKeg Zippy(100) // <- This is an array, but I can't user brackets because it thinks it's html coding...
    
Now that we have the formality out of the way, here is the real meat of the question. Let's say that I have some nice while loop...no, let's make that a for loop. So as we are transversing the loop as they say, we are doing something as follows:

for(int i = 0; i < 100 ; i++)
{
  Zippy .DeathlyIll.AppleSource.left = 0;
  Zippy .DeathlyIll.AppleSource.right = 1;
  Zippy .DeathlyIll.AppleSource.top = 2;
  Zippy .DeathlyIll.AppleSource.bottom = 3;
};
    
This would of course be pointless to do, but it seems to me that it would (should?) be possible to make this a little pretty (and assumedly faster) by doing something like the following:

RECT *SuperTemp = NULL; 
for(int i = 0; i < 100 ; i++)
{
  SuperTemp = &Zippy(i).DeathlyIll.AppleSource;
  *SuperTemp.left = 0;
  *SuperTemp.right = 1;
  *SuperTemp.top = 2;
  *SuperTemp.bottom = 3;
}
  
Well, something like that... Anyway, any answers or comments would be...well, most helpful ... Thanks, -> Briar LoDeran <- Edited by - BriarLoDeran on 6/14/00 11:33:09 PM
In this part:

RECT *SuperTemp = NULL;
for(int i = 0; i < 100; i++){
SuperTemp = &Zippy.DeathlyIll.AppleSource;
*SuperTemp.left = 0;
*SuperTemp.right = 1;
*SuperTemp.top = 2;
*SuperTemp.bottom = 3;
}

don''t you need this:

RECT *SuperTemp = NULL;
for (int i = 0; i < 100; i++) {
SuperTemp = &Zippy.DeathlyIll.AppleSource;<br> *SuperTemp->left = 0;<br> *SuperTemp->right = 1;<br> *SuperTemp->top = 2;<br> *SuperTemp->bottom = 3;<br>}<br><br>???<br><br>–dron1 </i>
Advertisement
Yes, you can do that, and if it helps readability, go for it.

It won''t really be any faster though. In either case, the CPU will access the data by way of offsets from a base pointer (in the first case, its hidden in the C array, but its there). You''re only making the offsets smaller when you make a direct pointer to your data. Anyways, on 386+ processors, indexed addressing is free, so there''s no penalty in any case.


- Remnant
- (Steve Schmitt)
- Remnant- (Steve Schmitt)
So, if I understand this right, aside from the readability of the code, there is no speed wise reason to do this, right? It seems to me that this MUST save you something, even if it is a mere processor operation. Well, thank Remnant!

-> Briar LoDeran <-
Nup, saves nothing. You could however go:

struct Happy
{
int X;
int Y;
int Z;
RECT *AppleSauce;
}

RECT SuperTemp;
SuperTemp.left = 0;
SuperTemp.right = 1;
SuperTemp.top = 2;
SuperTemp.bottom = 3;

for(int i = 0; i < 100 ; i++)
{
Zippy(i).DeathlyIll.AppleSource = &SuperTemp
}

Which would be you''d have to go and manually set each one to null when you lose the SuperTemp from the context, but until then, you can change them all by changing super temp.

Also quicker; since you only make 100 + 4 assignments, rather than your example, where you make: 100*(5) = 500 assignments...

Probably not worth doing; since you probably want an indervidual structure for each point the array; but hey. Nevermind.

You can always use the dreaded malloc to create a new Rect if you have to change the points, in which case it ends up being slower anyway... O well. *shrugs* Worth a thought.

This topic is closed to new replies.

Advertisement