简体   繁体   中英

Return a pointer from the function

I was just looking for a correct syntax to return a pointer from the function and got surprised.

The search produced a lot of code like this:

int *A::foo()
{
    int b = ;
    int *c = &b;
    return c;
}

However, I believe that this code won't work as 'c' is a local variable and will be destroyed when it will go out of scope.

So, what is the proper way of returning the pointer from the function? The code like this:

void A::foo(int *ptr)
{
    ptr = 5 + ptr;
}

int main()
{
    int b = 5;
    A a;
    a.foo( b );
}

So where I am wrong?

EDIT1:

I just tried the code suggested by Aiman, and it works. Which means that simple (standard) type works.

However, I need to make following code work:

SQLWCHAR *CODBCConfigure::ConvertFromString(const wxString &str)
{
    SQLWCHAR *string = new SQLWCHAR[str.length() * sizeof( SQLWCHAR )];
    string = const_cast<SQLWCHAR *>( str.wc_str() );
    SQLWCHAR *ret = string;
    return ret;
}

Trying to execute this code I see that the ret pointer still points to the proper address but the value there becomes wrong.

EDIT2:

Here is my latest code:

SQLWCHAR *tmp = const_cast<SQLWCHAR *>( str.wc_str() );
SQLWCHAR *string = new SQLWCHAR[sizeof( tmp ) * sizeof( SQLWCHAR )];
memcpy( string, tmp, sizeof( tmp ) );
return string;

Unfortunately this code also fails. The string pointer does not contain proper data.

What am I doing wrong?

Try this,

void A::foo(int *ptr)
{
    *ptr = 5 + *ptr;
}

int main()
{
    int b = 5;
    A a;
    a.foo( &b );
}

Whenever you're collecting a pointer, make sure you pass the address and not the variable itself, The pointer demands an address and not a value, and you need to update the value which the pointer is referencing, you were just trying to increment the pointer itself.

If you insist on returning a pointer to something which will persist to exist even outside the function's scope, then dynamically allocate your number:

int *A::foo()
{
    int *b;
    b = new int(5);
    int *c = b;
    return c;
}

Put in mind that you will need to delete b yourself afterwards with delete pointer_variable .

If you don't care, then I suggest passing the value by reference:

//ampers and means ptr will be 'passed by reference'
void A::foo(int &ptr) //so changing ptr now changes the variable you passed itself
{
    ptr = 5 + ptr; // it isn't a pointer anymore, so better call it "ref"
}

Edit:

SQLWCHAR *CODBCConfigure::ConvertFromString(const wxString &str)
{
    SQLWCHAR *string = new SQLWCHAR[str.length() * sizeof( SQLWCHAR )];
    string = const_cast<SQLWCHAR *>( str.wc_str() );
    SQLWCHAR *ret = string;
    return ret;
}

1-Don't name your variables something like string because this can cause conflicts. eg: In your case, if you're including the <string> header and using namespace std; .

2-You're redefining string immediately after assigning it something the second time, which is pointless. Much worse, it will cause a memory leak because you've allocated that memory in the first line and didn't free it (anything allocated with new needs to be freed using `delete').

3-Never take a pointer of what a function returns because when you do, you're taking the pointer of a temporary variable that was created on the fly and will be freed the moment you leave that line. You should assign it to a permanent memory location allocated by new first, and since what you're copying is essentially an array, you will need to copy the whole thing. I am not familiar with SQLWCHAR or wstrings in general, but something like memcopy can help you. Try this:

SQLWCHAR *CODBCConfigure::ConvertFromString(const wxString &str)
{
    SQLWCHAR *ret = new SQLWCHAR[str.length() * sizeof( SQLWCHAR )];
    SQLWCHAR *tmp = str.wc_str();
    memcopy(ret, temp, sizeof(tmp)); //probably str.length()+1 is what you need        
    //SQLWCHAR *ret = string; this line is redundant
    return ret;
}

You overrides ptr of allocated memory by

SQLWCHAR *string = new SQLWCHAR[str.length() * sizeof( SQLWCHAR )];

then

string = const_cast<SQLWCHAR *>( str.wc_str() );

Use loop to copy characters from string or some different approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM