Jump to content
  • Advertisement
Sign in to follow this  
AnonymousTipster

Simple C++ pointer problem

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

Hi. I have the current test code:
void ReturnMe(char *value){
	value = (char*)malloc(10);
}
Which i'm calling like this:
char* stringPtr;stringPtr = 0;
	ReturnMe(stringPtr);
So what i'm trying to achieve is to return a pointer to a memory location without using the return value. The reason for this is that in my real problem, I need to return multiple pointers from a function. The problem is that 'stringPtr' is always 0 and although 'value' is allocated a valid memory address, it isn't returned to 'stringPtr'. I somehow feel convinced that this would work in C (i've been using GCC for several months now, and I've just moved back to 2005 Express C++). Hope you can help. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
You have to pass the pointer as a reference so it looks something like that:

void ReturnMe(char **value){
*value = (char*)malloc(10);
}

Btw, have you seen Zahlman's sig :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by AnonymousTipster
Hi.
I have the current test code:
void ReturnMe(char *value){
value = (char*)malloc(10);
}

Which i'm calling like this:
char* stringPtr;stringPtr = 0;
ReturnMe(stringPtr);


So what i'm trying to achieve is to return a pointer to a memory location without using the return value. The reason for this is that in my real problem, I need to return multiple pointers from a function.
The problem is that 'stringPtr' is always 0 and although 'value' is allocated a valid memory address, it isn't returned to 'stringPtr'.
I somehow feel convinced that this would work in C (i've been using GCC for several months now, and I've just moved back to 2005 Express C++).

Hope you can help.
Thanks.

Its working correctly. You are modifying a local temporary (the parameter value) to hold the result of the malloc call. This will NOT modify stringPtr. You have two possible solutions to this: 1. return the result, or 2. pass in a pointer or a reference to your destination.

Share this post


Link to post
Share on other sites
In C, baumep above has already given you an idiomatic and working solution. In C++, the idiomatic solution would be:

void ReturnMe(char* & value)
{
value = new char[10];
}


Of course, as subtly hinted at by baumep, in C++ the allocated memory would be used as a buffer, not as a string.

Share this post


Link to post
Share on other sites
Thanks for your help everyone, it seems I need to learn the difference between pointers and references.

Out of curiosity, would my original code work in C, GCC or otherwise, or am I being delusional?

Quote:
Original post by Zahlman's Signature
As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.

Fair enough, but pointers of any size are expanded to 4byte addresses, regardless of type aren't they?

Share this post


Link to post
Share on other sites
Quote:
Original post by AnonymousTipster
Out of curiosity, would my original code work in C, GCC or otherwise, or am I being delusional?


Your original code creates a memory leak in C, GCC or otherwise. And it does not modify the original pointer either.

Quote:
Fair enough, but pointers of any size are expanded to 4byte addresses, regardless of type aren't they?


You can (and should) pass strings by reference.

Share this post


Link to post
Share on other sites
Quote:
Original post by AnonymousTipster
Thanks for your help everyone, it seems I need to learn the difference between pointers and references.


References are the object, pointers store the address of the object (or at least, this is how it should be thought of).


#include <iostream>

void incrementUsingReference(int &value)
{
// Using a reference means we can use value as if it were the variable itself
value++;
}

void incrementUsingPointer(int *value)
{
// Dereferences pointer and increments the value stored at the address
(*value)++;
}

void incrementPointer(int *value)
{
// No effect - increments the local pointer to the next address
value++;
}

int main()
{
int test = 0;

incrementUsingReference(test);
std::cout << test << std::endl; // "1"

incrementUsingPointer(&test); // We have to take the address of test, too...
std::cout << test << std::endl; // "2"

// Since the function's broken it doesn't matter if we take the address
incrementUsingPointer(&test);
std::cout << test << std::endl; // "2"

// Demonstration:
incrementPointer(test); // Access exception! Invalid pointer used.
}



Quote:
Out of curiosity, would my original code work in C, GCC or otherwise, or am I being delusional?


No, it wouldn't. value has scope local to that function, because it's passed in as a parameter. When you modify it, you modify the local copy - and so no changes are made.

Quote:
Quote:
Original post by Zahlman's Signature
As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.

Fair enough, but pointers of any size are expanded to 4byte addresses, regardless of type aren't they?


Only on 32 bit platforms. Don't rely on it.

EDIT (2/1/07): Oops, transposed two function calls in my sample code - sorry!

[Edited by - TheUnbeliever on February 2, 2007 9:00:29 AM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!