Archived

This topic is now archived and is closed to further replies.

what happends if nothing is returned?

This topic is 5009 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

high, say i have a funtion, called int Blah(x); now, insidee Blah looks like this:

int Blah(x)
{

if(x == 5)
return 5;

if(x == 10)
return 10;

}

now, obviously not all the control paths return a value, and the compiler will warn me of this. but what will happen if i do this? int z = Blah(7); so what will happen? will the universe instantly implode? or will something magical happen? im guessing some sort of garbage will get put into it, but im not sure. so what does really happen, and if the answer is "garbage", what kind of garbage? what does this garbage look like? thanks for any help

Share this post


Link to post
Share on other sites
actually, I believe that most compilers will silently insert a ''return 0;'' at the end of the function in that case. If they don''t, then yes, you would get garbage.



(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
Umh, what´s eax register? :o
AFAIK, there are 4 registers: eax, ebx, ecx, and edx. I don''t know their true purposes, but you use them in Assembly to do calculations.

Share this post


Link to post
Share on other sites
Dragon88: Yea, I got zero as a return value when I did a little test app..


[edited by - Mkk on March 26, 2004 7:11:17 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
Umh, what´s eax register? :o


registers are small one, two or four byte peices of memory directly inside the processor. That makes them the fastest possible memory, and the most expensive. eax is one of these on an x86 chip that is used to store the return value in the C calling convention.

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
Dragon88: Yea, I got zero as a return value when I did a little test app..


[edited by - Mkk on March 26, 2004 7:11:17 PM]


Did you make sure to load eax with a bogus value before returning?



(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

Share this post


Link to post
Share on other sites
quote:
Original post by alnite
quote:
Original post by Mkk
Umh, what´s eax register? :o
AFAIK, there are 4 registers: eax, ebx, ecx, and edx. I don't know their true purposes, but you use them in Assembly to do calculations.


There are actually eight general purpose registers: eax, ebx, ecx, edx, esi, edi, ebp, and esp.

Edit: check the intel manual ia-32 architecture manual volume one, chapter 3 if you want to know what they are for.

[edited by - DukeAtreides076 on March 26, 2004 7:20:23 PM]

Share this post


Link to post
Share on other sites
Depends on what compiler you use. Really, eax is a general purpose register. Just doing something will probably get eax used. It''s really not important though.



(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

Share this post


Link to post
Share on other sites
I´m using MSVC++ 6.0, tested this inside my real project and returned 0 again so I guess it will be 0 always then.. yep yep

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
I´m using MSVC++ 6.0, tested this inside my real project and returned 0 again so I guess it will be 0 always then.. yep yep
I just got 0xcccccccc or -858993460 in decimal.

edit:
You are right. It's whatever in the eax register. I tried this:

#include <iostream>
using namespace std;

int bogus( int e )
{
if ( e == 5 )
return 5;

__asm
{
mov eax, 20;
}
}

int main()
{
cout << bogus(7) << endl;

return 0;
}
And it returns 20;

[edited by - alnite on March 26, 2004 7:46:01 PM]

Share this post


Link to post
Share on other sites
hmm... but what if i dont want 0 returned? i think id like garbage better then 0..... if you think about it, what if you had:



int Blah(x)
{
if(x == 5)
return 0;

if(x == 10)
return 10;
}



then did

int z = Blah(7);

so this would return 0 to z by default, BUT obviously something else returns 0 inside this function. in this small example it wouldnt matter, but for my purposes (my function is much more complicated then Blah) it could cause problems. does anyone see what im talking about? i threw in a return -1 at the end of the function just for shits and giggles

Share this post


Link to post
Share on other sites
Just because you get it once, doesn''t mean it''ll do it every time. I''d be surprised if C or C++ specified a default return value. In fact, my compiler gives me an error, not a warning.

It may always return 0 in debug builds, but may not in release builds. Do not rely on that behavior. If you''re writing code that doesn''t specify return values for all paths, stop that! If you''re using a library that does, switch libraries, because it wasn''t written well.

I like pie.

Share this post


Link to post
Share on other sites
graveyard filla: One way could be so that rather than returning any values, give variable as a reference to a function and then change it values.. Like this:


void Function(int x, int &reference)
{
if (x == 5)
reference = 0;

if (x == 10)
reference = 10;
}

int main()
{
int var = 4;

function(7, var);
// After function var will still be 4..

function(5, var);
// Now var will be 0.

return 1;
}




[edited by - Mkk on March 26, 2004 7:56:23 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
I´m using MSVC++ 6.0, tested this inside my real project and returned 0 again so I guess it will be 0 always then.. yep yep


Were you running in debug mode? I believe that MSVC++ resets variables and do some general cleaning in debug mode (that can also be a major sucker since you wonder why your program craps out whenever you try running it in release mode, blegh!)

-Luctus

Statisticly seen, most things happens to other people.
[Mail]

Share this post


Link to post
Share on other sites
quote:
Original post by Luctus

Were you running in debug mode?


As long as I know, yes. Lol, I´m kinda unfamiliar with the compiler.. :D

I also tried to run the programs .exe itself and still got the number zero..

Anyway, I think that VolkerG said it shortly and concisely.


[edited by - Mkk on March 26, 2004 8:10:27 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Mkk
As long as I know, yes. Lol, I´m kinda unfamiliar with the compiler.. :D
In the default layout, there''s a drop-down box at the top of the IDE window that has something like "Win32 - Debug" initially visible. Change it to the alternative , "Win32 - Release". Rebuild All. Note the difference and allow the knowledge to illume your soul.

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
hmm... but what if i dont want 0 returned? (...)


Relying on code like that wouldn''t be a very good idea and could lead to a lot of problems. You should (read: must, but with no implicit obligation ) always make sure all paths return a value you yourself provide. For instance, suppose you have a little function that returns how much health a player has when their health is critically low...

int DumbFunction(int playerHP)
{
if(playerHP < 100)
{
return playerHP;
}
}


...you can''t make reliable use of this function at all. Let''s say you want to know if the player''s health is critical...

if(DumbFunction(playerHP) < 100) { /* OMFG player r teh dying!11!!!!1!one */ } 


...and random garbage is returned, you might end up having a "valid" number returned and your "player is dying" code gets executed anyways.

As for debug and release, debug is a safer yet slightly bulkier way to compile code. I THINK it initializes variables to NULL (usually 0, though this isn''t 100% reliable) by default and places NULL when an expected value is missing, but I''m not fully certain. You get the idea though. Release, which is much cleaner and produces smaller executables, doesn''t do that. You''ll want your final release to be compiled in release mode. You should make your beta-testing in release mode too, since stuff that works in debug may not always work in release (especially when you do shady things, like ignoring warnings the compiler generates )

Share this post


Link to post
Share on other sites

This topic is 5009 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.

Guest
This topic is now closed to further replies.