When (and how) tu use malloc?

Posted By: Gerrit

When (and how) tu use malloc? - 02/17/09 19:13

I use a couple of arrays in my program. The game consists of several levels and when I run one level at a time (to speedup design) I don't have a problem. However when I plug all the levels together code that was running perfectly before now gives me an error when using arrays. (Error E1513) The error occurs when I manipulate the following arrays:

int pole_flags[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int pole_good[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

I also tried with var

Again, if I run the level seperately no problem only when I plug the levels into one application do I have that error.

Could I be using too much memory? And if so how do I know and how would I use malloc for these arrays?

Thanks for your help / suggestions
PS. I read about malloc and did a search on this forum but could not find a clear example how to use it, and how to free memory. Can you perhaps guide me to where I can find more (and easy) examples?
Posted By: Ottawa

Re: When (and how) tu use malloc? - 02/18/09 00:04

Hi!

Try putting a
while (!situation) {wait(1);} //your situation == your code.

Before using your array.

This might help. smile

Ottawa
Posted By: EvilSOB

Re: When (and how) tu use malloc? - 02/18/09 01:19

The arrays shouldnt cause a problem unless you have defined them differently (sized) in two separate scripts,
or they are just being defined twice.

Also beware of using variables in other scripts that have the same name. That is, dont have int pole_flags[15]=...
in one script, and var pole_flags = 25; in another.

To create your arrays using malloc, heres an example.
Code:
int pole_flags[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int pole_good[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//
//becomes
//
int* pole_flags;     //delare array name
int* pole_good;      //delare array name
void poles_startup()
{
   pole_flags = (int*)malloc((long)sizeof(int)*15);   //allocate space for 15 int's
   memset(pole_flags, 0, (long)sizeof(int)*15);       //set data to all zeros
   pole_good = (int*)malloc((long)sizeof(int)*15);   //allocate space for 15 int's
   memset(pole_good, 0, (long)sizeof(int)*15);       //set data to all zeros
}
//
// Theoretically this will work too, but Ive never tried this style.
//
int* pole_flags = (int*)malloc((long)sizeof(int)*15);   //allocate space for 15 int's
int* pole_good = (int*)malloc((long)sizeof(int)*15);   //allocate space for 15 int's
void poles_startup()
{
   memset(pole_flags, 0, (long)sizeof(int)*15);       //set data to all zeros
   memset(pole_good, 0, (long)sizeof(int)*15);       //set data to all zeros
}

NOTE: Putting the (long) before the sizeof is VERY important, or it gives zero length arrays.
Posted By: Gerrit

Re: When (and how) tu use malloc? - 02/18/09 04:31

EvilSOB
Thank you very much for the code. Malloc works great but you were also right that was not the REAL problem but at least you did help me eleminate the issue of perhaps running out of memory. I'm sure I will find the real problem.

I am a VB programmer and relatively new in c (or lite-c) what book would you recommend (or is there a web site) to sharpen up my c knowledge?

Thanks again
Gerrit
Posted By: EvilSOB

Re: When (and how) tu use malloc? - 02/18/09 05:46

Cant help much with books, Im a practice learner, but any C++ books will help, just dont bother too much
with the advanced stuff like clases etc just yet, leave that till gamestudio supports it. (it will eventually, I just dont know when)
I dare say C++ for dummies would be perfect, even though Ive never seen it.
Visual C++ for dummies (if it exists) may help you drift from VB to C++ easier.
(Im primarily a VB# (with a touch of C#) programmer myself. )
Posted By: Gerrit

Re: When (and how) tu use malloc? - 02/18/09 05:53

Thanks EvilSOB
I will get a cimple c++ one...
Gerrit
Posted By: heinekenbottle

Re: When (and how) tu use malloc? - 02/19/09 05:31

Quote:
I dare say C++ for dummies would be perfect, even though Ive never seen it.


C++ For Dummies is an excellent book for beginning the C++ language. Mine goes from the basics all the way into the more challenging stuff like polymorphism and inheritance and all of that good stuff. And it is full of testable examples and good explanations.

It doesn't go into API, but it does lay out the basic foundation.

However, its been a while since I got it and so I don't remember where it is from. I'm sure amazon.com or barnes and noble have it.
© 2024 lite-C Forums