无法将元素分配给 C 中动态分配的二维数组

发布于 2025-01-09 21:16:40 字数 2653 浏览 1 评论 0原文

希望每个人都做得很好。

我试图通过创建一个矩阵结构,然后使用其内存地址来执行操作,用 C 语言编写一个简单的矩阵库。

这是我的库的头文件:

/*
To compile:

g++ -c simpMat.cpp
ar rvs simpMat.a simpMat.o
g++ test_simpMat.c simpMat.a
*/

#ifndef SIMPMATH_H
#define SIMPMAT_H

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct{
    uint8_t nRows;
    uint8_t nCols;
    uint8_t nElements;
    float **elements;
}simpMat;

/**
*@brief simpMat_Init
*@param simpMat
*@param uint8_t
*@param uint8_t
*@param uint8_t
*@param float* 
*@retval NONE
*/
void simpMat_Init(simpMat *Matrix, uint8_t nRows, uint8_t nColumns, uint8_t nElements, float elements[]);

/**
*@brief simpMat_Print
*@param simpMat
*@retval NONE
*/
void simpMat_Print(simpMat *Matrix);

/**
*@brief simpMat_Delete
*@param simpMat
*@retval NONE
*/
void simpMat_Delete(simpMat *Matrix);

#endif

这是源文件:

#include "simpMat.h"

void simpMat_Init(simpMat *Matrix, uint8_t nRows, uint8_t nColumns, uint8_t nElements, float elements[])
{
    Matrix->nRows = nRows;
    Matrix->nCols = nColumns;
    Matrix->nElements = nElements;

    Matrix->elements = (float**)malloc(nRows * sizeof(float*));
    for (uint8_t i = 0; i < nRows; i++)
    {
        Matrix->elements[i] = (float*)malloc(nColumns * sizeof(float));
    }
    
    uint8_t count = 0;

    for (uint8_t i = 0; i < nRows; i++)
    {
        for (uint8_t j = 0; j < nColumns; j++)
        {
            Matrix->elements[i][j] = elements[count];
            count++;
        }
    }      
        
}

void simpMat_Print(simpMat *Matrix)
{
    for (uint8_t i = 0; i < Matrix->nRows; i++)
    {
        for (uint8_t j = 0; j < Matrix->nCols; j++)
        {
            printf("%d ", Matrix->elements[i][j]);

        }

        printf("\n");
    }
}


void simpMat_Delete(simpMat *Matrix)
{
    uint8_t n = Matrix->nRows;
    while(n) free(Matrix->elements[--n]);
    free(Matrix->elements);
}

我还编写了一个小测试程序,看看是否可以成功地将元素分配给矩阵;例如:

#include "simpMat.h"
#include "stdio.h"

