views:

105

answers:

5

Alright, I'm guessing this is an easy question, so I'll take the knocks, but I'm not finding what I need on google or SO. I'd like to create an array in one place, and populate it inside a different function.

I define a function:

void someFunction(double results[])
{
    for (int i = 0; i<100; ++i)
    {
       for (int n = 0; n<16; ++n) //note this iteration limit
       {
           results[n] += i * n;
       }
    }
}

That's an approximation to what my code is doing, but regardless, shouldn't be running into any overflow or out of bounds issues or anything. I generate an array:

double result[16];
for(int i = 0; i<16; i++)
{
    result[i] = -1;
}

then I want to pass it to someFunction

someFunction(result);

When I set breakpoints and step through the code, upon entering someFunction, results is set to the same address as result, and the value there is -1.000000 as expected. However, when I start iterating through the loop, results[n] doesn't seem to resolve to *(results+n) or *(results+n*sizeof(double)), it just seems to resolve to *(results). What I end up with is that instead of populating my result array, I just get one value. What am I doing wrong?

EDIT Oh fun, I have a typo: it wasn't void someFunction(double results[]). It was:

void someFunction(double result[])...

So perhaps this is turning into a scoping question. If my double result[16] array is defined in a main.cpp, and someFunction is defined in a Utils.h file that's included by the main.cpp, does the result variable in someFunction then wreak havoc on the result array in main?

EDIT 2:

@gf, in the process of trying to reproduce this problem with a fresh project, the original project "magically" started working.

I don't know how to explain it, as nothing changed, but I'm pretty sure of what I saw - my original description of the issue was pretty clear, so I don't think I was hallucinating. I appreciate the time and answers...sorry for wasting your time. I'll update again if it happens again, but for the meantime, I think I'm in the clear. Thanks again.

+1  A: 
void someFunction(double results[])

should be exactly equivalent to

void someFunction(double *results)

Try using the alternative declaration and see if the problem persists.

Potatoswatter
Hm...just tried that, and it still seems to persist. I'm using VS2008, and one thing is that when I'm stepping through the loop, should hovering over `results[n]` force the debugger/variable viewer to resolve to `results[0]`, `results[1]`, etc? I thought it would do that, but it just shows `results[n] = ...`
awshepard
@awshepard: You can verify wether the hovering menu gives the wrong result by adding `results[n]` or say `results[2]` to the watch window explicitly.
Georg Fritzsche
@awshepard: No, because the language specifies that the first case is treated like the second, the debugger would be unlikely to specially treat the second as an array. I don't use Visual Studio but there should be an option to display the pointer as an array.
Potatoswatter
+1  A: 

To me it seems that your code should simply work.

I just tried this in g++ and worked fine. I guess your problem is elsewhere? have you tried the snipped you posted?

#include <iostream>

void someFunction(double results[])
{
    for (int i = 0; i<100; ++i)
    {
       for (int n = 0; n<16; ++n) //note this iteration limit
       {
           results[n] += i * n;
       }
    }
}

int main() 
{
  double result[16];
  for(int i = 0; i<16; i++)
  {
    result[i] = -1;
  }
  someFunction(result);
  for(int i = 0; i<16; i++)
    std::cerr << result[i] << " ";
  std::cerr << std::endl;  
}
baol
Yea, this works for me.
awshepard
+1  A: 

Have you perhaps double defined your results array in a couple places and then accidently refered to one copy in one place and another copy elsewhere? Perhaps the second is a pointer and not an array and that is why the debugger is confused?

Michael Dorgan
I think this relates to my scoping question above? Apparently this should work itself out (which is what I thought given c++ scoping rules), but yes, perhaps the debugger is confused?
awshepard
Perhaps. One never knows what the debugger is actually thinking. Can you typecast your array in the watch window to specifically tell it what the var is?
Michael Dorgan
+1  A: 

To ensure this problem doesn't occur, you should never use global variables like that. If you absolutely must have one, put it in a namespace for clarity.

DeadMG
+1  A: 

Just a point about the variable scope part of the question - there is no issue of variable scope here. result/results in your someFunction definition is a parameter -> it will take on the value passed in. There is no relation between variables in a called function and it's caller -> the variables in the caller function are unknown to the called function unless passed in. Also, variable scoping issues do not occur between routines in C++ because there are no nested routines. The following pieces of code would demonstrate scoping issues:

int i = 0;  
{  
    int i = 0;  
    i = 5; //changes the second i, not the first. 
    //The first is aliased by the second i defined first.  
}  
i = 5; //now changes the first i; the inner block is gone and so is its local i

so if C++ did have nested routines, this would cause variable scoping

void main()  
{  
    double results[16];  
    double blah[16];  
    doSomething(blah);  
    void doSomething(double * results)  
    {  
         //blah doing something here uses our parameter results, 
         //which refers to blah, but not to the results in the higher scope. 
         //The results in the higher scope is hidden.  
     }  
}
foxwoods