0 registered members (),
1,209
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
When (and how) tu use malloc?
#252339
02/17/09 19:13
02/17/09 19:13
|
Joined: Sep 2005
Posts: 75 CA, Orange County
Gerrit
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2005
Posts: 75
CA, Orange County
|
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?
|
|
|
Re: When (and how) tu use malloc?
[Re: Gerrit]
#252384
02/18/09 00:04
02/18/09 00:04
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
User
|
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi! Try putting a while (!situation) {wait(1);} //your situation == your code. Before using your array. This might help. Ottawa
|
|
|
Re: When (and how) tu use malloc?
[Re: Ottawa]
#252393
02/18/09 01:19
02/18/09 01:19
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
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. 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.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: When (and how) tu use malloc?
[Re: Gerrit]
#252406
02/18/09 05:46
02/18/09 05:46
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
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. )
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: When (and how) tu use malloc?
[Re: Gerrit]
#252587
02/19/09 05:31
02/19/09 05:31
|
Joined: Oct 2008
Posts: 218 Nashua NH
heinekenbottle
Member
|
Member
Joined: Oct 2008
Posts: 218
Nashua NH
|
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.
|
|
|
|