Page 1 of 1

About global struct pointer, Problem solved, Read me

PostPosted: Wed Sep 16, 2009 9:24 am
by cforall
The problem I come across

Global code :
Code: Select all
typedef struct tagtest
{
  int state;
} test;
test *mytest = (test*)malloc(sizeof(test));

Draw Actor
Code: Select all
if(mytest->state);
or
Code: Select all
if(((test *)mytest)->state);

a dialog will pop up and say READ error in invalid memory area

So I don't use pointer

Global code :
Code: Select all
typedef struct tagtest
{
  int state;
} test;
test mytest;

Draw Actor
Code: Select all
if((&mytest)->state);

everything is okay this time

with skydereign's help I get back to pointer

Global code :
Code: Select all
typedef struct tagtest
{
  int state;
} test;
test *mytest;

Create Actor
Code: Select all
mytest = (test*)malloc(sizeof(test));

Draw Actor
Code: Select all
if(mytest->state);


:mrgreen: :mrgreen: :mrgreen: :mrgreen:

Re: NEED HELP! with Global code

PostPosted: Wed Sep 16, 2009 9:39 am
by cforall
Is this a kind of security measure to prevent using pointer in some unsafety way?

Re: NEED HELP! with Global code

PostPosted: Wed Sep 16, 2009 2:03 pm
by Hblade
Some global codes can do that I hate it dude, it makes no sense it works fine without global code :/

Re: NEED HELP! with Global code

PostPosted: Thu Sep 17, 2009 5:30 am
by cforall
I update first post

ye Hblade as you say , some weird thing :?
I change the way to use global struct var , no pointer any more, this time it works~
but I still wanna know how to use global struct pointer, anyone know plz tell me :)
TIA!

Re: NEED HELP! with global struct pointer

PostPosted: Thu Sep 17, 2009 6:25 am
by skydereign
I used global struct pointers extensively for my TBS game that I have been working on, I think. I don't really remember what I did, but I had several layers of functions for it the entire system. The game used a large set of structs to hold everything, and a bunch of other stuff, so I am pretty sure I fixed whatever problem you are having. I used create and destroy functions, and wrapper allocation. There will be a problem if you are using variables, but I would always do the allocations in the create event of view. I haven't been working on the game recently, but I think that is what I did. I know I bypassed the problem you are having, but I don't remember how... Sorry if this didn't help. It is possible though... I am a bit tired right now, and don't have time to check my game, but I may do that later.

Re: NEED HELP! with global struct pointer

PostPosted: Thu Sep 17, 2009 7:00 am
by cforall
guys check my first post~

Thanks skydereign~! :D problem solved!!!+1
Absolutely right to do allocation in create event!
I am always a bit of a duffer .....