Gamestudio Links
Zorro Links
Newest Posts
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
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,454 guests, and 9 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rating: 5
Using var types in structs? #178121
01/15/08 22:42
01/15/08 22:42
Joined: Mar 2002
Posts: 580
San Francisco
clone45 Offline OP
User
clone45  Offline OP
User

Joined: Mar 2002
Posts: 580
San Francisco
Hello,


I'm playing around with structures and lite-c for the first time. I'd like to store a string in my structure. I thought, instead of mess around with char* stuff, maybe I could use a "var" type.

Here's my example:

Code:

#define new(type) malloc(sizeof(type##))
#define delete(object) free(object)

// Item
typedef struct
{
int id;
var name;
} Item;

Item* sword = new(Item);

sword->name = "sword"; // This line causes the error



This results in an compile error: "Cannot convert array to fixed."

Any ideas?

Thanks,
- Bret

Re: Using var types in structs? [Re: clone45] #178122
01/15/08 22:53
01/15/08 22:53
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
STRING* is a string and can be filled using str_cpy. I don't know if there is another, more "C like" way, but that works for me . I do know for sure that var is only to store numbers. It's like an integer with 3 decimal numbers.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Using var types in structs? [Re: Joozey] #178123
01/16/08 02:20
01/16/08 02:20
Joined: Mar 2002
Posts: 580
San Francisco
clone45 Offline OP
User
clone45  Offline OP
User

Joined: Mar 2002
Posts: 580
San Francisco

Thanks! That worked. Here's my corrected code:

Code:

typedef struct
{
int id;
STRING* name;
} Item;

Item* sword = new(Item);
sword->name = str_create("sword");
sword->id = 1;



Re: Using var types in structs? [Re: clone45] #178124
01/16/08 10:18
01/16/08 10:18
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Yes, that's the correct way to do it.

Be sure to seperately deallocate the STRING before you deallocate the struct. Otherwise only the pointer (4 bytes) is deallocated, instead of the string it points to. You may tend to forget this, especially when you create a struct on the stack (inside a function) because the struct is deallocated automatically, but the string isn't!

Re: Using var types in structs? [Re: Excessus] #178125
01/16/08 14:28
01/16/08 14:28
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Now we're on it anyway, what does "deallocation of a struct" actually means?


Click and join the 3dgs irc community!
Room: #3dgs
Re: Using var types in structs? [Re: Joozey] #178126
01/16/08 15:10
01/16/08 15:10
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Someone should make a FAQ about memory allocation, stack vs. heap, etc.

You can allocate something in two ways:
1) stack
whenever you put "var myvar" or "int myint" or "somestruct mystruct" in a function, you are allocating data on the stack.

2) heap
You can allocate a certain number of bytes from the heap with malloc(numbytes). Malloc returns a pointer to the allocated memory (the first byte). You can use it like this: mystruct* p; p = malloc(SIZEOF(mystruct);
There are creating functions for all structs in the a7 engine, like string_create(), that use malloc (or C++ new operator).

Deallocation:
1) stack
Memory on the stack is deallocated automatically for you when the function ends (actually, when the current scope ends, I think).

2) heap
Memory on the heap must be deallocated by you when you don't need it anymore. You can use free() for that. You should use ptr_remove for STRINGs, BMAPs, etc.




So if you want a function that creates a new object (var, float, some struct, whatever), you want that object to still exist after the creating function ends. So you must allocate it on the heap with malloc, and return a pointer to it.

Re: Using var types in structs? [Re: Excessus] #178127
01/16/08 18:20
01/16/08 18:20
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Lol, last week I was stuck on structs that do not exist anymore after a function ends. So it's possible to keep it existing without defining a struct outside the function ... sigh.

Thanx alot for the info!

Quote:


There are creating functions for all structs in the a7 engine, like string_create(), that use malloc (or C++ new operator).





Is ent_create an exception? For max_entities indicates the maximum amount of entities, and I assumed the compiler made an array with max_entities length. But acknex wouldn't need that if they used allocation right? Or am I being weird now .

Last edited by Joozey; 01/16/08 18:25.
Re: Using var types in structs? [Re: Joozey] #178128
01/16/08 18:52
01/16/08 18:52
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Quote:


Lol, last week I was stuck on structs that do not exist anymore after a function ends. So it's possible to keep it existing without defining a struct outside the function ... sigh.



Yes but be careful with it, you might create really nasty bugs. (Like the program crashing randomly after some time)


I'm not sure about ent_create. I know the engine uses a linked list for all engine objects internally. So that could not be the reason for the max_entities limitation. This is indirectly confirmed by the manual which states: "for every entity, around 1.5..2.5 KB memory is reserved from the nexus, dependent on its bones size." (max_entities page). That suggests that this memory is only allocated when the entity is created because it can't know the bones size before that.

EDIT: but then again, the word "reserved" suggests that it is in fact allocated beforehand.. I don't know.

Last edited by Excessus; 01/16/08 18:54.

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