int main()
{
    simpMat Matrix1;

    float toAppend[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

    simpMat_Init(&Matrix1, 3, 2, 9, toAppend);

    printf("MATRIX ELEMENTS ARE:\n");

    simpMat_Print(&Matrix1);

    simpMat_Delete(&Matrix1);

    return 0;

}

我在 CMD 上使用以下命令编译了我的库和主程序:

g++ -c simpMat.cpp
ar rvs simpMat.a simpMat.o
g++ test_simpMat.c simpMat.a

但是,当我运行可执行文件时,我得到以下输出:

MATRIX ELEMENTS ARE:
0 0
0 0
0 0

我无法理解无法分配值的原因。我对动态内存分配主题相当陌生,我怀疑我对该方法有误解。你能帮我吗?

Hope everyone is doing well.

I am trying to write a simple Matrix Library in C by creating a Matrix struct, and then using its memory address to execute operations.

Here is my header file for the library:

/*
To compile:

g++ -c simpMat.cpp
ar rvs simpMat.a simpMat.o
g++ test_simpMat.c simpMat.a
*/

#ifndef SIMPMATH_H
#define SIMPMAT_H

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct{
    uint8_t nRows;
    uint8_t nCols;
    uint8_t nElements;
    float **elements;
}simpMat;

/**
*@brief simpMat_Init
*@param simpMat
*@param uint8_t
*@param uint8_t
*@param uint8_t
*@param float* 
*@retval NONE
*/
void simpMat_Init(simpMat *Matrix, uint8_t nRows, uint8_t nColumns, uint8_t nElements, float elements[]);

/**
*@brief simpMat_Print
*@param simpMat
*@retval NONE
*/
void simpMat_Print(simpMat *Matrix);

/**
*@brief simpMat_Delete
*@param simpMat
*@retval NONE
*/
void simpMat_Delete(simpMat *Matrix);

#endif

Here is the source file:

#include "simpMat.h"

void simpMat_Init(simpMat *Matrix, uint8_t nRows, uint8_t nColumns, uint8_t nElements, float elements[])
{
    Matrix->nRows = nRows;
    Matrix->nCols = nColumns;
    Matrix->nElements = nElements;

    Matrix->elements = (float**)malloc(nRows * sizeof(float*));
    for (uint8_t i = 0; i < nRows; i++)
    {
        Matrix->elements[i] = (float*)malloc(nColumns * sizeof(float));
    }
    
    uint8_t count = 0;

    for (uint8_t i = 0; i < nRows; i++)
    {
        for (uint8_t j = 0; j < nColumns; j++)
        {
            Matrix->elements[i][j] = elements[count];
            count++;
        }
    }      
        
}

void simpMat_Print(simpMat *Matrix)
{
    for (uint8_t i = 0; i < Matrix->nRows; i++)
    {
        for (uint8_t j = 0; j < Matrix->nCols; j++)
        {
            printf("%d ", Matrix->elements[i][j]);

        }

        printf("\n");
    }
}


void simpMat_Delete(simpMat *Matrix)
{
    uint8_t n = Matrix->nRows;
    while(n) free(Matrix->elements[--n]);
    free(Matrix->elements);
}

I also wrote a small test program to see if I can successfully assign elements to the matrix; such as:

#include "simpMat.h"
#include "stdio.h"

int main()
{
    simpMat Matrix1;

    float toAppend[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

    simpMat_Init(&Matrix1, 3, 2, 9, toAppend);

    printf("MATRIX ELEMENTS ARE:\n");

    simpMat_Print(&Matrix1);

    simpMat_Delete(&Matrix1);

    return 0;

}

I compiled my library and the main program with the following commands on CMD:

g++ -c simpMat.cpp
ar rvs simpMat.a simpMat.o
g++ test_simpMat.c simpMat.a

However, when I run the executable, I get the following output:

MATRIX ELEMENTS ARE:
0 0
0 0
0 0

I could not understand the reason I cannot assign values. I am fairly new to the Dynamic Memory Allocation subject and I suspect that I had a misconception about the methodology. Can you help me with that?

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

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

发布评论

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

评论(1

浅笑依然 2025-01-16 21:16:40

如果您使用调试器并单步执行程序并查看内存,您应该会看到数据实际上在那里。您的问题假设问题是分配,而它实际上在您的输出中。这种事情最容易用调试器发现。

实际问题是你的矩阵元素是浮点的。但是您在 printf 中使用 %d 说明符,该说明符用于 int 值。将其更改为 %f

另外,您应该重新考虑 nElements 参数的用途。在复制数组之前,您没有进行任何完整性测试(例如,确保 rows * cols 不超过该值)。它似乎与实际矩阵没有任何关系,不应存储。

If you use a debugger and step through your program looking at the memory, you should see the data is actually there. Your question assumes the problem is assignment, whereas it's actually in your output. This kind of thing is most easily discoverable with a debugger.

The actual problem is your matrix elements are float. But you are using %d specifier in your printf, which is for int values. Change this to %f.

Separately, you should reconsider the purpose of the nElements parameter. You are not doing any sanity tests before copying the array (for example, ensuring rows * cols does not exceed that value). It doesn't appear to have any relation to the actual matrix and should not be stored.

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