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
0 registered members (), 1,295 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
using Malloc() to allotcate an array? #318859
04/10/10 12:56
04/10/10 12:56
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Is it possible to use malloc() to allocate an array at run time?

I tried the following and it just plain doesnt work:

var my_array_size = 100;
var** my_array;

function my_function_startup()
{ var count;
// allocate the array
my_array = (var**)malloc(sizeof(var)*my_array_size);
// initialize all array indexs to 0.
count = 0;
while(count<my_array_size)
{ my_array[count] = 0;
count += 1;
}
}

I get a compile error on 'my_array[count] = 0;' - 'subscript requires an array or pointer type' - in other words, my_array isnt being allocated i think?

ive also tried 'my_array = (var**)malloc(sizeof(var*)*my_array_size);' and that doesnt do me any good either.

Re: using Malloc() to allotcate an array? [Re: Caermundh] #318865
04/10/10 13:30
04/10/10 13:30
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
why do you use var**? for an one dimensional array just use var*.

Re: using Malloc() to allotcate an array? [Re: ventilator] #318874
04/10/10 15:29
04/10/10 15:29
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
Nope that doesnt work either:

var my_array_size = 100;
var* my_array;

function my_function_startup()
{ var count;
// allocate the array
my_array = (var*)malloc(sizeof(var)*my_array_size);
// initialize all array indexs to 0.
count = 0;
while(count<my_array_size)
{ my_array[count] = 0;
count += 1;
}
}

I still get the same compile error

Re: using Malloc() to allotcate an array? [Re: Caermundh] #318878
04/10/10 16:02
04/10/10 16:02
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
I don't get any error at all. The memory is allocated at runtime, so if its a compile time error, then it's not an unsuccessful allocation.

Are you sure you are getting the error, maybe you have a typo or a different version from the one you posted.

Re: using Malloc() to allotcate an array? [Re: DJBMASTER] #318925
04/10/10 19:57
04/10/10 19:57
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
hummm, when I do the var* as a global it DOES work....bugger.

Heres what im actually doing: (I didnt post this because I didnt think it made a difference. Im still allocating to a var* either way, but obviously there is a difference if one way produces a compile error and the other doesn't)

<objects.h file>
typedef struct my_struct
{ STRING* str1;
var var1;
var var2;
var* my_array;
struct my_struct* next;
} my_struct;

my_struct* struct_ptr;

<main.c>
#include <default.c>;
#include <acknex.h>;
#include "objects.h";

function my_function_startup()
{ var count;
var my_array_size = 100;
struct_ptr = (my_struct*)malloc(sizeof(my_struct*));
struct_ptr->my_array = (var*)malloc(sizeof(var)*my_array_size);
count = 0;
while(count<my_array_size)
{ struct_ptr->my_array[count] = 0;
count += 1;
}
}

'struct_ptr->my_array[count] = 0;' of course produces the error.

Re: using Malloc() to allotcate an array? [Re: Caermundh] #318926
04/10/10 20:18
04/10/10 20:18
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
struct_ptr = (my_struct*)malloc(sizeof(my_struct*));

you have to allocate space for your struct. not for the struct pointer.

Re: using Malloc() to allotcate an array? [Re: ventilator] #319007
04/11/10 13:26
04/11/10 13:26
Joined: Aug 2005
Posts: 238
Caermundh Offline OP
Member
Caermundh  Offline OP
Member

Joined: Aug 2005
Posts: 238
struct_ptr = (my_struct*)malloc(sizeof(my_struct)); - I still get the error message this way

struct_ptr = (my_struct)malloc(sizeof(my_struct)); - gives me compile time error 'cannot convert pointer to struct my_struct'

struct_ptr = (my_struct*)malloc(sizeof(my_struct*)); - this line works fine, it lets me assign a string to str1 by struct_ptr->str1 = str_create("hello world") and lets me assign numbers to var1 & var2 by struct_ptr->var1 = 1;, etc.

Im allocating space for the structure, but I cant seem to allocate an array for struct_ptr->my_array.


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