tags:

views:

98

answers:

2

File: A.h

class A
{
public:
    struct x X;
    int show()
    {
        x.member_variable ? 0: -1;
    }
};

Now if A.cpp is complied which includes A.h (which is actually in a huge project space) we see that x.member_variable value is not as expected. But if remove the show() method and place it in A.cpp the code behaves fine - meaning that x.member_variable value is correct.

How such a thing may happen - one thing we saw from objdump is that if the function is defined in A.h the the method is treated as inline function which otherwise is not if defined in A.cpp?

How the code can behave differently altogether?

+4  A: 

Did you forget the return?

int show()
{
return x.member_variable ? 0: -1;
}

Without it, the code is still legal but the value returned will be somewhat random. There may happen to be a correlation between the behavior and where you put the code.

Potatoswatter
Thanks for correcting - it is the function returns 0 (success) or -1 (failure) and based upon that we branch out. But how the behaviour is different.
Prakash
@Prakash: My code returns 0 or -1. Your original code returns a random value. That is how they are different.
Potatoswatter
A: 

OK we found out a way that the above issue could be possible code wise. Its the hierarchy of how we are linking our binary is the issue. Let me explain the situation - we have one header file that is conditionally compiled with #ifndef .. #define .. #endif kept in different directory. Now this header files gets archived into lib1.a lib2.a and so on. Hence when we link our library and if incase we required it from lib3.a during linking we need to make sure that it linked the first:

ldd .. lib1.a lib2.a lib3.a -- so the exact header does not gets linked properly. Note that all .a have some additional interfaces compiled and linked.

Its unfortunate is that the required header contains common declaration (defines same methods but are little bit different)

How can we resolve the issue? Including Namespace would mean a lot of revamp in our codebase? Is there a better way to do that?

What would be best design for such a code base - so that later onwards no developer can accidentally include these fatal signatures?

Please help

--Prakash

Prakash
SO does not work like a threaded discussion forum. Ask this as a new question.
Tyler McHenry
Please try to keep your password when you open an account here, or link SO to your Google/Yahoo/whatever account. For SO to function you should continue to log in as the question asker.
Potatoswatter