Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, TedMar, dr_panther, Ayumi), 1,048 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
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 Offline OP
Junior Member
Gerrit  Offline 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
O
Ottawa Offline
User
Ottawa  Offline
User
O

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. smile

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 Offline
Expert
EvilSOB  Offline
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.
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.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: When (and how) tu use malloc? [Re: EvilSOB] #252402
02/18/09 04:31
02/18/09 04:31
Joined: Sep 2005
Posts: 75
CA, Orange County
Gerrit Offline OP
Junior Member
Gerrit  Offline OP
Junior Member

Joined: Sep 2005
Posts: 75
CA, Orange County
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

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 Offline
Expert
EvilSOB  Offline
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: EvilSOB] #252408
02/18/09 05:53
02/18/09 05:53
Joined: Sep 2005
Posts: 75
CA, Orange County
Gerrit Offline OP
Junior Member
Gerrit  Offline OP
Junior Member

Joined: Sep 2005
Posts: 75
CA, Orange County
Thanks EvilSOB
I will get a cimple c++ one...
Gerrit

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 Offline
Member
heinekenbottle  Offline
Member

Joined: Oct 2008
Posts: 218
Nashua NH
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.


I was once Anonymous_Alcoholic.

Code Breakpoint;

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1