Skip to main content
gamedev.net
🔒 Locked

Curious function call semantics.

Started by tifkagh Jan 3, 2003 at 11:07 AM 10 replies 850+ views
Original Post
tifkagh
tifkagh
When I attempt the following call:
  
double test;
test = pTile->GetX;
  
the compiler spits it out. The error is of course that I should be calling the GetX function with brackets, i.e. pTile->GetX(). What interests me is that my Trace, effectively a call to printf using varargs and all of that, is accepted.
  
Trace ("X Coord: %.2f, Y Coord: %.2f, Style: %d",pTile->GetX , pTile->GetY, pTile->iStyle);
  
The output of this call is invariably: X Coord: 0.00, Y Coord: 0.00, Style: 1254784. I would be curious to know why this gets accepted by VC++ and why iStyle, a public int range 0-4 gets uniformly corrupted.
Pepe
Pepe
You are not executing the functions and printing their result.

Actually you are printing the address memory where the code of the functions resides !!!
tifkagh
tifkagh
Ok, I''m printing the address which is printed as zero because of the float formatting. Why is iStyle corrupted? It is a public integer.
tifkagh
tifkagh
Ok, I''m printing the address which is printed as zero because of the float formatting. Why is iStyle corrupted? It is a public integer.
Fruny
Fruny
Because printf-style functions don''t do argument checking. If %f needs 8 bytes (need to check that... I think %lf prints doubles and %f prints floats), it''ll read 8 bytes, regardless of how many you actually gave it.

Two %f will read 16 bytes, the following %d will read 4 more ... and with your two addresses and one int you''re only providing 4+4+4 = 12 bytes. Thus you seem to be printing garbage corresponding to no real parameter.

HTTP 500, retry 4


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
tifkagh
tifkagh
Cheers Fruny,
since we are on the topic of the meaning pointers in relation to classes/objects perhaps you could clear up another fuzzy area in my thinking.

If I declare a pointer:

MyObj* pExample

I''m essentially creating a 4 byte area to hold the address of a proposed, but not yet existing, object of type MyObj. This area will be set to all zeros commonly referred to as Null. (Y/N)?

Now, suppose it''s time to instantiate the object, which I do using:

pExample = new MyObj

A new block of memory is allocated by the compiler. The new block of memory will contain only the data members. All the functions, and statics for that matter, for a given class are stored once and once only, presumably at the start of the executable. (Y/N)?
Nazrix
Nazrix
quote:
Original post by tifkagh
Cheers Fruny,
since we are on the topic of the meaning pointers in relation to classes/objects perhaps you could clear up another fuzzy area in my thinking.

If I declare a pointer:

MyObj* pExample

I''m essentially creating a 4 byte area to hold the address of a proposed, but not yet existing, object of type MyObj. This area will be set to all zeros commonly referred to as Null. (Y/N)?

Now, suppose it''s time to instantiate the object, which I do using:

pExample = new MyObj

A new block of memory is allocated by the compiler. The new block of memory will contain only the data members. All the functions, and statics for that matter, for a given class are stored once and once only, presumably at the start of the executable. (Y/N)?



It won''t set a pointer to NULL automatically. You must do that yourself.

Yes, when you do:

pExample = new MyObj;

it will call the MyObj() constructor function and do whatever is there


A CRPG in development...

Need help? Well, go FAQ yourself.


"Just don''t look at the hole." -- Unspoken_Magi
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Fruny
Fruny
As Nazrix correctly points out, no pointers are not automatically zeroed, and yes, new will allocate memory and call the constructor, if any.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
tifkagh
tifkagh
quote:
Original post by Fruny
As Nazrix correctly points out, no pointers are not automatically zeroed, and yes, new will allocate memory and call the constructor, if any.



If the poineter is not automatically zeroed, what is in there when it is created? Just the contents that was there previously?

Nazrix
Nazrix
yeah basically it just points to some random memory location that is pretty useless to you


A CRPG in development...

Need help? Well, go FAQ yourself.


"Just don't look at the hole." -- Unspoken_Magi


HTTP 500, retry 3

[edited by - Nazrix on January 3, 2003 7:47:49 PM]
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Fruny
Fruny
As with any other uninitialized variable, a pointer variable contains garbage. It points to a random and probably invalid address.

HTTP 500, retry 1


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.