Skip to main content
GameDev.net gamedev.net
🔒 Locked

pointing to structs

Started by Redoc Dec 4, 2002 at 10:23 PM 10 replies 550+ views
Original Post
Redoc
Redoc
Hi, This is an edited post as I solved my own problem (gosh I can be stupid!) My *new* question is "Is this a safe way to do what I'm doing?" and is it correct? (should I use return instead? if so how?)... I want to make sure there are no memory leaks mainly. I have a structure like so struct PERSON { int age; char name[11]; } and I create an array like so PERSON Peep[3]; and I want a function to fill out the details so I do this: void UpdatePeeps(PERSON * Peep) { strcpy(Peep[0].name, "Fred"); Peep[0].age = 20; strcpy(Peep[1].name, "JOE"); Peep[1].age = 10; strcpy(Peep[2].name, "BILL"); Peep[2].age = 29; } Thanks sorry about the edit [edited by - redoc on December 4, 2002 11:30:47 PM]
Redoc
Redoc
Thanks! and WOW what a quick reply! though you have replied to my message before my edit (oops)

is the code I have safe to use? it appears to work as it is now.

Redoc
Redoc
Thanks again for your prompt reply!

However, it doesnt compile when I change the "."''s to "->"''s

The code I posted above actually does work. I would like to confirm the questions:

Is what I have done te correct way to do it

Should I use return instead? if so how?

and finally I want to make sure there are no memory leaks.

Thank you
Redoc
Redoc
Its amazing you reply faster than I can even write

(also sorry I can imagine this gets asked all the time too.. I tried reading up)

so what is the use in return, if you can alter the data as a parameter?

just curious

thanks lots
Redoc
Redoc
I see, I knew to return for the "fast" exit..

but having thought about it I imagine the use of return is to beable to make your commands easier.

like a return of a bool,

if (Initialization())
{
// init returned true so do something here
{
else
{
// init returned false so quit
}

Thanks, I''ll get the hang of it all soon
necromancer_df
necromancer_df
quote:
Original post by Anonymous Poster


void UpdatePeeps(PERSON * Peep)
{
strcpy(Peep[0]->name, "Fred");
Peep[0].age = 20;
strcpy(Peep[1]->name, "JOE");
Peep[1].age = 10;
strcpy(Peep[2]->name, "BILL");
Peep[2].age = 29;
}




dude that's not safe. there is no way UpdatePeeps can know how big Peep is. if the array you pass in only has 2 elements then when you do stuff to Peep[2] you are writing to some random part of memory. sure, maybe you'll just make sure that you always pass an array of at least 3 elements but it could be a source of errors if you're not careful. also you don't check the lengths of the names so if you had a really long name it would write past the end of the string and cause problems.

edit: broken code tags

[edited by - necromancer_df on December 4, 2002 12:16:05 AM]
Alpha_ProgDes
Alpha_ProgDes
void UpdatePeeps(PERSON * Peep)
{
strcpy(Peep[0].name, "Fred");
Peep[0].age = 20;
strcpy(Peep[1].name, "JOE");
Peep[1].age = 10;
strcpy(Peep[2].name, "BILL");
Peep[2].age = 29;
}

wouldn''t be better to do it like this?

void UpdatePeeps (PERSON people[])
{
strcpy(Peep[0].name, "Fred");
Peep[0].age = 20;
strcpy(Peep[1].name, "JOE");
Peep[1].age = 10;
strcpy(Peep[2].name, "BILL");
Peep[2].age = 29;
}

i take it you are doing this in C...
Beginner in Game Development?  Read here. And read here.  

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.