variable error?

I’m just a beginner in OF and C++.
I’m experiencing a weird error that I’ve never seen before.

I made a class named, let’s say, cat.

class Cat
void update();
void render();

float x;
float y;
int totalNum;
float a1, a2, a3, a4, a5…;


something like that.
basically what I’m doing is just calling update() and displaying images with render().
I declared bunch of float variables.
it works fine, unless I add more variables.
as I declare one more int or float variable( bool is fine ),
it starts to not showing any images.( I’m not doing anything else )
is there any kind of limitation in number of variables?
( I have 8 integer variables and 50 float variables. )



there’s no limit for variables. the error must be somewhere else. maybe if you post update() & render() we can help.


Hey Young!

We see this quite a lot actually when people haven’t given their variables default values. When you list your variables in your class they are allocated an address in memory. If you don’t set the values of your variables they could be anything!

Adding a new variable to your class can shift around where in memory your variables are sitting so this can make things seem really screwy.

The best practice is to always give your variables a default value in your constructor. So when the object is created all the variables are set to sensible values.

You can try:

class Cat   
Cat(); //the constructor  
void update();   
void render();   
float	 x;   
float	 y;   
int	 totalNum;   
float a1, a2, a3, a4, a5.....;   

Then in your cpp code

       x = 0;  
       y = 0;  
       totalNum = 0;  
       a1 = 0;  
       a2 = 0;    

what compiler are you on?

you might try “clean” or “clean all” which will remove object code. sometime it happens that objects (ie, object A) are compiled against the size of another object (object B). when you alter B, for whatever reason, A might not get updated (usually it does, but on some compilers, like devcpp, it doesn’t work very well w/ this) causing issues – the best thing to do, when you see voodoo like that is to first try clean all, so that you know that every object is recompiled with the new values.

theo is completely right on, this is *huge* problem that most people get wrong, esp new to c++, since java sets an inital value on all variables – I thought I’d try to make a list of things to watch out for that cause memory issues:

a) variables not initialized (big error ! most common!)
b) clean all / object dependency getting messed up
c) over stepping the boundary of an array
d) multiple #defines that are conflicting (ie two of the same defines in two different h files)
e) unallocated object beings called

others: can you think of other common memory errors?

hope that helps!!

If you use initialization lists, the compiler can optimize things for you and generally speaking, your constructor will run faster. Here’s the syntax:

     :  x(0),  

Of course you can’t do this with arrays although you can call static functions and object constructors.

thanks all!

I actually initiated variables.
but it didn’t work.
I’m using xcode on mac os x,
I think the reason was build configuration setting.
I set it to release, and as I changed it to debug, it works.( but slightly different ).
I still can’t figure out why ‘release’ and ‘debug’ make different results.

what I’m doing in void update() and void render() is like…
setting up values in update() and draw them in render().

void Cat::update()

angle_acc_rope = (-1*0.3/radius_rope) * sin(angle_rope);
angle_speed_rope += angle_acc_rope;
angle_speed_rope *= 0.97;
angle_rope += angle_speed_rope;


void Cat::render(float _mx, float _my){

ofEllipse(ankleL_x, ankleL_y, 2, 2);
ofEllipse(ankleR_x, ankleR_y, 2, 2);

ofEllipse(lapL_x, lapL_y, 2, 2);
ofEllipse(lapR_x, lapR_y, 2, 2);


glVertex2f(640/2, 0);
glVertex2f(rope_x, rope_y-10);

glVertex2f(ankleL_x, ankleL_y);
glVertex2f(rope_x, rope_y-10);

thank you


hi young,

can you zip the whole code (ie, src folder and the data folder if you use it) ?

also, if you put code in your posts, can you use the “code” tag, it allows you to past formatted code in there, like this:

for (int i = 0; i < 20; i++){  
    // ...  


for (int i = 0; i < 20; i++){
// …


I zipped src folder and uploaded here.

the problem is occurring in victim.h and victim.cpp.
but because I’m using opencv addon and pure opencv at the same time,
I’m not sure if you can compile it on your machine.

thank you so much!!


release-builds can be much faster than debug-builds. today i tested my app and it was around 20-30% faster in release-mode (also xcode & mac).
so if your code is quite sensible to performance-issues this might change a lot.


debug vs. release often changes the memory layout (ie, where variables are allocated, etc) – this is another argument for theo’s theory. would make sense that one would work and not the other, if a variable isn’t initialized, etc.

does your app crash at the start? if you put printfs in setup() or draw() do you get to them at all?

take care,

I think you were right.
there were some variables I forgot to initiate…
still getting weird errors but at least it’s drawing something!

thank you all.