开发者

C++ arrays as parameters, EDIT: now includes variable scoping

开发者 https://www.devze.com 2022-12-28 04:26 出处:网络
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 differen

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.


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.  
     }  
}


void someFunction(double results[])

should be exactly equivalent to

void someFunction(double *results)

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


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;  
}


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消