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 .
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 .