|
Revised file read test
Ok... i cleaned up what i had to some extent (it''s amazing the mistakes one makes with only two hours of sleep for the past two days :D).
If anyone would help, i would appreciate it. Basically, when the char* temp below gets reallocated, it has a bunch of garbage stuck on the end when i read info into it. Furthermore, i need help with the frees at the bottom. Lastly, should i give up structures in C and just go with classes in C++? i think C++ would be easier, but i''m wondering about performance, etc. Anyways, to the brave soul who reads this, here ya go:
"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
i changed the above strcat''s to correctly be:
,etc.
Still having some problems... the second time through the big loop when i=1, nothing is written to the m_stTileSetHeader[1] or the m_stTileSetBody[1]... any help is appreciated.
Furthermore, i would love some recommendations on how to better go about this whole thing. Thanks!
"You call him Dr. Grip, doll!"
|
,etc.
Still having some problems... the second time through the big loop when i=1, nothing is written to the m_stTileSetHeader[1] or the m_stTileSetBody[1]... any help is appreciated.
Furthermore, i would love some recommendations on how to better go about this whole thing. Thanks!
"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
Since I''m not a master of c-style i/o (i use iostreams), I''ll just give you a little heuristic help.
A) You are not checking the reads to make sure they send anything ...
B) Output the strings as you read them ... clearly labeled ... this is the ONLY reasonable way to debug an unproven file parsing routine - This let''s you easily detect if your crossing white space and getting to the end of file too early ... or if your file format is slightly wrong.
C) I don''t understand why you are allocating the memory for the strings TWICE ... and copying from one to the other ... instead of just allocating it once ... and assigning that pointer into the struct (in other words transfering owenership to the struct)
That''s all the time I have for now ... I''ll try to get back later ...
Also .. you might as well get in the habit of not using void main(void) ... and instead use int main(void) .. which means you need to add a return 0; to the bottom of your main function ... The reason is simple ... void main(void) is NOT standard .. and only works on DOS/Windows platforms ... so when people grab your code and try to compile it under linux/beos like I just did (beos) they get needless warnings ...
Thanks and good luck
A) You are not checking the reads to make sure they send anything ...
B) Output the strings as you read them ... clearly labeled ... this is the ONLY reasonable way to debug an unproven file parsing routine - This let''s you easily detect if your crossing white space and getting to the end of file too early ... or if your file format is slightly wrong.
C) I don''t understand why you are allocating the memory for the strings TWICE ... and copying from one to the other ... instead of just allocating it once ... and assigning that pointer into the struct (in other words transfering owenership to the struct)
That''s all the time I have for now ... I''ll try to get back later ...
Also .. you might as well get in the habit of not using void main(void) ... and instead use int main(void) .. which means you need to add a return 0; to the bottom of your main function ... The reason is simple ... void main(void) is NOT standard .. and only works on DOS/Windows platforms ... so when people grab your code and try to compile it under linux/beos like I just did (beos) they get needless warnings ...
Thanks and good luck
Thanks for the input... i always just type void main(void) when running simple tests, but you''re right - i should use an int return.
As far as copying the strings twice, i tried assigning the structure pointer ownership of the char* temp pointer, but when the temp pointer is realloc''ed, the value in the structure becomes garbage (if i remember correctly). The syntax i used was something like this (pseudo)
struct[0].charpointer=temp;
Like i said, later on the value at struct[0].charpointer becomes some interesting garbage. If i''m doing this assignment wrong, please do help.
i appreciate the help - i''m struggling to figure this file i/o stuff out on my own and i''m new to C/C++. Any recommendations as to how to do this differently (even completely different) are definitely welcome (including better ways from a performance perspective). Maybe i should just stick with the C++ way... it seems a heck of a lot easier. Unfortunately, i''m so stinkin'' anal that i HAVE to figure this out (grrrr...).
Thanks again for the help! Any more input is appreciated.
"You call him Dr. Grip, doll!"
As far as copying the strings twice, i tried assigning the structure pointer ownership of the char* temp pointer, but when the temp pointer is realloc''ed, the value in the structure becomes garbage (if i remember correctly). The syntax i used was something like this (pseudo)
struct[0].charpointer=temp;
Like i said, later on the value at struct[0].charpointer becomes some interesting garbage. If i''m doing this assignment wrong, please do help.
i appreciate the help - i''m struggling to figure this file i/o stuff out on my own and i''m new to C/C++. Any recommendations as to how to do this differently (even completely different) are definitely welcome (including better ways from a performance perspective). Maybe i should just stick with the C++ way... it seems a heck of a lot easier. Unfortunately, i''m so stinkin'' anal that i HAVE to figure this out (grrrr...).
Thanks again for the help! Any more input is appreciated.
"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
Well .. it is kinda critical that when you "transfer" ownership .. you take it away from the original owener ... meaning it would be completely wrong to have temp do anything at all with the memory ... so you alloc it again ... not realloc it ... realloc REDIZES a piece of memory ... it does not allocate a totally new block unless it needs to, and if it does, it deletes the old one .. very bad for you ...
So here''s the overview ... alloc temp every time you read in something (using malloc or calloc) ... and then assign the pointer into the variable you wish to become the owner ... and then set temp to NULL ... then do the next one ... when you transfer ownership it is always a good practice to set the non-owner to NULL so you don''t accidentally do something you didn''t mean to - like realloc the memory![](smile.gif)
Also note ... you can just alloc the block right into the correct owner and then read it directly there if you want ... but that''s not really a big issue .. since a single pointer copy is almost zero work - and it makes it obvious to you which one your working with.
I really think you have a somwhat weak understanding of dynamic memory ... not real bad, I just think it probably isn''t second nature to you ... and you could really benifit by spending 2-3 days of intense study and experiment to get a more solid grasp of the way it all works ... Here''s a few things to try ... 1) try using some dynamically allocated 2 dimensional arrays ... they are kinda tough at first. 2) rewrite you structures with constructors and destructors ... the data member can stay public for now ... but Add get and set functions for the members which use dynamic memory .. then the object can manage it''s members memory ... and when it''s destroyed it can delete them too. You will find that this is the main advantage of objects ... letting them manage the rules they have to follow ... so the client code can stay ignorant.
Good Luck.
So here''s the overview ... alloc temp every time you read in something (using malloc or calloc) ... and then assign the pointer into the variable you wish to become the owner ... and then set temp to NULL ... then do the next one ... when you transfer ownership it is always a good practice to set the non-owner to NULL so you don''t accidentally do something you didn''t mean to - like realloc the memory
![](smile.gif)
Also note ... you can just alloc the block right into the correct owner and then read it directly there if you want ... but that''s not really a big issue .. since a single pointer copy is almost zero work - and it makes it obvious to you which one your working with.
I really think you have a somwhat weak understanding of dynamic memory ... not real bad, I just think it probably isn''t second nature to you ... and you could really benifit by spending 2-3 days of intense study and experiment to get a more solid grasp of the way it all works ... Here''s a few things to try ... 1) try using some dynamically allocated 2 dimensional arrays ... they are kinda tough at first. 2) rewrite you structures with constructors and destructors ... the data member can stay public for now ... but Add get and set functions for the members which use dynamic memory .. then the object can manage it''s members memory ... and when it''s destroyed it can delete them too. You will find that this is the main advantage of objects ... letting them manage the rules they have to follow ... so the client code can stay ignorant.
Good Luck.
Thanks for the input... i switched over to simply using predetermined char arrays (b/c i got so frustrated).
The above code is not meant to represent anything efficient or well put-together. i am just trying to work with varying aspects of file i/o (since i just learned it like two days ago). You''re right in that i''ve got some room to learn as far as dynamic memory goes - i''ll try out your suggestions. Since i posted last on the above code, i revamped the whole thing and created a small program that i can cut and paste into my tile game engine. In doing so, i learned how to use linked lists, so now my stuff works a heck of a lot better and i feel much more confident about what i''m doing.
That''s good to know about realloc... i always thought it simply created a new chunk of memory... didn''t realize it destroyed it. Obviously, this is the first time i''ve used it.
Thank you so much for taking the time to help me... i know the above postings were a bit much to go through.
"You call him Dr. Grip, doll!"
The above code is not meant to represent anything efficient or well put-together. i am just trying to work with varying aspects of file i/o (since i just learned it like two days ago). You''re right in that i''ve got some room to learn as far as dynamic memory goes - i''ll try out your suggestions. Since i posted last on the above code, i revamped the whole thing and created a small program that i can cut and paste into my tile game engine. In doing so, i learned how to use linked lists, so now my stuff works a heck of a lot better and i feel much more confident about what i''m doing.
That''s good to know about realloc... i always thought it simply created a new chunk of memory... didn''t realize it destroyed it. Obviously, this is the first time i''ve used it.
Thank you so much for taking the time to help me... i know the above postings were a bit much to go through.
"You call him Dr. Grip, doll!"
"You call him Dr. Grip, doll!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement