简体   繁体   English

如何在C函数内部复制结构? Memcpy

[英]How to copy structure inside the function in C? memcpy

I'm student, so sorry for a stupid question if it's actually is. 我是学生,很抱歉有一个愚蠢的问题。 Didn't finish C lang module yet :c 尚未完成C lang模块:c

So the memcpy(); 所以memcpy(); in core.c file doesn't work like it should. core.c文件中无法正常工作。 Or maybe it just works in the way I don't need. 也许它只是以我不需要的方式工作。 The question is help to find a solution for the following task: 问题是可以帮助您找到以下任务的解决方案:

The idea is to have a function multiply(); 这个想法是要有一个功能multiply(); which accepts nMatrix [4x4] and nVector [4] pointers. 它接受nMatrix [4x4]和nVector [4]指针。 It suppose to make buffer(tempVector) for getting multiplied results and then memcpy the whole structure to nVector . 它想使缓冲区(tempVector)用于获取相乘的结果,然后memcpy整体结构nVector。

But instead beautiful multiplied vector result it gives garbage from the memory. 但是取而代之的是美丽的乘向量结果,它从内存中产生了垃圾。 Also before go out of the scope I free the tempVector array to prevent any bad heap memory management. 同样在超出范围之前,我释放了tempVector数组以防止任何不良的堆内存管理。 Unfortunately the nVector array is still depends on copied memory from tempVector . 不幸的是, nVector数组仍然依赖于tempVector复制的内存。

I would appreciate any advice! 我将不胜感激任何建议! :D :D

So that is the problem code part - core.c 这就是问题代码的一部分-core.c

#include "core.h"
#include "init.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#define vecLength 4

typedef struct Vector {
double * vector;
int N;
} Vector;

typedef struct Matrix {
double ** matrix;
int nRow;
int nCol;
} Matrix;
...
...
void multiply(Vector* nVector, Matrix* nMatrix)
    {
    Vector tempVector;
    vectorInit(&tempVector);
    for(int i = 0; i < nMatrix->nRow; i++)
        for(int j = 0; j < nMatrix->nCol; j++)
            tempVector.vector[i]+= nMatrix->matrix[i][j]*nVector->vector[j];

    memcpy(nVector,&tempVector,sizeof(Vector));
    vectorFree(&tempVector);
    }

main.c main.c

int main(){
    ...
    ...
    Vector* v_Test;
    Matrix* m_Test;

    //Test structures memory allocation
    v_Test = (Vector*) malloc(sizeof(Vector));
    m_Test = (Matrix*) malloc(sizeof(Matrix));

    //Initialization of vector in structure
    vectorInit(v_Test);

    for(int i = 0; i < v_Test->N; i++)
        v_Test->vector[i]=i+1;

    //Print vector
    vectorPrint(v_Test);
    getch();

    //Initialization of matrix in structure
    matrixInit(m_Test);

    for(int i=0; i<m_Test->nCol; i++)
        for(int j=0; j<m_Test->nCol;j++)
            m_Test->matrix[i][j]=m_Test->nRow*i+j;


    //Print matrix
    matrixPrint(m_Test);
    getch();


    multiply(v_Test,m_Test);

    vectorPrint(v_Test);
    getch();


    vectorFree(v_Test);
    getch();

    matrixFree(m_Test);
    getch();
  }
...
...
}

Already found the solution by trying the last idea before upload. 已经通过尝试上载之前的最后一个想法找到了解决方案。 So when you call memcpy it just copies array pointer of the vector which will be deallocated in the next instruction vectorFree(); 因此,当您调用memcpy它仅复制向量的数组指针,该向量将在下一条指令vectorFree();释放vectorFree(); . So to prevent that we just copy the actual array from tempVector to nVector structure!* 因此,为防止这种情况,我们只是将实际数组从tempVector复制到nVector结构!*

Solution in code: 代码中的解决方案:

memcpy(nVector->vector,tempVector.vector,vecLength*sizeof(double)); memcpy(nVector-> vector,tempVector.vector,vecLength * sizeof(double));

I still upload it cause I paid 45 min to write this question. 我仍然上传它,因为我花了45分钟来写这个问题。 And I hope it will help anyone in the future :) 我希望它将对以后的任何人有所帮助:)

Stay cool guys and have a wonderful day :D 保持冷静,并度过美好的一天:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM