tags:

views:

46

answers:

2

In OSX I have the following code. Using gcc 4.0.1. I'm new to OSX development so I'm not sure what other system information would be useful here...

static int     textstrArgs[] = { 1, 1, 1 };

void func()
{
    static int first = 1;
    if (first)
    {
        first = 0;
        // stuff
    }
    /* other stuff */
}

where func() is declared as 'extern' and is called from another library.

The problem is that the address of 'texstrArgs[2]' and 'first' is the same. That is, when the application loads it's putting both of these variables at the same spot in memory. When func() is called the first = 0 is clobbering the value in the static textstrArgs array.

Would could I be doing that would cause this to occur?

Thanks for any help anyone can give.

A: 

Just a hunch - try changing:

static int textstrArgs[] = { 1, 1, 1 };

to

static int textstrArgs[3] = { 1, 1, 1 };

One other thing - are you actually referencing textstrArgs anywhere ? If not then it may be that it's getting optimised away.

Paul R
Ok ya that pointed me to the issue. The wrong static variable was actually getting used, but every value was usually '1', so things usually worked.No compiler bug, just programmer error.
Malcolmb
+1  A: 

I doubt that they are actually sharing the address. I believe it's more likely that you are accessing the array out of bounds or something similar.

Try printing the address of both variables. That will show you if your suspicion is correct.

Zan Lynx