-1

i have a function that dynamically allocates memory and keeps the address in its local pointer. I want that address in the caller function. Yes, i can do that using return.But is it possible to do that using the function's parameters ?

#include<bits/stdc++.h>
using namespace std;
void getAddress(int *ptr){
    int *temp=(int*)malloc(sizeof(int));
    ptr=temp;
    cout<<ptr<<endl;
} 
int main(){
    int *ptr=NULL;
    cout<<ptr<<endl;
    getAddress(ptr);
    cout<<ptr<<endl;
    return 0;
}
output : 
0
0x6fa010
0

Expected output :
0
0x6fa010
0x6fa010
  • 1
    There are a lot of different ways to do this and which one is best depends on the application. In this case, returning the pointer is clearly better. – David Schwartz May 03 '17 at 08:31
  • Side note: it's very unlikely you actually need to use `malloc`, so try not to. – StoryTeller - Unslander Monica May 03 '17 at 08:32
  • 1
    @DavidSchwartz - The duplicate paragraph clearly states "**This question already has an answer here:**". Which the accepted answer at the link very much is. – StoryTeller - Unslander Monica May 03 '17 at 08:33
  • 1
    @DavidSchwartz - It is a very good answer to this question. The OP asked (with slight paraphrasing by me) "can I change the argument using the functions parameter". The linked dup asked "Why aren't the changes in the parameter visible on the argument". The answer is the same in both cases. – StoryTeller - Unslander Monica May 03 '17 at 08:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143248/discussion-between-david-schwartz-and-storyteller). – David Schwartz May 03 '17 at 08:41
  • Let me be more explicit: Can you clarify your use case? There are a lot of ways to do this and explaining all of them would be quite tedious. If you explained your use case, we could explain the right one for your particular situation and you will get a more helpful answer. – David Schwartz May 03 '17 at 08:53
  • **Recommended reading: "[Why should I not #include ?](http://stackoverflow.com/q/31816095/560648)"** – Lightness Races in Orbit May 03 '17 at 09:10

2 Answers2

2

Stylistically it's best to return the allocated pointer if you allocate memory in the function body (as that's what the C standard library functions do). But passing it as a parameter is possible but you need an additional level of indirection:

void getAddress(int **ptr){

with

*ptr=temp;

in the function body, and

getAddress(&ptr);

at the call site. An alternative is to pass the pointer by reference:

void getAddress(int*& ptr){

which requires fewer changes perhaps at the expense of readability at the calling site.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Yes you can do it, via pass-by-reference:

void getAddress(int *&ptr){
//                   ~
    int *temp=(int*)malloc(sizeof(int));
    ptr=temp;
    cout<<ptr<<endl;
} 

OT: Isn't temp redundant?
OT2: Don't forget to free the pointer at the end of main().

songyuanyao
  • 169,198
  • 16
  • 310
  • 405