Functions defined in header / cpp file behaves different

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Functions defined in header / cpp file behaves different
# 1  
Old 08-27-2010
Functions defined in header / cpp file behaves different

File: A.h

Code:
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?

Last edited by Scott; 08-27-2010 at 03:13 PM.. Reason: Added code tags
# 2  
Old 08-27-2010
This is not a good example of your problem, as the code cannot even compile(incomplete type errors). Even if it did, you never set the variable to anything hence cannot expect it to be any particular value. Could you post the relevant section of actual code?

Last edited by Corona688; 08-27-2010 at 01:56 PM..
# 3  
Old 08-27-2010
The sample code is a snippet. Assume the compilation issue is resolved (x.member_variable is changed to 'return X.member_variable? 0 : -1' - which I believe is not the point I am trying to stress here). The issue is if we are having a similar type functionality in our feature (show method when defined in header file we see that value of X.member_variable is non-zero) but if we remove and keep the same code in A.cpp the value of x.member_varaible is 0. Please note that the flow is same. However objdump notifies us that show() method is promoted as inline function when within header file
# 4  
Old 08-27-2010
Maybe you're actually including a different A.h than you think you are. That's what this smells like.

If you want to get to the bottom of this, make the smallest code you can that recreates your problem. Make a B.h that only contains the snippet above and a B.cpp that uses it and does nothing else. Does the problem still happen? What if you try using B.h from inside A.cpp (complex cpp, simple .h)? What about if you use A.h from inside B.cpp (simple cpp, complex .h)? What if you take A.h and cut it in 1/2,1/4,1/8 etc... (A1.h,A2.h include in B.cpp (1/2 complex .h file, simple .cpp file).
# 5  
Old 08-30-2010
Thanks for pointing that.

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Mkdir fails when defined and called with functions.

In the below script i found that the alias mkdir_s is getting invoked from function configure() i.e the alias is kicking in. #!/bin/bash -e shopt -s expand_aliases alias mkdir=mkdir_s mkdir_s(){ if ]; then return else /usr/bin/mkdir "$1" return fi } configure() { mkdir -p... (9 Replies)
Discussion started by: mohtashims
9 Replies

3. UNIX for Dummies Questions & Answers

Error in compiling .cpp file

I get this error, defaults.cpp: In member function ‘int Defaults::GetIntDefault(const std::string&)’: defaults.cpp:68: error: ‘atoi’ was not declared in this scope defaults.cpp: In member function ‘real_t Defaults::GetRealDefault(const std::string&)’: defaults.cpp:76: error: ‘atof’ was not... (1 Reply)
Discussion started by: bstephens
1 Replies

4. Shell Programming and Scripting

[solved] awk: placement of user-defined functions

Hi folks, is there any recommendation, especially from a point of performance, about where to place a user-defined function in awk, like in BEGIN{} or if it is only need once at the end in END{}? Or doesn't it matter at all since, awk is so clever and only interprets it once, wherever it is... (3 Replies)
Discussion started by: zaxxon
3 Replies

5. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

6. Shell Programming and Scripting

user-defined functions, "$@", $jobs and $ps wwaxu

Imagine a user-defined function. func() { /usr/pkg/bin/program long-string-of-switches-and-configs "$@" ;} I execute it once. Then background it. I execute another instance. Then bg it. func unique-user-input ^Z func unique-user-input ^Z First I view with ps ps wwaux ... (0 Replies)
Discussion started by: uiop44
0 Replies

7. Shell Programming and Scripting

want to find out a function name in a cpp file

I have an error in my logs as it shows some function name . 1. I dnt know where is the file.cpp located only i know the machine . 2. How to find out that the function name is loacated in which path and which file into that machine. Thanks . (1 Reply)
Discussion started by: madfox
1 Replies

8. Shell Programming and Scripting

reading a cpp file

I need to find all the methods in a cpp file ... using shell script Pls guide me regarding the grep criteria for searching methods I mean what are the patterns to be grepped in *.cpp which match methods Hope i have made myself clear Thanks and Regards -- Ultimatix (2 Replies)
Discussion started by: ultimatix
2 Replies

9. Shell Programming and Scripting

If file = .cpp then print?

I'm trying to develop a script that makes it so only .cpp programs can print. I'm doing it for my computer programming class because everyone keeps printing the executable instead of the source code and it's wasting a lot of paper. How can I accomplish this? Thanks for the help. :D (5 Replies)
Discussion started by: Irish_Cereal
5 Replies

10. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question