returning allocated space from function

This is a discussion on returning allocated space from function within the C Programming forums, part of the General Programming Boards category; Hello, this could be a stupid question, but I don't know the answer...a short example explains more than many words: ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    returning allocated space from function

    Hello, this could be a stupid question, but I don't know the answer...a short example explains more than many words:

    char* function(){
    char *str;

    str=(char *)MallocSafe(SIZE);
    return str;
    }

    void main(){
    char *buffer;

    buffer=function();
    }

    In this case, will the value of the pointer "buffer" be the same of "str"? In other words, will the memory allocated and pointed to by str be accessible again outside?

    I'd like to get a technical answer to understand the behavior of the compiler and linker in this case, thanks for help.

    BrownB

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Yes, both buffer and str would be pointing to the same memory location.

  3. #3
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    i'm o.k with you thantos

  4. #4
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    I'd like to get a technical answer to understand the behavior of the compiler and linker in this case, thanks for help.

    @==adresse ...
    str is @ of (*str) so your function return the adress not the caracter
    buffer receive (@*str) so buffer will have the adress of *str then buffer and str will point the same bloc
    Code:
    
    sorry about my english because i use a translator 
    

  5. #5
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >str=(char *)MallocSafe(SIZE);
    If you have to cast it, is it really that safe?

    >void main(){
    main has never been defined to return void, it returns int and nothing else. In other words, your definition should be (assuming you don't want main to take arguments):
    Code:
    int main(void)
    But because this is a definition and not a declaration, this is also acceptable:
    Code:
    int main()
    >In this case, will the value of the pointer "buffer" be the same of "str"?
    In this case, no. str and buffer are in mutually exclusive scopes, so by the time execution gets back to buffer, str has been destroyed. However, the memory that str pointed to still exists and is referenced by buffer, so a resource leak isn't caused by losing access to the memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 05:02 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21