-4

For some reason the function doesn't save member values for an array of objects except the first one. I condensed the code to highlight the nature of this problem.

classFile.cpp

myClass objectArray[10];

void myClass::Set(float x, int elementID)
{
    myX = x;
    log<< myX; //output is equal to x
    log<< elementID; //output ranges from 0-9
    log<< objectArray[elementID].myX; //output is incorrect if elementID is higher than 0
}

classFile.h

extern myClass objectArray[10];

callingFunctionFile.cpp

for(int i=0; i<10;i++)
{
   objectArray[i].Set(5.0f, i);
   i++;
}

The incorrect output of objectArray[elementID].myX is always the same for specific elementID but differs between other elementIDs. Sometimes it's 0, sometimes it's something like 8231924021

Edit: Here's the original code with relevant parts written at the top (in case if you notice some nuances cause it's just too long to actually go through it): classFile.h, classFile.cpp, functionCallingFile.cpp

Edit2: It looks like the position of the class members is shifted in the memory 3 bytes per each object and that makes it impossible to read them except the first one. Here's the comparision of the myX addresses and objectArray[i].myX addresses - pastebinLink

I could just counter this 3 bytes shift manually but it's like spraying deodorant into the toiled instead of flushing it.

Edit3: What solved the problem was changing the position of #include "classFile.h" inside callingFunctionFile.cpp file. After placing it at the top as the first include the problem disappeared

michalmonday
  • 441
  • 5
  • 11
  • 2
    @JoachimPileborg Not necessarily, right? It is modifying objects in an array that access the same array they are stored in. – juanchopanza Dec 13 '15 at 13:48
  • 1
    I think you need to show us the entire contents of classFile.h and callingFunctionFile.cpp. I suspect that you have got a strange difference between the definition of objectArray in callingFunctionFile and classFile – Martin Bonner supports Monica Dec 13 '15 at 13:48
  • @Captain Obvilous Hmm, it looks like there is no copy of it. @ JoachimPileborg myX is a member of myClass, objectArray[i] is the object of the same class. Shouldn't it save the value for each object when used within myClass function? @ Martin Bonner I'm affraid it's a bit too lengthy but I'll edit the question in a minute with some most relevant part of it – michalmonday Dec 13 '15 at 13:56
  • @MartinBonner Actually when I'm looking at it now there's nothing more seemingly relevant I could add, the only reference to the objectArray in callingFunctionFile.cpp is what I posted in question + #include "classFile.h". objectArray[i].Set(5.0f, i) is called successfuly because the "log" output is correct (xPos, elementID). – michalmonday Dec 13 '15 at 14:36
  • Here's the original code with relevant parts written at the top (in case if you notice some nuances cause it's just too long to actually go through it): classFile.h [link](http://pastebin.com/1rn8RAZ4) classFile.cpp [link](http://pastebin.com/DmcCczA7) functionCallingFile.cpp [link](http://pastebin.com/PxVF1LvZ) – michalmonday Dec 13 '15 at 14:38
  • Try to output address of `objectArray[elementID]` and `this` value in `myClass::Set` and see if they are the same address – Revolver_Ocelot Dec 13 '15 at 15:13
  • @Revolver_Ocelot [outputLink](http://pastebin.com/WMpezi2k) It looks like it shifts the address 3 bytes per each additional object – michalmonday Dec 13 '15 at 15:44
  • @monday , did you change anything in header recently? It looks like you added/removed some data in `Cursor` definition and some files were rebuilt and some not. Try to do a clean build. – Revolver_Ocelot Dec 13 '15 at 15:54
  • @Revolver_Ocelot: Wouldn't be at all surprised if that is the problem! – Martin Bonner supports Monica Dec 13 '15 at 16:00
  • @Revolver_Ocelot I just included neccessary header files to functions' cpp file + included functions header file to the file where I'm calling it. After clean build the problem still occurs – michalmonday Dec 13 '15 at 16:05
  • Thanks to all for help @Revolver_Ocelot, big thanks, moving classFile.h include in callingFunctionFile.cpp to the top solved the problem – michalmonday Dec 13 '15 at 21:36

1 Answers1

2

You are incrementing i twice in the loop, making you skip every other element in the array.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • It would not lead to undefined behavior. Notice, that bounds check is peformed right before access. It will still skip every second index, but it would not break anything. – Revolver_Ocelot Dec 13 '15 at 14:16
  • @Revolver_Ocelot True, update my answer. – Some programmer dude Dec 13 '15 at 14:17
  • Hmm.. sorry that's my mistake while condensing the code to present it here – michalmonday Dec 13 '15 at 14:35
  • @monday If it's not in the actual code, then you should edit your question to reflect the actual code. Unrelated errors distracts from the actual errors. And please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. The code you show us is not enough other than to make wild (and mostly wrong) guesses. – Some programmer dude Dec 13 '15 at 15:23