![]() |
|
address of local variable returned warning |
Ricardo Santos
Member #6,609
November 2005
![]() |
Hi there! [Warning] address of local variable `coords' returned I'm having this warn from compiler. I need to have a function returning 2 values, so (as a newbie) i did this:
So then i could set a variable to *coords and *(coords+1). Is it bad idea? Thanks! |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
coords is a reference (or is it a pointer?) put in the stack and is removed when the function returns. Make it an array and return the adress or put it in a a struct and return normally. The struct is prolly easier as you dont have to free it afterwards. EDIT: oh and you could also add (oslt)
|
Tobias Dammers
Member #2,604
August 2002
![]() |
Quote: int &coords[2] Or else just: Or, to make absolutely sure the array you pass has at least 2 members:
--- |
ReyBrujo
Moderator
January 2001
![]() |
Or return a int pointer with the address of your parameter, like Or just declare the array as static:
-- |
Ricardo Santos
Member #6,609
November 2005
![]() |
Wow, so many ways! Ok, thanks guys |
Paul Pridham
Member #250
April 2000
![]() |
Here's another one.
---- |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
WHAT??? coords is a local stored on the stack, when the function returns a hardware interrupt could blow it away before you use it. It needs to be static at least. They all watch too much MSNBC... they get ideas. |
Fladimir da Gorf
Member #1,565
October 2001
![]() |
The returned value is a copy in this case. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori) |
Tobias Dammers
Member #2,604
August 2002
![]() |
Exactly. You return by value, just like you'd return an int. An "interrupt" that happens while in the function isn't a problem anyway, because the intr code restores the stack after execution, and jumps back to the exact same place in your function. Your code won't even notice. And for the fun of it, here's another solution (C++ only):
--- |
Felipe Maia
Member #6,190
September 2005
![]() |
Tobias Dammers
Member #2,604
August 2002
![]() |
Cleanest way, imo, is this:
This way, you pass one pointer instead of 2. The speed difference is absolutely negligible, but since it comes for free, and renders cleaner code, might as well use it. But what Felipe said is good too. --- |
Milan Mimica
Member #3,877
September 2003
![]() |
Yet another
-- |
Tobias Dammers
Member #2,604
August 2002
![]() |
New, but not better, nor safer. You'll be leaking memory all over before you know what hit you... --- |
Milan Mimica
Member #3,877
September 2003
![]() |
You'll be making segmention faults if you use C/C++ before you know what hit you... Use VisualBasic if you're unable to write good C/C++ code.
-- |
Tobias Dammers
Member #2,604
August 2002
![]() |
Nah, he's not accessing unallocated memory, he's allocating memory without freeing it. Leak, not segv. And may I suggest BASIC? --- |
Milan Mimica
Member #3,877
September 2003
![]() |
I know. I'm saying that to write good C/C++ you need to follow some rules. Otherwise you'll end up with a leaking and segfaulting code. Memory leaks and segfaults are the consequences of the same cause - lack of coding discipline. You can't say don't use dynamic memory allocation because your memory will leak. But you can say not to use it if not necessary, like here
-- |
Michael Faerber
Member #4,800
July 2004
![]() |
There have been written 11 (!) solutions for this problem! -- |
Paul Pridham
Member #250
April 2000
![]() |
Here's one in Forth: : get_gif_coords dup 30 / swap 30 mod TILE_H over * + 1+ swap TILE_W over * + 1+ ;
---- |
Tobias Dammers
Member #2,604
August 2002
![]() |
Quote: There have been written 11 (!) solutions for this problem! Hah, and we're only just getting started!
Now if this isn't bloated... --- |
Paul Pridham
Member #250
April 2000
![]() |
What, no STL? ---- |
Tobias Dammers
Member #2,604
August 2002
![]() |
You asked for it:
--- |
Carrus85
Member #2,633
August 2002
![]() |
Another STL (probably less overhead than vector) #include <utility> std::pair<int,int> get_coord(int num) { return std::pair<int,int>(num/30, num%30); } Boost.org Tuple #include "boost/tuple/tuple.hpp" boost::tuples::tuple<int, int> get_coord ( int num ) { return boost::tuples::make_tuple(num/30, num%30); } Yet another possibility (assuming int is exactly half the size of a long, bitwise). This should work, but it would definately be a bad idea, portability-wize. unsigned long get_coord ( int num ) { unsigned long ret = num%30; ret = ret << 32; ret = ret | (num/30); return ret; } These are all untested, but just thought I'd put up some more options...
|
Jonatan Hedborg
Member #4,886
July 2004
![]() |
I always thought that a on most 32 bit systems, an int is the same as a signed long.
|
|