memory allocation

Posted on 2004-11-19
Last Modified: 2010-04-17
i am trying to replace dynamic memory used in a game code with fixed memory(i.e replace calloc with array).but the memory allocated needs to be changed after some should i handle it.I don't want to use dynamic memory allocation.
Question by:diptimali

    Author Comment

    calloc is used  4 times in program,the program runs properly for 1st time then ,it crashes down or gives garbage value.are pointers not handled properly?
    LVL 3

    Accepted Solution

    Look using Dynamic Memory Allocation in your case is the Best approach according to me since using Arrays would be kind of Static Memory Allocation that might result in short memory availability than required or result in memory wastage.
    If you know the Maximum size you will need for storage and are sure that it will be enough then use Array.

    diptimali >>calloc is used  4 times in program,the program runs properly for 1st time then ,it crashes down or gives garbage value.are pointers not handled properly?

    Yes the pointers are not used properly in your case. Check for Pointer validity every time after you do a calloc.

    I hope this helps. Let me know if you have more doubts on this.

    Have a great day ahead!
    LVL 3

    Expert Comment

    This is what I mean by checking for Pointer validity every time after you do a calloc -

    void main( void )
       long *buffer;

       buffer = (long *)calloc( 40, sizeof( long ) );
       if( buffer != NULL )
          printf( "Allocated 40 long integers\n" );
          printf( "Can't allocate memory\n" );
       free( buffer );
    LVL 3

    Assisted Solution

    You're going to have a lot of trouble converting from allocated blocks to arrays.

    Foremost: Arrays don't stick around once you leave the function they're declared in.

    You can't change

    byte* returnBuffer(int size)
        return calloc(size);


    byte* returnBuffer(int size)
        byte array[size];
        return array;
    }  // The array[size] was allocated on the stack and now it no longer exists, even though you return a pointer.  Yipes!

    You'll have to get comfortable with heap allocation.  Try using smart pointers in C++ that handle some memory lifetime issues for you (though you'll still have to understand what's going on to get it right).

    - Frank
    LVL 3

    Expert Comment

    I think myself and esorf  should get points :-)

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    Join & Write a Comment

    A short article about a problem I had getting the GPS LocationListener working.
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now