0

I'm creating a new object of my class 'Dynamic' (not shown), which inheritates from 'Organic', which inheritates from 'Being' certain parameters such as id, biomeRow, etc.

Organic has: features_ (a struct), max_spawn_, total_spawn_, age_dur_ (an array) and current_age_.

The problem: Upon creating a Dynamic object, all values are set just right except max_spawn_. I've done my printfs both before creating Dynamic, in the creation of Dynamic and in the creation of Organic for the input value, and all of them are correct.

Features struct is right, total_spawn_ is right, age_dur_ array and current_age_ are both also right.

All of them are what I asked except for max_spawn_. maxSpawn is the value I'm passing (20), max_spawn_ should then be 20, but it isn't. All my printfs and debugging console show it is something around -858993460. I'm guessing that's just garbage, but I don't know how is it possible when all I'm doing is:

max_spawn_ = maxSpawn;

So, this is my function:

Organic::Organic(int id, int biomeRow, int biomeColumn, int biomeType, int beingType,
        int object, Features features, int maxSpawn, int totalSpawn,
        int age_dur[5], int current_age)
    : Being(id, biomeRow, biomeColumn, biomeType, beingType, object)
{
    features_ = features;
    max_spawn_ = maxSpawn;
    total_spawn_ = totalSpawn;

    age_ = current_age;
    for (int i = 0; i <= 5; i++) 
            age_dur_[i] = age_dur[i];

    printf("\n%d\n", max_spawn_);
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Darkatom
  • 1
  • 2
  • 1
    Output the value in hex, is it a http://en.wikipedia.org/wiki/Magic_number_%28programming%29 ? Anyway you have a very bad style of naming your members and parameters. Also relevant: http://stackoverflow.com/help/mcve , also: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Samuel Dec 30 '14 at 15:35
  • maybe something in the call of `Organic::Organic()`? Since that part of the code is not included, it's hard to guess, but things could go wrong there. – kratenko Dec 30 '14 at 15:35
  • 1
    It would be interesting to see your actual class definition. Is `max_spawn_` anywhere near `age_dur_`? You're overrunning `age_dur_` if it's an `int [5]`. (Your loop should have `i < 5` as its conditional, not `i <= 5`). – Paul Roub Dec 30 '14 at 15:39
  • Compile with all warnings & debug info (e.g. `g++ -Wall -Wextra -g`). **Use the debugger** (`gdb`) notably watchpoints – Basile Starynkevitch Dec 30 '14 at 15:42
  • @Paul Roub: Thank you! That was exactly the problem. max_spawn_ was next to age_dur_, I didn't notice the <=, so it was being overwritten. I should be more careful with that. – Darkatom Dec 30 '14 at 15:44
  • @PaulRoub As your comment has solved the problem, can you post it as an answer? – Joseph Mansfield Dec 30 '14 at 15:49
  • -858993460 = 0xCCCCCCCC -> Uninitialized stack memory. http://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations/127404#127404 – drescherjm Dec 30 '14 at 16:29

1 Answers1

3

age_dur (and presumably age_dur_) are int [5] arrays. Copying like this:

for (int i = 0; i <= 5; i++) 
        age_dur_[i] = age_dur[i];

will overwrite something near age_dur_ with something. If max_spawn_ is adjacent to age_dur_, it's probably being overwritten with garbage.

Change the loop to:

for (int i = 0; i < 5; i++) 
  age_dur_[i] = age_dur[i];
Paul Roub
  • 36,322
  • 27
  • 84
  • 93