How Do You Add Onto Dynamic Memory After You've Already Malloc-ed It?

This is a discussion on How Do You Add Onto Dynamic Memory After You've Already Malloc-ed It? within the C Programming forums, part of the General Programming Boards category; Hi! I'm a new C programmer, and I just took my final exam! It was complicated, but I understood everything ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Oakland, CA
    Posts
    6

    How Do You Add Onto Dynamic Memory After You've Already Malloc-ed It?

    Hi! I'm a new C programmer, and I just took my final exam! It was complicated, but I understood everything except how to store the results of a specific function's returns into dynamic memory.

    The test was a lot longer and more complicated than the code I'm going to post, but basically the user was in a menu ( do / while loop ) that would have them enter a number, send that number to a function. The function would then break up the number and do all sorts of nefarious things to it, then return the mutilated int it crunched out.

    The program was supposed to keep track of the returned values into dynamic memory, and then print them out in the end. I understand how to grab a chunk of dynamic memory using malloc, but I don't understand if I have a certain chunk of dynamic memory, how to add onto it, and keep the previous values in the memory intact.

    Here is the sample code I wrote that kind of mimics my final ( very basic )

    Code:
    /**
    *Program Name:      junk test
    *Written  By:       steven.riedel@gmail.com
    *Discussion:        using pointers
    *Compiler:          gcc
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int square( int );
            /* returns the square of a number */
    
    int main( void ) {
    
            int number, theSquare, count = 0; // ints to hold stuff
            int* pointer;  // my pointer
            int i;  // index var for "for" loops
    
            do {
    
            printf( "\nHello, lover, what number would you like to square?"
                    "\n  Enter a negative number to quit : " );
            scanf( "%d", &number );
    
            if ( number >= 0 ) {
                    count++;
                    theSquare = square( number );
                    printf( "\nThe square of %d is %d\n", number, theSquare );
                    /* I'm sure this is where Im getting my code wrong.  I want to have a way to add
                     * onto dynamic memory 4 bytes at a time ( int's size ) and then add the newly found
                     * square that was returned into that spot
                     */
                    pointer = ( int* ) malloc( sizeof( int ) * count );
                    *( pointer + count ) = theSquare; 
            }
    
            } while ( number >= 0 );
    
            printf( "\nHere were the squares that I found:\n" );
    
            for ( i = 0; i < count; i++ ) {
                    printf( "\n%d", *( pointer + count ) );
    
            }
    
            free( pointer );
      
    return 0;
    }
    
    int square( int arg ) {
    
    return arg * arg;
    }
    
    /* OUTPUT:
    Hello, lover, what number would you like to square?
      Enter a negitive number to quit : 3
    
    The square of 3 is 9
    
    Hello, lover, what number would you like to square?
      Enter a negitive number to quit : 4
    
    The square of 4 is 16
    
    Hello, lover, what number would you like to square?
      Enter a negitive number to quit : 5
    
    The square of 5 is 25
    
    Hello, lover, what number would you like to square?
      Enter a negitive number to quit : 6
    
    The square of 6 is 36
    
    Hello, lover, what number would you like to square?
      Enter a negitive number to quit : 7
    
    The square of 7 is 49
    
    Hello, lover, what number would you like to square?
      Enter a negitive number to quit : -1
    
    Here were the squares that I found:
    
    49
    49
    49
    49
    49
    */
    I'm sure there has to be a way to add onto memory already allocated, but our teacher never told us how to do it...maybe it's simple and I just don't get the logic? Thanks for the help!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    You can use realloc().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Oakland, CA
    Posts
    6
    ohhh realloc()!! I've never heard of it! I will check it out! Happen to have a syntax of it in use? I knew that teacher was up to trixy business! I just knew it!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your description of keeping a returned ints in dynamic memory, immediately struck as something to use in a linked list.

    Have you considered that?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    realloc is basically the old pointer with the previously allocated memory + the new size.
    It will expand the memory if possible; if not, it will allocate a new region of memory and copy over the contents. The return value of realloc is the storage area where the memory lies. It may or may not be the same address. It can also return NULL if it failed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Elysia View Post
    It can also return NULL if it failed.
    Which means you should never do this:
    Code:
    type *p = malloc( foo );
    ...
    p = realloc( p, ... );
    Because you will lose your old data if it returns NULL. Instead you should do:
    Code:
    type *p, *q;
    
    p = malloc( foo )
    ...
    
    q = realloc( p, ... );
    if( q )
        p = q;
    That way, if it fails, you still have your original data.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    << applause! >>

    Excellent point, Quzah.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Location
    Oakland, CA
    Posts
    6
    This really helps! Thank you very much everyone!

  9. #9
    Registered User
    Join Date
    Sep 2009
    Location
    Oakland, CA
    Posts
    6
    Quote Originally Posted by Adak View Post
    Your description of keeping a returned ints in dynamic memory, immediately struck as something to use in a linked list.

    Have you considered that?
    We haven't learned linked lists. I'll check them out!! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory (Static vs Dynamic)
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 12-11-2009, 05:39 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Why use Dynamic Memory
    By appleGuy in forum C++ Programming
    Replies: 11
    Last Post: 08-28-2006, 02:46 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 07:34 PM

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