About global struct pointer, Problem solved, Read me

Non-platform specific questions.

About global struct pointer, Problem solved, Read me

Postby cforall » Wed Sep 16, 2009 9:24 am

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:
Last edited by cforall on Thu Sep 17, 2009 7:17 am, edited 5 times in total.
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score

Re: NEED HELP! with Global code

Postby cforall » Wed Sep 16, 2009 9:39 am

Is this a kind of security measure to prevent using pointer in some unsafety way?
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score

Re: NEED HELP! with Global code

Postby Hblade » Wed Sep 16, 2009 2:03 pm

Some global codes can do that I hate it dude, it makes no sense it works fine without global code :/
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: NEED HELP! with Global code

Postby cforall » Thu Sep 17, 2009 5:30 am

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!
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score

Re: NEED HELP! with global struct pointer

Postby skydereign » Thu Sep 17, 2009 6:25 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: NEED HELP! with global struct pointer

Postby cforall » Thu Sep 17, 2009 7:00 am

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 .....
User avatar
cforall
 
Posts: 65
Joined: Thu Aug 06, 2009 2:46 pm
Location: Shanghai
Score: 8 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron