Sign in to follow this  

Multiple C++ questions

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a whole load of general programming and C++ questions.
1.) Is floating point addition as fast as integer addition on modern processors? It seems to me that it is so.
2.) Is floating point multiplication faster than floating point division on modern processors?
3.) Is it possible to make a reference to a byte of an integer? I mean, for example having a 32-bit integer of type Uint32, and then making 4 references of type Uint8&, each of them to a byte of the 4-byte integer. I know it's possible to make pointers to bytes of the integer, but I'd like to know if references can do this too. (I could use this to have some automatical multiplications with 256 and 65536 in a color class.) PS Uint32 and Uint8 are the datatypes provided by SDL, they mean unsigned 32-bit int and unsigned char.
4.) When I have the following header file:
#ifndef header_h
#define header_h
int foo(int x) {return x * x;}
#endif
and I include it in multiple .cpp files, then I get a linker error about multiple definition of the function, even if there's the #ifndef structure! The only way is to put the definition of this function in a .cpp file instead of in the header file. However, if I want to make the function inline, then its definition has to be in the header file:
#ifndef header_h
#define header_h
inline int foo(int x) {return x * x;}
#endif
I'm just really confused by this. Why can't not-inline functions be defined in the header file, and why can inline functions only be defined in the header file? This doesn't make a lot of sense to me, so what's the reasoning behind this?
Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:

4.) When I have the following header file:

#ifndef header_h
#define header_h
int foo(int x) {return x * x;}
#endif



and I include it in multiple .cpp files, then I get a linker error about multiple definition of the function, even if there's the #ifndef structure! The only way is to put the definition of this function in a .cpp file instead of in the header file.

However, if I want to make the function inline, then its definition has to be in the header file:

#ifndef header_h
#define header_h
inline int foo(int x) {return x * x;}
#endif



I'm just really confused by this. Why can't not-inline functions be defined in the header file, and why can inline functions only be defined in the header file? This doesn't make a lot of sense to me, so what's the reasoning behind this?


The include guard on header files only works on a per source file basis.

Say you have a project with 2 source files in it. The compiler compiles them separately, it doesn't know about the presence of other source files while its compiling one. It only uses the header files to work out how to interface with the code in other source files.

The inline directive forces the implementation into a header because the compiler must make the substitution during compilation, and as I have mentioned, it won't look at any other source files during compilation, so it cannot find the code associated with the inline if you have it in a seperate source file.

Share this post


Link to post
Share on other sites
I think the answers to remaining questions are (1) can be, (2) yes and (3) sort of. Specs of the exact cycle counts of Intel CPU instructions seem to be pretty hard to come by, but apparently Athlon's can do an fadd or fmul every cycle provided they are properly scheduled (http://cr.yp.to/hardware/x86.html). I'm pretty sure division operations are slower.

As for (3) yes, you can get references in exactly the same way as pointers, just like:
char& firstByteRef = ((char*)&intValue)[0];
But of course that comes with all the old warnings about non-portability (and possibly confusing the compiler's alias analysis).

Share this post


Link to post
Share on other sites
Quote:

1.) Is floating point addition as fast as integer addition on modern processors? It seems to me that it is so.

Not quite, but for all intents and purposes it is. Casting floats to integers, or vice versa, is still very slow though, so definitely minimize that.

Quote:
2.) Is floating point multiplication faster than floating point division on modern processors?

Yes, although there are special SSE2 instructions for improving division speed. But if you're ever dividing more than one variable by the same number, always calculate the reciprocal (1 / number) and multiply all the variables by that.

Quote:
3.) Is it possible to make a reference to a byte of an integer?

Yes, ZQJ gave the correct answer.

Quote:


#ifndef header_h
#define header_h
int foo(int x) {return x * x;}
#endif


and I include it in multiple .cpp files, then I get a linker error about multiple definition of the function, even if there's the #ifndef structure! The only way is to put the definition of this function in a .cpp file instead of in the header file.

The only prevents it from being included twice from the same .cpp file. For instance, if you include header.h and you also include stuff.h which itself includes header.h, the #ifndef statement prevents it from being included twice, which would make errors.

If you had only put the function declaration in the header file, between the #ifndef stuff:

int foo ( int x );

and defined it in a .cpp file, there would be no problems. But when you place the definition ("{return x*x;}") in the header file, there ends up being multiple definitions for the function -- one for each .cpp file. That causes problems for the linker.

Inline functions are the exception because they're not really functions; they're compiled as if their contents were pasted into the calling code. Yes it's confusing and inconsistent; C++ is not a perfect language.

Share this post


Link to post
Share on other sites

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this