Jump to content
  • Advertisement
Sign in to follow this  
Kalasjniekof

function returning pointer

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

just like the title says, is it possible to have a function return a pointer to a variable? I thought it would be something like (*int)getpointerfunc(int i) { return &i; } but my compiler returns an error when I try to compile. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement

int *getPointerFunc()
{
return &someInteger;
}


Just make sure someInteger isn't a parameter to the function or a local variable. Otherwise all hell will break loose.

Share this post


Link to post
Share on other sites
Quote:
Original post by hunta
*** Source Snippet Removed ***


That would be very bad, as you are returning a pointer to a variable on the stack. Never, ever do that. Once the function returns, that address is no longer valid.

Share this post


Link to post
Share on other sites
you can return a pointer to a variable from a function. your syntax is a little off in your example. it should be:

int* getpointerfunc( int i )
{
return &i;
}

however, this will not work the way you expect because the variable i is on the stack and will be destroyed when you leave the function thereby making the pointer useless. if you are going to be returning a pointer, you shouldn't be pointing to something that is on the stack. eg.

int* getpointfunc( int i )
{
int *newInt = new int;
*newInt = i;
return newInt;
}

that would allocate a new heap variable and assign the value of i to it.

[edit - whoops, missed that one Dave. Thanks for the catch]

[Edited by - Jbs on April 1, 2005 2:26:27 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Jbs
you can return a pointer to a variable from a function. your syntax is a little off in your example. it should be:

int* getpointerfunc( int i )
{
return &i;
}

however, this will not work the way you expect because the variable i is on the stack and will be destroyed when you leave the function thereby making the pointer useless. if you are going to be returning a pointer, you shouldn't be pointing to something that is on the stack. eg.

int* getpointfunc( int i )
{
int newInt = new int;
*newInt = i;
return newInt;
}

that would allocate a new heap variable and assign the value of i to it.


That should be:

int *newInt = new int; // note the added '*'

Share this post


Link to post
Share on other sites
so something like this would be correct?

[source language=cpp]
#include <cstdlib>
#include <iostream>

using namespace std;

int *intarray;

int *getint(int index)
{
int *newInt;
newInt = &intarray[index];
return newInt;
}


int main(int argc, char *argv[])
{
intarray = new int[10];

for(int i=0; i<10; i++)
intarray = i;

for(int i=0; i<10; i++)
cout << *getint(i) << endl;

system("PAUSE");
return EXIT_SUCCESS;
}




or would this still fail (it compiles fine though...)

Share this post


Link to post
Share on other sites
In that case you can just do this:


#include <cstdlib>
#include <iostream>

using namespace std;

int *intarray;

int *getint(int index)
{
return &intarray[index];
}




Assuming intarray is an array of integers that has already been allocated and has at least index elements in it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!