   what the hell
  i have a function that opens a file and takes that information and fills in a class. one of the members in teh class is an array of structs, called AnimationState. it has an xcellposition, ycellposition, frames, currentframe, and animation speed. however, when this function loaded everything up, and returned to the function taht called it, it would never return the value that is the first made in teh struct. for instance:struct AnimationState{int  XCellPosition;int  YCellPosition;int  frames;unsigned int   AnimationSpeed;int  CurrentFrame;};with this, when the sprite (the class) was passed back after creation, it would not copy the XCellPosition, and it would be filled with garbage. if this was the case:struct AnimationState{int  YCellPosition;int  XCellPosition;int  frames;unsigned int   AnimationSpeed;int  CurrentFrame;};the y cell position would not be passed back, and would be filled with garbage. if i added a decoy bool value at the top of that struct, to get the garbage buffer so to speak, it works, but i want to know why it does that because 4 animation states per sprite, and theres 400 sprites on the screen, thats a lot of space taken up that isnt even used.does anyone know why the first member gets filled with garbage when being returned from the function?ps: the cSprite class is declared and created in this function, would exiting the function screw it up? 
  I assume where you say "class" you mean "object".  Show us the code that fills up the structs... ;  ok.this is the function that loads the SDF File //LOADS THE SDF FILE///////////////cSprite cSpriteManager::LoadSDF(char* filename){//Creates Sprite//cSprite newSprite;////////////////////Creates the file stream//////ifstream in_file;in_file.open(filename);/////////////////////////////////Loads the Sprite info////////in_file >> newSprite.file;in_file >> newSprite.myWidth;in_file >> newSprite.myHeight;newSprite.mySurface = mySurfMan.AddSurface(newSprite.file);/////////////////////////////////Loads Animation info/////////in_file >> newSprite.myAnimation.AnimationStateCount;in_file >> newSprite.myAnimation.move_right;in_file >> newSprite.myAnimation.move_left;in_file >> newSprite.myAnimation.move_up;in_file >> newSprite.myAnimation.move_down;//creates arraynewSprite.myAnimation.AnimationStates = new AnimationState[newSprite.myAnimation.AnimationStateCount];//loop for loading animation statesfor(int x = 0; x < newSprite.myAnimation.AnimationStateCount; x++){in_file >> newSprite.myAnimation.AnimationStates[x].AnimationSpeed;in_file >> newSprite.myAnimation.AnimationStates[x].frames;in_file >> newSprite.myAnimation.AnimationStates[x].XCellPosition;in_file >> newSprite.myAnimation.AnimationStates[x].YCellPosition;newSprite.myAnimation.AnimationStates[x].CurrentFrame = 0;}//Sets current animation statenewSprite.myAnimation.CurrentAnimationState = 0;//Closes the file stream///////in_file.close();///////////////////////////////return newSprite;}///////////////////////////////////this is the function that it returns it to. when returning it to this function, whatever is declared first in the AnimationState struct is full of garbage, but not during the load function. //AddFile Function/////////////////cSprite cSpriteManager::AddSprite(char* filename){//Loops through to make sure it doesn''t already exsistfor(int x = 0; x < FileNames.size(); x++){if(FileNames[x] == filename){return Sprites[x];}}//Couldn''t find filename in the library, so add it////////Adds filename to directoryFileNames.resize(FileNames.size() + 1, filename);//Adds the Sprite to the directorySprites.resize(Sprites.size() + 1, LoadSDF(filename));////////////////////////////////////////////////////////return Sprites[Sprites.size()-1];}///////////////////////////////////;  anyone? im dying here! ;  Maybe cSprite''s copy constructor is messed up. ;  I am not that familiar with ifstream, but shouldn''tin_file >> newSprite.myWidth;in_file >> newSprite.myHeight;  be:in_file >> &newSprite.myWidthin_file >> &newSprite.myHeight  I.e. place the data in to the address of myWidth, etc.Luck,Feral ;  that shouldnt be the problem because all of the other inputs work fine, its only that one that is getting screwed up.there is no copy constructor. there is no copying being done.the function returns it into this lineSprites.resize(Sprites.size() + 1, LoadSDF(filename));since LoadSDF returns a cSprite, it adds it to the vector. ;quote:cSprite cSpriteManager::LoadSDF(char* filename){cSprite newSprite;//[snip]   return newSprite;}you create a staticly allocated cSprite, and then returns it - im supprised that you compiler lets you get away with it... newSprite ONLY exists within the scope of LoadSDF. What you need to do is allocate it dynamicly ie with new. Im pretty shure that takes care of your "garbage" problem ass well. /Please excuse my bad spelling - My native language is binary not english|Visit me\Take my advice - I don''''t use it... ;  Make thisloading method a member of your CSprite class. It will make life easier ;quote:Original post by guppy  cSprite cSpriteManager::LoadSDF(char* filename){cSprite newSprite;//[snip]   return newSprite;}you create a staticly allocated cSprite, and then returns it - im supprised that you compiler lets you get away with it... newSprite ONLY exists within the scope of LoadSDF. What you need to do is allocate it dynamicly ie with new. Im pretty shure that takes care of your "garbage" problem ass well.Um, no.  He''s returning the object by value, this is completely fine C++ (if not very optimized) because the compiler returns a local copy (i.e. it calls the copy constructor on a new object).  However, the problem is in your copy constructor (or lack thereof) for cSprite.You have to be very careful:  If you don''t implement a copy constructor the compiler creates one for you and tries to do the best it can by copying each member variable to the new one.  So if you have: class cSprite {  int arraySize;  double* pArray;public:  cSprite() : arraySize(10) {    pArray = new double[arraySize];  }  ~cSprite() {    delete[] pArray;  }};and you don''t supply a copy constructor, the copy constructor created for you does this: cSprite::cSprite(const cSprite& o) {  this->arraySize = o.arraySize;  this->pArray = o.pArray;}The problem here is in the pArray copy.  It is not a DEEP copy, in the sense that the compiler only copies the pointer value, it does not allocate an array for the new object and copy element-by-element into the new array.  So when you do this:cSprite makeSprite() {  cSprite newSprite;  return newSprite;}and call it:void main() {  theSprite = makeSprite();}Once makeSprite returns, newSprite is destroyed so it makes a copy and assigns it to theSprite.  However, the memory pointed to by pArray has been deleted by cSprite''s destructor when makeSprite() finishes and newSprite goes out of scope.What you SHOULD do is explicitly create a copy constructor like so: void cSprite::deepcopy(const cSprite& o) {  this->arraySize = o.arraySize;  this->pArray = new double[this->arraySize];  for(int loop = 0; loop < this->arraySize; ++loop) {    this->pArray[loop] = o.pArray[loop];  }}cSprite::cSprite(const cSprite& o) {  deepcopy(o);}cSprite& cSprite::operator=(const cSprite& o) {  deepcopy(o);  return *this;}In this way you ensure that any time a cSprite object is copied or assigned it is getting a full, deep copy instead of a shallow copy.Regards,Jeff 