Function Returning Pointer


 
Thread Tools Search this Thread
Top Forums Programming Function Returning Pointer
# 1  
Old 03-22-2011
Function Returning Pointer

Hi guys.

how a functions such fdopen, ... can return pointer?
are these functions use static memory(variables)?
# 2  
Old 03-22-2011
No, they simply use malloc inside the function.
Just a quick example of how it could work (REALLY JUST AN EXAMPLE)

Code:
typedef struct {
    int fd;
    int mode;
     /* a lot of variables to describe the filestream */
}FILE;

FILE *fopen(char *path, char *mode) {
    FILE *ret;
    ret = (FILE *) malloc(sizeof(FILE));
    /* do some processing */
    return ret;
}


Accordingly I assume that fclose() will then make a free() call to free the memory
# 3  
Old 03-22-2011
but when when returning from a function, all memory returned to OS isn't it?
# 4  
Old 03-22-2011
Quote:
Originally Posted by majid.merkava
but when when returning from a function, all memory returned to OS isn't it?
No that will happen only when the program exits otherwise all memory allocated is in use by the process.
This User Gave Thanks to shamrock For This Post:
# 5  
Old 03-22-2011
Thanks I got it.
Another question is:
How about allocating memory with alloca function? because it allocates memory from stack frame.
# 6  
Old 03-22-2011
Quote:
Originally Posted by majid.merkava
Thanks I got it.
Another question is:
How about allocating memory with alloca function? because it allocates memory from stack frame.
This question about alloca can start a flame war...some are proponents of it (i am not) but why would you want to allocate memory from the stack which to begin with is small...and the fact that alloca is not portable. Better to leave it to malloc which gets it from the bigger sized data segment. In any case use your best judgement. If i were you i wont use alloca but that's just me.
This User Gave Thanks to shamrock For This Post:
# 7  
Old 03-22-2011
Quote:
Originally Posted by majid.merkava
How about allocating memory with alloca function? because it allocates memory from stack frame.
alloca memory's main advantage, and main problem, is that it's fairy gold -- it vanishes. When your function returns, anything it's allocated with alloca becomes invalid (and potentially overwritten with trash). This means you don't have to free it yourself -- but you can't return it, either. You can pass it into a function, but can't pass it out of the function that allocated it.

alloca is fairly portable in one sense -- its convenience and (in some cases) performance advantage are tempting enough that you'll find it almost anywhere an implementation of it's even possible. But it's not actually defined by standard, and not actually possible everywhere. The long-standing flamewar over it is whether it should be considered a de-facto standard. Pros are it's simplicity and performance advantages. Its cons are potential bugs from implementations that don't do what you expect, limited stack memory, potential nonexistence if you port your code anywhere, and it not doing what you'd expect if you're used to malloc().

I think the best practice for returning a pointer is to take a pointer in the first place. Just use the memory you were given, then give it back. Where the memory came from and why becomes someone else's problem Smilie

Last edited by Corona688; 03-22-2011 at 07:10 PM..
This User Gave Thanks to Corona688 For This Post:
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

Pure C function pointer on printing vowels twice

Have difficulty to understand this pure C code to only print vowels twice from input string. Questions are commented at the end of each place. #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <limits.h> /* *Demonstrate the use of dispatch tables */ /*Print a char... (11 Replies)
Discussion started by: yifangt
11 Replies

3. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

4. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

5. Programming

Function pointer to inline function ?

Hi. Problem: I have to parse the payload of a packet. The payload could be in Big Endian Format (network byte order) or little. That depends on a flag present in the header of the packet. Solution: A horrible solution could be to check for that flag everytime I have to read a field in the... (11 Replies)
Discussion started by: emitrax
11 Replies

6. Shell Programming and Scripting

Returning the name of function used

Hi All In my script, I can call on several functions. I have a logging function that is called by any of these functions. What I would like is some way of identifying which function I am using and pass this to the log function as some parameter. Is there some built in command or way of... (3 Replies)
Discussion started by: kingpin2502
3 Replies

7. Shell Programming and Scripting

returning from a function

Hi all, I am very new to BASH shell programming. I need to return an integer from a function to the caller function. I did this: but it keeps giving me wrong return: Can someone help me out here, please? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

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

9. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies

10. Programming

Problem with function which reutrns pointer to a value

i have a function: char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID) this returns a pointer to CountryName if cityId is given. to retrieve countryname i give: char *CountryName; CountryName = pcCityIdToCountryName(..................); but when i compile it is giving :... (5 Replies)
Discussion started by: jazz
5 Replies
Login or Register to Ask a Question