Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

memory allocation

Posted on 2004-11-19
8
Medium Priority
?
264 Views
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 moves.how should i handle it.I don't want to use dynamic memory allocation.
0
Comment
Question by:diptimali
  • 3
5 Comments
 

Author Comment

by:diptimali
ID: 12624315
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?
0
 
LVL 3

Accepted Solution

by:
Santino_k earned 100 total points
ID: 12624463
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!
0
 
LVL 3

Expert Comment

by:Santino_k
ID: 12624471
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" );
   else
      printf( "Can't allocate memory\n" );
   free( buffer );
}
0
 
LVL 3

Assisted Solution

by:esorf
esorf earned 100 total points
ID: 12627411
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);
}

to

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
0
 
LVL 3

Expert Comment

by:Santino_k
ID: 12820102
I think myself and esorf  should get points :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Progress
Starting up a Project

810 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