My friend didn't want to post this himself so I'm doing it for him.

The creatures.size() returns wrong value! I don't know why i push back only two objects and sometimes its returns things like -89252... I'm using Mingw-g++ 4.7.2 . I tested it also on my linux and the results are always the same. Please help me!

http://pastebin.com/G1R0NFCQ

Your program doesn't compile correctly, until you fix the errors it will produce undefined behavior.

You need to add = symbol in two places, like this:

creature human  = // <<<< add the = symbol here
{
    100,

I can't compile the rest of the program because you need to post "bresenham.h"

Your problem is on line 225 of gen_map. It's quite a subtle problem that is causing a stack corruption. You need to change map_g[yt][xt][1] = 1; to map_g[yt][xt][0] = 1;, there's only one unit of z-dimension, so you can't access element 1 in this dimension.

The explanation of why you get the strange value for the size of createures for this is a little convoluted...

When you declare variables in C++ they're pushed onto a stack, which is just a series of memory locations. In your case you have declared a static array (map_g, which is 80000 bytes in size(!)) on this stack. Another feature of the stack is that variables are pushed onto it in the order that they're encountered in the program, so immediately after map_g is pushed on, creatures is pushed onto the stack too. creatures is a std::vector, which is probably implemented by your STL version as a pair of pointers - one to the start of the vector and one to (one-past) the end of the vector. Don't forget that this pair of values immediately follows the memory allocated to map_g on the stack. So, when gen_map goes through map_g and assigns the value 1 to all its elements, you were actually out by 1 the whole way. This means that when you come to fill in the final element of the array, you actually over-write the first two bytes (the size of a short int) of the pointer that points to the first element of creatures. Now, when you call creatures.size() this function goes and tries to work out the size by subtracting the value of the end pointer from the value of the start pointer (which has been rubbished by your gen_map function), so you get a garbage value for the size.

Hope that helps.

In general: don't use global variables, don't use large static arrays, do use std::vector

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.