Thread: memory allocation for functions! :(

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    memory allocation for functions! :(

    we use pointers after allocating its memory size....
    for example...

    char *name=(char *)malloc(20);//allocationg memory

    free(name); //removing the pointer

    But what if I use a function that returns pointer of string???
    for example...

    char* return_name(char *name)
    {

    name=(char *)malloc(20);

    return name;
    ----------
    ----------------
    -----------------
    free(name); /*remving the memory but after the statement "return" this line will not be executed...so the pointer remains mortal and creates bugs when i recall the function */
    }

    what is to be done in that case???


    next is:

    char* return_name(char *name)

    Does the function allocates memory for the pointer that it returns???
    if "yes" then how??? we haven't allocated it and since we haven't done that there is no question about how to free the allocation. so it is containing bugs and creating problems in my programmes.

    Pls remember that I am a new commer
    RaHaTk

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    char* return_name(char *name)
    {

    name=(char *)malloc(20);

    return name;
    ----------
    ----------------
    -----------------
    free(name); /*remving the memory but after the statement "return" this line will not be executed...so the pointer remains mortal and creates bugs when i recall the function */
    }

    Is that because you haven't saved the pointer?
    how about....

    char* return_name(char *name)
    {

    name=(char *)malloc(20);

    return name;
    }

    then when you call it....

    char* name=return_name(input_string);
    .
    .
    .
    .
    free(name);

    For your second question the compiler makes a temporary object to pass the value back to the callee. This is all done behind the scenes and you do not need to worry about it just be aware of whats happening.Just make sure that the value passed back isn't a pointer to a local object within the function.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The scope of malloc / free has nothing to do with where you malloc something (in one function), and where you free it (in another function).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *new_name ( ) {
        char *res = malloc( 20 );
        strcpy( res, "hello world\n" );
        return res;
    }
    int main ( ) {
        char *foo = new_name( );
        printf( "%s", foo );
        free( foo );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, but doesn't the pointer go out of scope after function exit, thus making it possible that you end up with a null pointer?

    I am specifically talking about Salems demonstration.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    res itself goes out of scope (this much is true), but it's value (the pointer) is preserved through the return statement in the function, and subsequent assignment in the caller.

    What an odd question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM