473,386 Members | 1,602 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,386 software developers and data experts.

Compiler issue?

42
If a struct with the same name are defined in two different files,
something is wrong when building since the wrong struct definition is used when accessing via pointer.

See the following example:
-----------------------------------------------------------------
File A:
typedef struct
{
uint8 a;
uint16 b;
} MyStruct_t;

int FuncA(void)
{
MyStruct_t ObjA = {2, 3};
MyStruct_t *PtrA = &ObjA;

printf("Offset of element b in ObjA = %i\n", &ObjA - &ObjA.b);
printf("Offset of element b in PtrA = %i\n", PtrA - &PtrA->b);
}

--------------------------------
File B:

typedef struct
{
uint8 a;
uint32 b;
} MyStruct_t;

int FuncB(void)
{
...
}
----------------------------------
In the example FuncA will display, "Offset of element b in ObjA = 2" and "Offset of element b in PtrA = 4". The difference being due to element b being treated as a uint32 when accessed via a pointer changing the alignment in the structure.

Does anyone know what is causing this error? Is it a compiler issue? If so, is it a known issue?
I'm using Visual C++ Express 2005 compiler with service pack 1.

Thank you!
Nov 20 '08 #1
11 1666
gpraghuram
1,275 Expert 1GB
I think it should be 4 in both cases as (32 bit)compiler takes 4 byts alignment.

raghu
Nov 20 '08 #2
JosAH
11,448 Expert 8TB
What happens if you cast all addresses to char* first and then do the subtraction?

kind regards,

Jos

ps. The struct definition in file B isn't in scope at all.
Nov 20 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
There's no compiler issue.

Each implementation file is compiled separately. Therefore you can have a MyStruct_t in every file with completely definitions. However, when all these different definitions reach the linker who requires variable and function names to be unique, your build terminates with redefinition errors.
Nov 20 '08 #4
vmpstr
63
That code doesn't even quite compile for me.

Putting various brackets and casting to int reveals that the answer is -2 for both (for me). Are you sure that you are getting 2 and 4? I mean I would think that the answer would be negative?

Try using %p instead of %i in the printf statements.
Nov 20 '08 #5
MimiMi
42
Thank you all for your input!
Nov 21 '08 #6
JosAH
11,448 Expert 8TB
There's no compiler issue.
As a matter of fact there is, but it's another issue: when you do x-y for two
pointers x and y to different types (and different sizes!) it is an error. That's
why I asked to cast both pointers to a char*.

kind regards,

Jos
Nov 21 '08 #7
MimiMi
42
So, is the fault "mine", ie you're not supposed to write code that way, or is it Microsoft (Visual C++ Express 2005 linker)?
Nov 21 '08 #8
JosAH
11,448 Expert 8TB
So, is the fault "mine", ie you're not supposed to write code that way, or is it Microsoft (Visual C++ Express 2005 linker)?
I'd say: both; pointer arithmetic (x-y) doesn't yield a result when x and y are pointers
to different types but your compiler should've warned you about it. Your linker
is innocent in this particular case. ;-)

kind regards,

Jos

edit: this is what the C99 Standard has to say about it:

79)Another way to approach pointer arithmetic is first to
convert the pointer(s) to character pointer(s): In this
scheme the integer expression added to or subtracted from
the converted pointer is first multiplied by the size of
the object originally pointed to, and the resulting
pointer is converted back to the original type. For
pointer subtraction, the result of the difference between
the character pointers is similarly divided by the size
of the object originally pointed to.
Nov 21 '08 #9
MimiMi
42
I should have added that if PtrA->b is read in FuncA the 4 bytes beyond the end of the structure are accessed rather than the expected value; 3. ObjA.b does return the correct value...

Does anyone know why?
Dec 10 '08 #10
MimiMi
42
I have now found the answer to my question! The issues I had are well explained here:
Working with Packing Structures

I didn't know about this before.. Well, I knew data alignment and stuff, what I mean is that I didn't know about the compiler ways to "specify" it. It's always nice to learn new stuff!
Cheers!
Dec 11 '08 #11
donbock
2,426 Expert 2GB
@MimiMi
#pragmas are not portable. You should consider whether there's a way to accomplish your goals without tying your source code to a particular compiler implementation. If you do rely on nonportable behavior, at least document your assumptions carefully and clearly identify at least one compiler that is acceptable.
Dec 11 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dbrown2 | last post by:
I'm trying to understand and document how to install pypar on Win2k. Pypar needs to be installed with some MPI library code and also needs libpython23.a and other files which are not included with...
8
by: jon morgan | last post by:
OK, I'm going to be brave. There is a bug in VS.Net 1.1 that causes random compiler errors. I have raised this issue in posts at least three time in the past couple of months without attracting...
7
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
6
by: David Lack | last post by:
Hi, I recently installed a 60-day trial of .NET 2003 on my development system. I made tests with previous personal projects (which compiled ok with VC6) and some open source files, and keep...
19
by: Jerry | last post by:
I managed to narrow this down to a very simple expression. try this: private void Bug() { bool b = false; Test(3, (b || b) && b && !b); } private void Works() {
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
7
by: ndessai | last post by:
Hi All, I discovered following issue with the VC 7.1 compiler regarding the backword compatibility. I created a dll (Redirect.dll) which exposes a function and simply writes some text (say...
16
by: cyber citizen | last post by:
Hi Folks, We are encountering the following code issue on compiler susch as "xlc","gcc" but "icc" passes it successfully. Sample code: int main(void) { typedef unsigned char oratext;...
159
by: bernard | last post by:
howdy! please recommend a good c compiler. - should be small - should be fast - should come with a good ide - should be inexpensive i am using windows os.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.