C++数组作为参数,编辑:现在包括变量范围

发布于 2024-08-30 04:19:47 字数 1495 浏览 5 评论 0原文

好吧,我猜这是一个简单的问题,所以我会接受敲门,但我没有在谷歌或SO上找到我需要的东西。我想在一个地方创建一个数组,并将其填充到另一个函数中。

我定义了一个函数:

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

这是我的代码正在执行的操作的近似值,但无论如何,不​​应遇到任何溢出或越界问题或任何问题。我生成一个数组:

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

然后我想将其传递给 someFunction

someFunction(result);

当我设置断点并单步执行代码时,输​​入 someFunction 后,results 是设置为与 result 相同的地址,并且该值与预期一样为 -1.000000。但是,当我开始迭代循环时, results[n] 似乎无法解析为 *(results+n)*(results+n *sizeof(double)),它似乎只是解析为*(results)。我最终得到的是,我只得到一个值,而不是填充结果数组。我做错了什么?

编辑 哦,有趣,我有一个拼写错误:它不是 void someFunction(double results[])。答案是:

void someFunction(double result[])...

所以也许这正在变成一个范围界定问题。如果我的 double result[16] 数组是在 main.cpp 中定义的,并且 someFunction 是在 main.cpp 包含的 Utils.h 文件中定义的,那么someFunction 中的 result 变量然后对 main 中的 result 数组造成严重破坏?

编辑2:

@gf,在尝试用新项目重现此问题的过程中,原始项目“神奇地”开始工作。

我不知道如何解释它,因为什么都没有改变,但我很确定我所看到的 - 我最初对问题的描述非常清楚,所以我不认为我出现了幻觉。我很感谢您的时间和答案...很抱歉浪费您的时间。如果再次发生这种情况,我会再次更新,但与此同时,我想我是清白的。再次感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

牵你手 2024-09-06 04:19:47

只是关于问题的变量范围部分的一点 - 这里不存在变量范围的问题。 someFunction 定义中的 result/results 是一个参数 ->它将采用传入的值。被调用函数中的变量与其调用者之间没有关系 ->除非传入,否则调用函数中的变量对于被调用函数来说是未知的。此外,C++ 中的例程之间不会出现变量作用域问题,因为没有嵌套例程。以下代码片段将演示作用域问题:

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

因此,如果 C++ 确实有嵌套例程,这将导致变量作用域

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

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.  
     }  
}
忱杏 2024-09-06 04:19:47
void someFunction(double results[])

应该完全等同于

void someFunction(double *results)

尝试使用替代声明并查看问题是否仍然存在。

void someFunction(double results[])

should be exactly equivalent to

void someFunction(double *results)

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

她比我温柔 2024-09-06 04:19:47

对我来说,你的代码似乎应该可以正常工作。

我刚刚在 g++ 中尝试过这个并且工作得很好。我猜你的问题在其他地方?你试过你发布的截图吗?

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

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;  
}
够运 2024-09-06 04:19:47

您是否可能在几个地方双重定义了结果数组,然后意外地在一个地方引用了一个副本,在其他地方引用了另一个副本?也许第二个是指针而不是数组,这就是调试器感到困惑的原因?

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?

时光倒影 2024-09-06 04:19:47

为了确保不会发生此问题,您永远不应该使用这样的全局变量。如果您绝对必须拥有一个,请将其放在命名空间中以保持清晰。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文