memory issues with structs?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
CheChin
Posts: 8
Joined: Thu Aug 04, 2005 10:51 am

memory issues with structs?

Post by CheChin »

it's hard to explain, so here's an example.

my structs are defined like this:

Code: Select all

typedef struct {
	double x;
	double y;
	double dx;
	double dy;
	int carrier;
	int avoid;
} BALL_STRUCT;

typedef struct {
	double x;
	double y;
	double dx;
	double dy;
	double sa;
	int team;
	int keys;
	int coll;
} USER_STRUCT;

typedef struct {
	double x;
	double y;
	double delta;
	double dest;
} GLIE_STRUCT;
then i make the objects with theese types:

Code: Select all

BALL_STRUCT ball;
USER_STRUCT user[USER_COUNT - 1];
GLIE_STRUCT goalie[GOALIE_COUNT - 1];
then i set the initialization values:

Code: Select all

ball.x = 240;
ball.y = 136;
ball.avoid = 255;
ball.carrier = 255;

user[0].x = 50;
user[0].y = 136;
user[0].team = 0;

user[1].x = 430;
user[1].y = 136;
user[1].team = 1;

goalie[0].x = 4;
goalie[0].y = 136;

goalie[1].x = 476;
goalie[1].y = 136;
this results in ball.x having a value of 476, AND continue to get that value even after i alter goalie[1].x again. Why is that?
User avatar
mc
Posts: 211
Joined: Wed Jan 12, 2005 7:32 am
Location: Linköping

Post by mc »

Let me guess... GOALIE_COUNT (and USER_COUNT) is 2? In that case you are writing outside the arrays and trashing memory. You probably made some kind of thought lapse when you wrote [...COUNT - 1] instead of [...COUNT].
Flying at a high speed
Having the courage
Getting over crisis
I rescue the people
User avatar
ReKleSS
Posts: 73
Joined: Sat Jun 18, 2005 12:57 pm
Location: Melbourne, Australia

Post by ReKleSS »

Also, on a half-related note, from what I recall the psp works considerably faster with floats than with doubles. You probably shouldn't be using doubles unless you really need the precision.
-ReK
CheChin
Posts: 8
Joined: Thu Aug 04, 2005 10:51 am

Post by CheChin »

thanx mc, that was the case. works perfectly now.

all the vars' were origionally floats, but i changed em since i thought doubles was faster. thanx for the information, ReKleSS
Post Reply