Same struct, different sizes.

C

Control Freq

Hi,
Apologies if you have heard this before, But,
I have inherited a large project, and I have found a struct defined in
one header file, and another structure of the same name in another
header file.

Now, the two structs are *almost* the same, but one has a char array
larger than the other.

It looks like the two structs should be identical, and one header file
should have been used throughout.
But, the connection has been lost at some point in history.

I discovered it when my program crashed when a pointer to the struct
was used in a function - with the different struct definition. It
stomped on the return address.

Anyway, I am concerned that this might be a timebomb in other places
in the project.

Is there a way that the compiler (or linker) can warn me that there is
a structure size problem like this?

Looking forward to your response.

Regards
 
A

Active Volcano

Hi,
Apologies if you have heard this before, But,
I have inherited a large project, and I have found a struct defined in
one header file, and another structure of the same name in another
header file.

Now, the two structs are *almost* the same, but one has a char array
larger than the other.

It looks like the two structs should be identical, and one header file
should have been used throughout.
But, the connection has been lost at some point in history.

I discovered it when my program crashed when a pointer to the struct
was used in a function - with the different struct definition. It
stomped on the return address.

Anyway, I am concerned that this might be a timebomb in other places
in the project.

Is there a way that the compiler (or linker) can warn me that there is
a structure size problem like this?

Looking forward to your response.

Regards


You should check your program with the QAC tool.It can check the same
problems.
Or you can write a shell program for check your code!
 
B

Ben Pfaff

Control Freq said:
I have inherited a large project, and I have found a struct defined in
one header file, and another structure of the same name in another
header file. ....
Is there a way that the compiler (or linker) can warn me that there is
a structure size problem like this?

If the program is carefully designed, then it may be possible to
include all the header files into a single .c file. If you do
this, then the compiler will complain about the duplicate struct
definition.
 
C

Control Freq

Control Freq wrote:

[...]> I have inherited a large project, and I have found a struct defined in
one header file, and another structure of the same name in another
header file.
Now, the two structs are *almost* the same, but one has a char array
larger than the other.
It looks like the two structs should be identical, and one header file
should have been used throughout.
But, the connection has been lost at some point in history.
I discovered it when my program crashed when a pointer to the struct
was used in a function - with the different struct definition. It
stomped on the return address.

[...]

So your original code is something like

    // X1.h
    struct X { char s [10]; };

    // X2.h
    struct X { char s [99]; };

    // foo.h
    struct X;
    void foo( struct X* );

    // foo.c
    #include "foo.h"
    #include "X1.h"
    void foo( struct X* x ) { ... }

    // bar.h
    struct X;
    void bar( struct X* );

    // bar.c
    #include "bar.h"
    #include "X2.h"
    void bar( struct X* x ) { ... }

    // problem.c
    #include "foo.h"
    #include "bar"
    #include /* X1.h or X2.h?, or do foo and bar use different ones? */
    void problem( struct X* x ) { foo( x ); bar( x ); }

And you want to know what to #include in problem.c, or if calling foo
and bar with x isn't even legal? If so, let the compiler do the
searching for you by changing X1.h to define struct X1. Now when you
attempt to compile, you'll get errors on uses that need to be changed
from X to X1. Now you have two distinct types and will know whether
problem() is really such.

Hi, thanks for in-depth reply.
But, I know what is happening, and why.
But, I am concerned that this sort of thing is littered all over the
project.
There are hundreds of files in this legacy project.
I am hoping that the compiler can find the problems for me, without me
going through every file by hand.
I simply don't have time for that.


Regards
 
J

JosephKK

Control Freq wrote:

[...]> I have inherited a large project, and I have found a struct defined in
one header file, and another structure of the same name in another
header file.
Now, the two structs are *almost* the same, but one has a char array
larger than the other.
It looks like the two structs should be identical, and one header file
should have been used throughout.
But, the connection has been lost at some point in history.
I discovered it when my program crashed when a pointer to the struct
was used in a function - with the different struct definition. It
stomped on the return address.

[...]

So your original code is something like

    // X1.h
    struct X { char s [10]; };

    // X2.h
    struct X { char s [99]; };

    // foo.h
    struct X;
    void foo( struct X* );

    // foo.c
    #include "foo.h"
    #include "X1.h"
    void foo( struct X* x ) { ... }

    // bar.h
    struct X;
    void bar( struct X* );

    // bar.c
    #include "bar.h"
    #include "X2.h"
    void bar( struct X* x ) { ... }

    // problem.c
    #include "foo.h"
    #include "bar"
    #include /* X1.h or X2.h?, or do foo and bar use different ones? */
    void problem( struct X* x ) { foo( x ); bar( x ); }

And you want to know what to #include in problem.c, or if calling foo
and bar with x isn't even legal? If so, let the compiler do the
searching for you by changing X1.h to define struct X1. Now when you
attempt to compile, you'll get errors on uses that need to be changed
from X to X1. Now you have two distinct types and will know whether
problem() is really such.

Hi, thanks for in-depth reply.
But, I know what is happening, and why.
But, I am concerned that this sort of thing is littered all over the
project.
There are hundreds of files in this legacy project.
I am hoping that the compiler can find the problems for me, without me
going through every file by hand.
I simply don't have time for that.


Regards

Grep ought to help with that.

Then put it into a proper revision control system.
.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top