a strange problem with pointers

S

Sameh Halabi

Hi
I have a strange problem in C , I hope someone may help with it .
the problem is as follows :
I have an existing big structure (that I can't change) which
contains in it groups of variables that is repeated .
typedef struct big_struct
{
.....
.....
char a1[9];
char b1[8];
char c1[6];
char d1[8];
long e1;
char f1;
char g1[8];
char h1;
char i1;
char j1;
char k1;

char a2[9];
char b2[8];
char c2[6];
char d2[8];
long e2;
char f2;
char g2[8];
char h2;
char i2;
char j2;
char k2;
....
until char a20[9] ..........
} big_struct_t

I want to work with this structure with loops instead of working with
these groups of variables seperately . so , what I did was defining a
structure that matches the group of variables :

typedef struct strct{
char a[9];
char b[8];
char c[6];
char d[8];
long e;
char f;
char g[8];
char h;
char i;
char j;
char k;
}strct_t;

then I defined an array of pointers from the same type , then I let
each pointer point to the beginning of the group of variables casting
it to the right type in the following way :
ptr_arr[0] = (strct_t*)&bigStruct->a1;
ptr_arr[1] = (strct_t*)&bigStruct->a2;
....
....
and so on until
ptr_arr[19] = (strct_t*)&bigStruct->a20;


the strange thing is that when I did that from the second member in
the array I had a move of 1 byte in memory as you can see below :
here you can see the adress in memory where each variable sits .
ptr_arr[0] = (strct_t*)&bigStruct->a1; (this is OK )
bigStruct ptr_arr
a1 0x406b64d8 0x406b64d8 (a)
b1 0x406b64e1 0x406b64e1 (b)
c1 0x406b64e9 0x406b64e9 (c)
d1 0x406b64ef 0x406b64ef (d)
e1 0x406b64f8 0x406b64f8 (e)
f1 0x406b64fc 0x406b64fc (f)
.....


ptr_arr[1] = (strct_t*)&bigStruct->a2; (the problem begins here
.....)
bigStruct ptr_arr
a2 0x406b6509 0x406b6509 (a)
b2 0x406b6512 0x406b6512 (b)
c2 0x406b651a 0x406b651a (c)
d2 0x406b6520 0x406b6520 (d)
e2 0x406b6528 0x406b6529 (e)===> the problem
f2 0x406b652c 0x406b652d (f)
.....

I tried also to do it with :
memcpy(&ptr_arr[0],&bigStruc->a1,sizeof(strct_t));
but it didn't work also .
this causes a memory leak .
 
A

Allan Bruce

the strange thing is that when I did that from the second member in
the array I had a move of 1 byte in memory as you can see below :
here you can see the adress in memory where each variable sits .


I think your problem here is padding in the structure. c is allowed to pad
any bits in the structure (cant remember the exact definition). The
practicalities of this usually mean that each new type will be stored in the
next boundary (usually 4 or 8 Bytes). This can usually be changed with your
compiler options but your code will change to take this into account.

The main problem is that your big struct has sub-structs within but the
sub-struct's first and last members are chars. These will be aligned in
memory until the long comes along - then padding will come in.

Ways around this - the easiest way would be to make the big structs have the
long variable at the end but you say you cant change the big struct.
The only other way I can see this working is if you use two sub structs, the
first for the first chars of your sub-struct and the second for the long and
remaining chars, e.g.

typedef struct strct1{
char a[9];
char b[8];
char c[6];
char d[8];
} sub1_struct;

typedef struct strct2{
long e;
char f;
char g[8];
char h;
char i;
char j;
char k;
}sub2_struct;

Now if you assign your pointers, you should (hopefully) get the proper
alignment
HTH
Allan
 
A

Allan Bruce

Hi Al .
The bigstruct is defined well . the inner variables in my real
structure are not defined as char[8] for example . they are defined by
domains such as DOMAIN_1(var_a) , where DOMAIN_1 is defined to be of a
type char[8] . so there's no chance that there are differences in the
difinition of the variables .
Thanks .

Sameh, did you receive my post about making 2 smaller strcuts? Did this
help?
Allan
 
S

Sameh Halabi

Allan Bruce said:
Hi Al .
The bigstruct is defined well . the inner variables in my real
structure are not defined as char[8] for example . they are defined by
domains such as DOMAIN_1(var_a) , where DOMAIN_1 is defined to be of a
type char[8] . so there's no chance that there are differences in the
difinition of the variables .
Thanks .

Sameh, did you receive my post about making 2 smaller strcuts? Did this
help?
Allan

Good morning Allan
I tried your suggestion and it seems to be good . I'm still in the
phase of testing it , but it seems to be working . Thanks .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top