Random (quick) C/++ question...

Joined
Jul 8, 2001
Messages
610
I have a function say,

Code:
int* dosomething()
{
	int number, *pointer ;
	pointer = &number ;

	return pointer ;

}

Which compiles in VC++ without any trouble. Now suppose I have this instead,

Code:
int* dosomething()
{
	int number ;

	return &number ;

}

Which gives me "warning C4172: returning address of local variable or temporary", but aren't I returning the address of a local variable in both cases? Does anybody have any idea what's so different about the second case that it warrents the warning from the compiler?

Thanks.
 
Joined
Mar 9, 2004
Messages
3,322
The warning should probably go for the above example too. It's telling you that after that function exits the local variable you are pointing at is garbage.

ps: whats up with that type preceding dereference operator? It really shouldnt compile at all like that. ;o)
 
Joined
Jul 8, 2001
Messages
610
Oh I see about the dereference operator, yeah I just typed this up it didn't come from an actual program.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
It doesn't compile for me because the function declaration is incorrect as LoS points out.

The second example does indeed return a pointer to temporary memory, and is a bug in your program. The compiler tries to help you when it can; in this case, you've gone beyond its ability to notice the problem.

"stuff like" the declaration you quote is fine. It's what you assign the pointer to that's the problem.
 
Joined
Jul 8, 2001
Messages
610
If the declaration is fine, then I don't think I understand what the problem is. What's the matter with what I assign the pointer to?
 

HHunt

Supreme [H]ardness
Joined
Apr 12, 2001
Messages
6,063
DarkSoldier said:
If the declaration is fine, then I don't think I understand what the problem is. What's the matter with what I assign the pointer to?

As he said: "The compiler tries to help you when it can; in this case, you've gone beyond its ability to notice the problem."
 

drizzt81

[H]F Junkie
Joined
Jan 21, 2004
Messages
12,361
DarkSoldier said:
If the declaration is fine, then I don't think I understand what the problem is. What's the matter with what I assign the pointer to?
The problem is that you are return a reference (pointer) to a local variable. This means that as soon as you leave the function dosomething() the pointer will be invalidated, i.e. it points to a memory location you are not allowed to use. There is no guarantee that two consecutive calls of dosomething() will return the same memory location .

A quick seach revealed this guide.
 
Joined
Apr 4, 2003
Messages
836
say you have created a pointer inside that function and use to to reference some dynamically-allocated memory. in that instance, it would be beneficial to return a pointer.

however, there can be nothing good coming from returning the address of a local var.

so it is an instance of the compiler permitting the first due to the fact that sometimes you do indeed want to return the value of a pointer, and the second being denied because you never want to return the address of the local var.
 

mikeblas

[H]ard|DCer of the Month - May 2006
Joined
Jun 26, 2004
Messages
12,776
DarkSoldier said:
If the declaration is fine, then I don't think I understand what the problem is. What's the matter with what I assign the pointer to?
Your function is returning a pointer. It should return a pointer that's usable by the caller of the function. Your function is returning a pointer ot memory that's no longer usable.

Your code isn't much different than this function, semantically:

Code:
int* Function()
{
   // valid pointer
   int* pInteger = malloc(sizeof(int));
   // release the memory
   free(pInteger);
   // return it anyhow
   return pInteger;
}

and you see the problem there, right?
 
Top