C++函数问题中的数组分配
C++ Array Assignment in Function Issue
C++这里的新手,我遇到了一个我无法弄清楚的问题。
当我运行下面的代码时,我得到了我期望的正确结果:
#include <iostream>
#include <math.h>
void factorize(int *factors, int number)
{
// declare vars
int index = 0;
// find factors of number
for (int x = 1; x <= number; ++x) {
// if x is a factor of number, save it to array
if (number % x == 0) {
std::cout << " Index: " << index << ", F: " << x << "n";
index++;
}
}
}
int main()
{
//declare vars
int triangle = 0;
int factors[] = {};
//loop through all numbers
for (int x = 1; x <= 5; ++x) {
// calculate triangle value
std::cout << "initial triangle value: " << triangle << ", ";
triangle = triangle + x;
std::cout << "x value: " << x << " , new triangle value: " << triangle << "nn";
// find factors of triangle number
factorize(factors, triangle);
std::cout<<"n";
}
return 0;
}
正确结果:
initial triangle value: 0, x value: 1 , new triangle value: 1
Index: 0, F: 1
initial triangle value: 1, x value: 2 , new triangle value: 3
Index: 0, F: 1
Index: 1, F: 3
initial triangle value: 3, x value: 3 , new triangle value: 6
Index: 0, F: 1
Index: 1, F: 2
Index: 2, F: 3
Index: 3, F: 6
initial triangle value: 6, x value: 4 , new triangle value: 10
Index: 0, F: 1
Index: 1, F: 2
Index: 2, F: 5
Index: 3, F: 10
initial triangle value: 10, x value: 5 , new triangle value: 15
Index: 0, F: 1
Index: 1, F: 3
Index: 2, F: 5
Index: 3, F: 15
但是当我将线因子[index] = x;添加到因式分解函数中时,我得到了奇怪的结果。出于某种原因,主三角形的值被修改了。
#include <iostream>
#include <math.h>
void factorize(int *factors, int number)
{
// declare vars
int index = 0;
// find factors of number
for (int x = 1; x <= number; ++x) {
// if x is a factor of number, save it to array
if (number % x == 0) {
std::cout << " Index: " << index << ", F: " << x << "n";
factors[index] = x;
index++;
}
}
}
int main()
{
//declare vars
int triangle = 0;
int factors[] = {};
//loop through all numbers
for (int x = 1; x <= 5; ++x) {
// calculate triangle value
std::cout << "initial triangle value: " << triangle << ", ";
triangle = triangle + x;
std::cout << "x value: " << x << " , new triangle value: " << triangle << "nn";
// find factors of triangle number
factorize(factors, triangle);
std::cout<<"n";
}
return 0;
}
意外结果(注意三角形值是如何变化的:0、1、3,但由于某种原因跳回 2(:
initial triangle value: 0, x value: 1 , new triangle value: 1
Index: 0, F: 1
initial triangle value: 1, x value: 2 , new triangle value: 3
Index: 0, F: 1
Index: 1, F: 3
initial triangle value: 3, x value: 3 , new triangle value: 6
Index: 0, F: 1
Index: 1, F: 2
Index: 2, F: 3
Index: 3, F: 6
initial triangle value: 2, x value: 4 , new triangle value: 6
Index: 0, F: 1
Index: 1, F: 2
Index: 2, F: 3
Index: 3, F: 6
initial triangle value: 2, x value: 5 , new triangle value: 7
Index: 0, F: 1
Index: 1, F: 7
我错过了什么?
定义
int factors[] = {};
将factors
定义为空的int
数组。它的每次索引都将超出界限并导致未定义的行为。第一个程序有效是因为您实际上没有使用factors
。
如果你想在C++中使用一个动态"数组",你应该使用 std::vector
.
如果您事先知道大小,请使用该大小或使用 std::array
.
相关文章:
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 如何通过 malloc 为队列数组分配内存?
- 具有动态数组分配的OpenMP嵌套循环
- 如何为数组结构的数组分配内存
- 如何为结构数组分配内存?
- cpp 数组 - 分配常量索引有效,而非常量索引不起作用
- 如何为字符指针数组分配内存?
- 在 C++ 中初始化后的数组分配
- 将数据分配给了数组分配,但程序显示了以缓冲区超支的输出
- C++函数问题中的数组分配
- 子数组分配:使用聚合对象错误的预期"{...}"进行初始化
- 为什么我的指针数组分配不起作用
- std::堆栈和堆栈溢出错误的数组分配
- 在 c++ 中是否有通过声明"implicit"数组分配?
- 在 c++ 中为 2-D "Dynamically Created"数组分配值时遇到问题
- 如何为C++数组分配指向第一个元素的指针
- 字符数组分配
- 将数组分配在固定数量的线程之间
- 为构造函数中的数组分配值