C++ 调用函数后数组中的垃圾

C++ 调用函数后数组中的垃圾,c++,function,pointers,matrix,matrix-multiplication,C++,Function,Pointers,Matrix,Matrix Multiplication,问题出现在foo()(注释行中),问题是foo2()应该在其第一个参数中返回矩阵乘法重复过程的结果。它在第一种情况下起作用,然后就失败了 B和B_tmp数组在foo()的末尾应该具有相同的值,但这不会发生 T是1x6矩阵,A是6x3矩阵,B是200x3矩阵 foo3()乘以TxA并将结果(1x3矩阵)存储在B foo2()在开始时对B_t1_t2所做的事情并不相关,它只是准备1x6矩阵,以某种方式改变顺序 我必须尝试在不更改任何函数声明的情况下解决此问题 我是C++新手,现在搜索的时间太长了,我

问题出现在
foo()
(注释行中),问题是
foo2()
应该在其第一个参数中返回矩阵乘法重复过程的结果。它在第一种情况下起作用,然后就失败了

B
B_tmp
数组在
foo()的末尾应该具有相同的值,但这不会发生

T
是1x6矩阵,
A
是6x3矩阵,
B
是200x3矩阵

foo3()
乘以TxA并将结果(1x3矩阵)存储在
B

foo2()
在开始时对B_t1_t2所做的事情并不相关,它只是准备1x6矩阵,以某种方式改变顺序

我必须尝试在不更改任何函数声明的情况下解决此问题 我是C++新手,现在搜索的时间太长了,我很绝望。
#include <stdio.h>
#include <iostream>
#include <random>
#include <thread>

using namespace std;

double fRand(const double & min, const double & max) {
    thread_local std::mt19937 generator(std::random_device{}());
    std::uniform_real_distribution<double> distribution(min, max);
    return distribution(generator);
}

int iRand(const int & min, const int & max) {
    thread_local std::mt19937 generator(std::random_device{}());
    std::uniform_int_distribution<int> distribution(min, max);
    return distribution(generator);
} 

void foo3(double T[6], double A[18], double *B)
{
    for(int i = 0; i < 3; i++) {
        double r = 0;
        for(int j = 0; j < 6; j++) {
            r += T[j] * A[i*3+j];
        }
        *B = r; B++;
    }
}

void foo2(double *B, double *A, int from, int to)
{
    for (int i=from; i < to; i++) {         //This is not relevant but I leave it just in case
        double B_t1_t2[6];
        for (int x = 0; x < 3; x++)
            B_t1_t2[x] = B[(i-1)*3 + x];
        for (int x = 0; x < 3; x++)
            B_t1_t2[x+3] = B[(i-2)*3 + x];

        foo3(B_t1_t2, A, &B[i*3]);
    }
}


void foo(double *A, double *B)
{   
    for (int i = 0; i < 18; i++)
        A[i] = fRand(1, 2);

    foo2(B, A, 2, 200);
    cout << "\nB" << endl;
    for (int i = 0; i < 600; i++)
        cout << B[i] << " "; // HERE IS WORKING, B DOES NOT CONTAIN GARBAGE
    cout << endl;

    double B_tmp[600];
    foo2(B_tmp, A, 2, 200);
    cout << "\nB_tmp" << endl;
    for (int i = 0; i < 600; i++)
        cout << B_tmp[i] << " "; // WHY NOT WORKING HERE?
    cout << endl;
}

int main()
{
    double A[18], B[600];

    for(int i = 0; i<6; i++)
        B[i] = 1;

    foo(A, B);
}
#包括
#包括
#包括
#包括
使用名称空间std;
双法兰(恒双和最小、恒双和最大){
线程_本地std::mt19937生成器(std::random_设备{}());
标准:均匀实分布(最小值、最大值);
回流分配(发电机);
}
int iRand(常量int&min、常量int&max){
线程_本地std::mt19937生成器(std::random_设备{}());
标准:均匀分布(最小值、最大值);
回流分配(发电机);
} 
void foo3(双T[6],双A[18],双*B)
{
对于(int i=0;i<3;i++){
双r=0;
对于(int j=0;j<6;j++){
r+=T[j]*A[i*3+j];
}
*B=r;B++;
}
}
void foo2(双*B,双*A,int-from,int-to)
{
对于(inti=from;ifoo(A,B)之前;
填充了B数组的前6个元素(全部设置为1)。在
foo
函数中,您调用
foo2
函数两次。在第一次调用中,您将
B
数组传递到
foo2
函数中,因为
B
已填充,所以它可以工作。在
foo
中的
foo2
的第二次调用中,您传递了
B_tmp
数组,但该数组的所有项都有垃圾值,您没有输入初始化它们,也要初始化它们

double B_tmp[600];
for (int i = 0; i < 6; ++i)
    B_tmp[i] = 1;
foo2(B_tmp, A, 2, 200);
double B_tmp[600];
对于(int i=0;i<6;++i)
B_tmp[i]=1;
foo2(B_tmp,A,2200);

将函数和变量标记为不同于自由文本的代码