memory allocation

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.
diptimaliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

diptimaliAuthor Commented:
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
Santino_kCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Santino_kCommented:
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
esorfCommented:
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
Santino_kCommented:
I think myself and esorf  should get points :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.