Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (Joey, flink, AndrewAMD, ozgur, Ayumi), 1,195 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19059 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
memory allocation #237552
11/20/08 16:41
11/20/08 16:41
Joined: Aug 2004
Posts: 1,305
New York
PrenceOfDarkness Offline OP
Serious User
PrenceOfDarkness  Offline OP
Serious User

Joined: Aug 2004
Posts: 1,305
New York
okay i've been avoiding this topic for a long time simply because it's a bit scary (hold me :().

Okay I get the basic drift of it but I'm not really seasoned in it.

lets say I have defined a struct and I want to create a new struct on the fly, I understand now that I need to set memory aside for it. I believe when using malloc it returns a pointer to that memory location. Can someone please give me a simple example of how to do that?

Also i'm noticing that sometimes I want different size arrays. Again memory allocation is the key to this as well. Can someone give me a simple example of how to allocate memory for a varying array?


"There is no problem that can't be solved with time and determination." -me
prenceofdarkness for instant messages on AIM.

Looking for a model designer
PLEASE, SEND ME A PRIVATE MESSAGE OR EMAIL IF YOU'RE INTERESTED.
Re: memory allocation [Re: PrenceOfDarkness] #237554
11/20/08 17:13
11/20/08 17:13
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Don't be afraid, I am from the Internet, ma'am! ;-)

Code:
typedef struct mytruct // struct definition
{
  int example;
};
void foo()
{
  mystruct ms = NULL;                // variable definition
  ms = malloc(sizeof(mystruct));     // allocate memory.
  memset(ms,0,sizeof(mystruct));     // initialize memory.
  ms->example = 1337;                // use the struct
  free(ms);                          // free the memory allocated
  ms = NULL;                         // signalize the pointer has become invalid.
}


And for the dynamic array ...

Code:
void bar()
{
  int* a = NULL;                   // define array as a pointer to an int.
  a = malloc(sizeof(int) * 23);    // Allocate memory for 23 elements of int
  a[12] = 1337;                    // Use the array for whatever you want.
  free(a);                         // free the allocated memory.
  a = NULL;                        // invalidate the pointer.
}




Always learn from history, to be sure you make the same mistakes again...
Re: memory allocation [Re: Uhrwerk] #237610
11/20/08 23:16
11/20/08 23:16
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Quote:
Don't be afraid, I am from the Internet, ma'am! ;-)

Shouldn't that raise a grave fear instead? grin.

I do believe you need to typecast the newly allocated memory block pointer to your desired structure. It's required in C, not sure if the Lite-C compiler complains about it.
Code:
 ms = (mystruct*)malloc(sizeof(mystruct));     // allocate memory.
 ...
 a = (int)malloc(sizeof(int) * 23);



Last edited by Joozey; 11/20/08 23:24.

Click and join the 3dgs irc community!
Room: #3dgs
Re: memory allocation [Re: Joozey] #237612
11/20/08 23:44
11/20/08 23:44
Joined: May 2006
Posts: 53
Puerto Rico
monchito Offline
Junior Member
monchito  Offline
Junior Member

Joined: May 2006
Posts: 53
Puerto Rico
Hi
my compiler complains with the struct code. I have to rewrite to get it work. smile Not sure if typecasting is required in C-lite but I think yes.
Code:
[/code]

typedef struct  // struct definition
{
  int example;
}mystruct;

[code]


Re: memory allocation [Re: monchito] #237628
11/21/08 02:50
11/21/08 02:50
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Type-casting IS required but not by the compiler.
IE It will compile the malloc command without typecasting,
but later calls to that variable will fail IF IT IS A STRUCT.
If its not a struct, it seems to work OK, I think it is defaulting
to a var pointer.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: memory allocation [Re: EvilSOB] #237764
11/22/08 04:26
11/22/08 04:26
Joined: Aug 2004
Posts: 1,305
New York
PrenceOfDarkness Offline OP
Serious User
PrenceOfDarkness  Offline OP
Serious User

Joined: Aug 2004
Posts: 1,305
New York
after looking into this I found out that type casting is not a requirement however it is high suggested. Of course this is for C, and i'm not sure about lite C.

THe code looks useful and friendly I'll look into it and ask any questions if i encounter and problems. Thank you for the commments guys!


"There is no problem that can't be solved with time and determination." -me
prenceofdarkness for instant messages on AIM.

Looking for a model designer
PLEASE, SEND ME A PRIVATE MESSAGE OR EMAIL IF YOU'RE INTERESTED.
Re: memory allocation [Re: PrenceOfDarkness] #237821
11/22/08 16:51
11/22/08 16:51
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Typecasting is not necessary in Lite-C. However, it's a good habbit to do it and it's best you get once used to it and then do it forever. I simply forgot it when writing the examples.


Always learn from history, to be sure you make the same mistakes again...

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