Simple C++ pointer problem

Started by
7 comments, last by TheUnbeliever 17 years, 2 months ago
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.
Advertisement
Moved to For Beginners.
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 :-)
baumep
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
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?
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.

Thanks :)
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]
[TheUnbeliever]

This topic is closed to new replies.

Advertisement