![]() |
Adjacent data members
I have a class Vector. The first members I have typed into the class:
Scalar x; Scalar y; Scalar z; Scalar is a typedef for double. There are several other members (data and code) I examined this class at runtime and discovered that the doubles are not packed tightly in memory, ie: &x+sizeof(Scalar) != &y. This expression evaluates to true. Is there a way I can ensure they are adjacent in memory? I want to be able to use the data in calls to OpenGL array routines, but would rather avoid converting each Vector to a struct PackedVert3{Scalar x,y,x;} |
I'm confused, you want x, y, and z which are just plain doubles to be adjacent, but are not using arrays to store them?
|
The compiler packs the members as it deems best. One can force the issue at the risk of blowing portability.
|
If you have other data members after the XYZ you wont be able to pass them as an array anyway, unless it takes in a stride value in which case the fact that the doubles arent closely packed shouldnt matter.
EDIT: scrub that of course it would still matter, try putting your XYZ at the start of the structure ten they shouldnt need any packing. |
I see I'm not the only one confused. Why do you want them to be adjacent, why do you care. Thing is, they most likely are adjacent, you just have'nt realised it.
As DaWei said, the compiler will pack your data as it see fit. Lets look at an example ( I compiled this with Dev-C++): :
#include <iostream>:
0x22ff70You cant assume that data you define in your source is sequential. Also your code is wrong to begin with. :
&x+sizeof(Scalar) != &yYou have to be carefull when doing arithmetic with pointers or references. Lets look at an example: :
int *ptr = 0;:
0Similarly *(p+2) is element at address 8, the same as p[2]. Its the same with references. This is how your code should be: :
&x+1 != &ySince sizeof(Scalar) is 8, &x + 8 is the eight element of type Scalar after &x. The same as saying address of x pluss 8*8 wich is pluss 40. |
Quote:
The test: :
#include <stdio.h>shows different. Good catch on the &x+sizeof(Scalar) != &y thing though. Quote:
|
I'm a retard!
I just realised that before seeing your post, about the whole only adding one thing. I was about to make a post saying that. :-) Anway, the were packed in the first place :-). void glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer); Does take a stride size between triplets, but still needs each triplet packed. Thanks everybody! - I refer you to my signatures quote. |
You were right about the stack thing Animatronic.
Crap. I though the compiler would arrange data as It saw fit..I mean, lets say you interweave global chars and ints. This would make many ints not on a four byte boundary. I tried an example. The compiler would only padd the data, no rearranging. Not exactly space efficient. |
Okay, I wrote this reply before seeing you'd solved the problem, but I'll post it anyways in case you or someone else hits a similar issue in the future.
Quote:
|
| All times are GMT -5. The time now is 8:06 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC