Random Noob
Random Noob

Reputation: 45

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

So the memcpy(); in core.c file doesn't work like it should. 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(); which accepts nMatrix [4x4] and nVector [4] pointers. It suppose to make buffer(tempVector) for getting multiplied results and then memcpy the whole structure to 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. Unfortunately the nVector array is still depends on copied memory from tempVector.

I would appreciate any advice! :D

So that is the problem code part - 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

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();
  }
...
...
}

Upvotes: 0

Views: 402

Answers (1)

Random Noob
Random Noob

Reputation: 45

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();. So to prevent that we just copy the actual array from tempVector to nVector structure!*

Solution in code:

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

I still upload it cause I paid 45 min to write this question. And I hope it will help anyone in the future :)

Stay cool guys and have a wonderful day :D

Upvotes: 1

Related Questions