Array-like variable format

Nov 12, 2012 at 6:31pm
Hey everyone, is there any way to ensure that variables declared in this format:
1
2
3
4
int a;
int b;
int c;
int d;
would be accessible with a pointer like this:
(&a)[someint]
Last edited on Nov 12, 2012 at 6:33pm
Nov 12, 2012 at 6:44pm
1
2
3
4
5
6
7
struct fourInts
{
  int a;
  int b;
  int c;
  int d;
}
with suitable instructions to your compiler to remove padding.
Nov 12, 2012 at 6:48pm
I should've mentioned that the variables are in a class and I need them to be a direct member of the class. I'm just not sure how to be sure that the variables are next to each other in the memory.
Nov 12, 2012 at 6:56pm
1
2
3
4
5
6
7
class fourInts
{
  int a;
  int b;
  int c;
  int d;
};

with suitable instructions to your compiler to remove padding.

Nov 12, 2012 at 7:00pm
What exactly do you mean by "remove padding"?
Would this work even if multiple threads were creating a 'fourInts' at the same time?
Nov 12, 2012 at 7:16pm
When you create an instance of a struct or class, the member variables are all created next to each other in memory. However, the compiler will sometimes pad the member variables with gaps. You can insist that it does not do any padding, thus ensuring that the member variables are all next to each other. http://en.wikipedia.org/wiki/Data_structure_alignment#Data_structure_padding

If multiple threads made fourInts, each fourInt would be somewhere in memory. They would not be intermixed.
Nov 12, 2012 at 7:18pm
Ok, thank you.
Topic archived. No new replies allowed.