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.
 
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)
 
Oh I see about the dereference operator, yeah I just typed this up it didn't come from an actual program.
 
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.
 
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?
 
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."
 
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.
 
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.
 
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?
 
Back
Top