pass a pointer-to-pointer, or return a pointer?


 
Thread Tools Search this Thread
Top Forums Programming pass a pointer-to-pointer, or return a pointer?
# 1  
Old 01-06-2009
pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
Code:
int my_malloc(int size, char **pmem)
{
      *pmem=(char *)malloc(size);
      if(*pmem==NULL) {
             return NOK;
      }
      return OK;
}
int main()
{
       char *my_pmem;
       if(my_malloc(64, &my_pmem)==OK){
        ...
       } else {
       ...
       }
}

(2)the other is to return a pointer, like:
Code:
void * my_malloc(int size)
{
       void * pmem;
       pmem=malloc(size);
       return pmem;
}
int main()
{
      char *my_pmem;
      my_pmem=(char *)my_malloc(64);
      if(my_pmem==NULL) {
           ...
      } else {
           ...
      }
}

Could anyone tell the difference(advantages and disadvantages )about those two methods ?

Thanks in advance !

Last edited by Franklin52; 01-06-2009 at 07:17 AM.. Reason: adding code tags
# 2  
Old 01-06-2009
Why are you using the function in this scenario? By using function calls you are creating the overhead of function call and temporaray object.
# 3  
Old 01-06-2009
Quote:
Originally Posted by steephen
Why are you using the function in this scenario? By using function calls you are creating the overhead of function call and temporaray object.
I just want to know the advantages and disadvantages of those two methods by this simple example... actually, in practice, especially in memory management application for a special or embedded system, it is necessary to implement our own function to malloc a block of memory. Then the case raises.
# 4  
Old 01-06-2009
I prefer the second form because:
  1. The interface is simpler: one parameter in, the answer returned. You just check whether the pointer is null to verify success. The first form has some kind of other return value to check, which could be boolean, but I cannot tell, so I would have to look up the definitions of OK and NOK. And it is unclear from the interface whether I also need to check the pointer value, or indeed whether the pointer is NULL on failure. Looking into you implementation I can see that OK is returned if and only if the pointer is valid and the pointer is NULL on failure, but then I can write
    Code:
    char *my_pmem;
    my_malloc(64, &my_pmem);
    if(my_pmem){
      ...
    }

    so the return value is redundant -- it gives me no extra information.
    However, if you want the function to indicate more than just a simple fail/succeed (e.g. different failure modes) then the first way is the only way to do it.
  2. It mimics the standard malloc(3) function -- or it would do if the parameter was of type size_t rather than int -- and therefore has the benefit of familiarity, and makes it easier to port code written with malloc to use my_malloc.
# 5  
Old 01-06-2009
The actual difference is that the first example copies the address of your pmem pointer to the stack and the second example doesn't. The second example only works because you're using malloc() which allocates memory from the heap, otherwise you could have unexpected behavior because you would be returning a local variable (which is cleared upon function completion).

IMHO the second option would be faster because there's one less parameter to be copied to the stack and there's no desreferencing of the pointer's address to obtain the data. Anyway, you should feel no significant speed difference in any of the alternatives.
# 6  
Old 01-06-2009
I can't see any effective difference aside from the extra complications of effectively returning two values in your first example. I'd tend to avoid that kind of redundancy in case my fumble fingers ever cause one to contradict the other.
# 7  
Old 01-06-2009
Quote:
Originally Posted by aaronwong
I just want to know the advantages and disadvantages of those two methods by this simple example... actually, in practice, especially in memory management application for a special or embedded system, it is necessary to implement our own function to malloc a block of memory. Then the case raises.
Like everyone else I too prefer the second method one over the first as it is simpler, easier to understand and uses much less storage in the stack segment. Also I don't see the need for a function call just to malloc a chunk of memory. For an embedded system where resources are scarce you are better off mallocing that block of memory in main.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segmentation fault when I pass a char pointer to a function in C.

I am passing a char* to the function "reverse" and when I execute it with gdb I get: Program received signal SIGSEGV, Segmentation fault. 0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72 72 *q = *p; Attached is the source code. I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies

2. Programming

Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines? I have talked to one of the curators of the forum, but I am still not quite clear. Here... (1 Reply)
Discussion started by: yifangt
1 Replies

3. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

5. Programming

matrix pointer

Can anyone tell me what the following statements do? float (*tab); tab=(float (*)) calloc(MAXCLASS, (MAXCLASS+1)*sizeof(float)); (3 Replies)
Discussion started by: littleboyblu
3 Replies

6. Programming

How to return void function pointer

Hello all im trying to build function that will return void function pointer what is mean is ( not working ) the main function void * myClass::getFunction(int type){ if(type==1) return &myClass::Test1; if(type==2) return &myClass::Test2; } void myClass::Test1(){... (1 Reply)
Discussion started by: umen
1 Replies

7. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

8. Programming

pointer problem

could any one tell why the following is showing segmentation fault while using **ptr but working fine using **a #include<stdio.h> ... (1 Reply)
Discussion started by: useless79
1 Replies

9. Programming

Regarding char Pointer

Hi, char *s="yamaha"; cout<<s<<endl; int *p; int i=10; p=&i; cout<<p<<endl; 1) For the 1st "cout" we will get "yamaha" as output. That is we are getting "content of the address" for cout<<s. 2) But for integer "cout<<p" we are getting the "address only". Please clarify how we are... (2 Replies)
Discussion started by: sweta
2 Replies

10. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
Login or Register to Ask a Question