Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Allocate memory inside function ??

Allocate memory inside function ??

Scheduled Pinned Locked Moved C / C++ / MFC
22 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F fx9200

    void fill(void *input); struct mystruct { char *ch; int n; }; #define ARG_SIZE sizeof(struct mystruct) int main(){ void *ptr; ptr = (void*) malloc (ARG_SIZE); fill(ptr); printf ("%i",((struct mystruct*)ptr)->n); printf ("--%s",((struct mystruct*)ptr) ->ch); return 0; } void fill(void *input){ struct mystruct* st; st = (struct mystruct*) input; st ->n = 3;//operation is ok st->ch = (char *) malloc (3*sizeof(char)); strcpy(st->ch,"OK"); input = (void*) st; }

    R Offline
    R Offline
    Roger Stoltz
    wrote on last edited by
    #21

    What I think you need to understand after having read your previous posts, is that when you call a function the arguments passed are copies on the stack. Have a look here[^] if you're not sure what the stack is and how it is used. Let's examine this with some code examples. A function called AddOne is used for increasing a variable by the value of 1.

    void AddOne( int nTheVariable )
    {
        nTheVariable = nTheVariable + 1;
    }
    
    void main()
    {
        int nMyValue = 0;
        printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0."
        AddOne( nMyValue );
        printf( "The value is %d.\n", nMyValue ); // Also prints "The value is 0."
    }
    

    When AddOne() is declared as above, the value of nMyValue is copied to the stack and read by AddOne(). When the function returns is has indeed increased its local copy (nTheVariable) by one, but nMyValue still remains the same since it was a copy of it that was passed to the function. If you want to alter the value of nMyValue, you have to pass its location as argument to AddOne(). "Location" in this aspect means "the address of". In code it would look like this:

    void AddOne( int* pnTheVariable )
    {
        // Here we've got a copy of the address of the variable so
        // we can modify it at its original location 
        *pnTheVariable = *pnTheVariable + 1;
    }
    
    void main()
    {
        int nMyValue = 0;
        printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0."
        AddOne( &nMyValue ); // Pass a copy of the address of nMyValue
        printf( "The value is %d.\n", nMyValue ); // Prints "The value is 1."
    }
    

    In your case you want to allocate memory and you always assign it to a pointer. To be able to alter the address assigned to a pointer, you have to pass a copy of the address of the pointer to the function. This is still the same as above, but now you want to alter the value of a pointer. In code it would look something like this if you want to allocate memory for three ints:

    void Allocate( int** ppnTheMemory )
    {
        *ppTheMemory = (int*)malloc( 3 * sizeof( int ) );
    }
    
    void main()
    {
        int* pnMyValues = NULL;
        Allocate( &pnMyValue ); // Pass the address of the pointer
        if( pnMyValues )
        {
            free( pnMyValues );
    
    1 Reply Last reply
    0
    • F fx9200

      how to pass reference(pointer) by reference ? please give example

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #22

      fx9200 wrote:

      how to pass reference(pointer) by reference ? please give example

      See here.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups