473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

a strange problem with pointers

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 .
Nov 13 '05 #1
3 2044
> 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
Nov 13 '05 #2

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
Nov 13 '05 #3
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bg**********@news.freedom2surf.net>...

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 .
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Al Newton | last post by:
My class has this member function: vector<UniqueCustId*>* CMyClass::GetCustList( void ) { SYSTEMTIME st; string strServiceDate; UniqueCustId* uci = new UniqueCustId; string strCustNumberOld =...
5
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
4
by: dd | last post by:
Hello. My primary goal is to initialize array of pointers to structures like this: struct R aa={ {"asd",{"dsa","dda"},{"441","882"}}, {"ddd",{"aaa","666"},{"111","772"}},...
2
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
11
by: junw2000 | last post by:
The following code can be compiled. But When I run it, it causes "Segmentation fault". #include <iostream> int main(){ char **c1; *c1 = "HOW"; // LINE1
2
by: Andy Carlson | last post by:
I have a strange problem. I am trying to use a global variable, but I can't get the second routine to recognize changes to the globabl. I finally realize, that I was forking, so the global was...
23
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.