C矩阵,分配不会将所有元素归零吗?

发布于 2025-01-02 10:50:02 字数 1733 浏览 2 评论 0原文

我正在尝试编写一个小矩阵程序。使用双指针不起作用,所以我认为最简单的方法是使用一个包含 #rows 和 #columns 以及一个一维数组作为矩阵的结构。

但正如我所得到的,矩阵的启动存在一些错误: 索引 (0,0) 和 (0.1) 的奇怪值而不是 0。

也许与此有关: 矩阵 *mtrx = malloc(sizeof(矩阵)); mtrx->m = malloc(r * c * sizeof(int));

矩阵.c:

#include <stdio.h>
#include <stdlib.h>
#include "Matrix.h"

matrix *alloc_matrix(int r, int c)
{
 matrix *mtrx = malloc(sizeof(matrix));
 mtrx->m = malloc(r * c * sizeof(int));
 if (mtrx == NULL || m == NULL) {
   printf("Out of memory.");
   exit(1);
 }
 mtrx->rows = r;
 mtrx->columns = c;
 return mtrx;
}

void free_matrix(matrix *mtrx)
{
 free(mtrx->m);
 free(mtrx);
}

void set(matrix *mtrx, int r, int c, int v)
{
 (mtrx->m)[r * mtrx->columns + c] = v;
}

int get(matrix *mtrx, int r, int c)
{
 return (mtrx->m)[r * mtrx->columns + c];
}

void print_matrix(matrix *mtrx)
{
 int i,j;
 printf("\n");
 for(i=0; i<mtrx->rows; i++) {
   for(j=0; j<mtrx->columns; j++) {
     printf("%i ", get(mtrx,i,j));
   }
   printf("\n");
 }
}

矩阵.h:

struct matrix_ {
 int rows;
 int columns;
 int *m;
};
typedef struct matrix_ matrix;

matrix *alloc_matrix(int r, int c);
void free_matrix(matrix *mtrx);
void set(matrix *mtrx, int r, int c, int v);
int get(matrix *mtrx, int r, int c);
void print_matrix(matrix *m);

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "Matrix.h"

int main(void)
{
 matrix *m = alloc_matrix(3,4);
 print_matrix(m);
 printf("\nm[0][0] = %i", get(m,0,0));
 set(m,0,0,0);
 printf("\nm[0][0] = %i", (m->m)[0]);
  printf("\nm[0][0] = %i", (m->m)[12]);

 return 0;
}

输出: 除 (0,0) 和 (0,1) 之外的所有元素均为 0。

I'm trying to write a little matrix program. Using doublke pointers doesnt work so I figure the easiest way is to have a struct that has the #rows and #columns and a 1d array as the matrix.

But there is some error in the initiation of the matrix as i get:
weird values for the indices (0,0) and (0.1) instead of 0.

Something with this perhaps:
matrix *mtrx = malloc(sizeof(matrix));
mtrx->m = malloc(r * c * sizeof(int));

matrix.c:

#include <stdio.h>
#include <stdlib.h>
#include "Matrix.h"

matrix *alloc_matrix(int r, int c)
{
 matrix *mtrx = malloc(sizeof(matrix));
 mtrx->m = malloc(r * c * sizeof(int));
 if (mtrx == NULL || m == NULL) {
   printf("Out of memory.");
   exit(1);
 }
 mtrx->rows = r;
 mtrx->columns = c;
 return mtrx;
}

void free_matrix(matrix *mtrx)
{
 free(mtrx->m);
 free(mtrx);
}

void set(matrix *mtrx, int r, int c, int v)
{
 (mtrx->m)[r * mtrx->columns + c] = v;
}

int get(matrix *mtrx, int r, int c)
{
 return (mtrx->m)[r * mtrx->columns + c];
}

void print_matrix(matrix *mtrx)
{
 int i,j;
 printf("\n");
 for(i=0; i<mtrx->rows; i++) {
   for(j=0; j<mtrx->columns; j++) {
     printf("%i ", get(mtrx,i,j));
   }
   printf("\n");
 }
}

matrix.h:

struct matrix_ {
 int rows;
 int columns;
 int *m;
};
typedef struct matrix_ matrix;

matrix *alloc_matrix(int r, int c);
void free_matrix(matrix *mtrx);
void set(matrix *mtrx, int r, int c, int v);
int get(matrix *mtrx, int r, int c);
void print_matrix(matrix *m);

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "Matrix.h"

int main(void)
{
 matrix *m = alloc_matrix(3,4);
 print_matrix(m);
 printf("\nm[0][0] = %i", get(m,0,0));
 set(m,0,0,0);
 printf("\nm[0][0] = %i", (m->m)[0]);
  printf("\nm[0][0] = %i", (m->m)[12]);

 return 0;
}

output:
all elements except (0,0) and (0,1) is 0.

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

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

发布评论

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

评论(6

﹏雨一样淡蓝的深情 2025-01-09 10:50:02

函数 malloc 分配一块内存,返回一个指向该内存块开头的指针堵塞。它不会将所有位设置为零。

分配一块内存并将其所有位初始化为零 - 这就是calloc 函数是针对的。

或者,您可以使用 memset 将这些位显式设置为零

Function malloc allocates a block of memory, returning a pointer to the beginning of the block. It doesn't set all its bits to zero.

Allocating a block of memory and initializing all its bits to zero - that's what calloc function is for.

Or you can explicitly set these bits to zero by using memset

梦开始←不甜 2025-01-09 10:50:02

malloc 分配的对象具有未指定的值。如果需要,您必须自己将对象归零(例如使用 memset 函数)或调用 calloc 而不是 malloc

The object allocated by malloc has an unspecified value. If needed, you have to zero-ize the object yourself (for example using memset function) or call calloc instead of malloc.

绮烟 2025-01-09 10:50:02

malloc() 不保证将内存清零。使用 calloc() 以零分配内存。

malloc() is not guaranteed to zero the memory. Use calloc() to allocate memory with zeros.

仄言 2025-01-09 10:50:02

malloc 不以 0 开头(由于性能问题...它并不总是您想要的)...

使用 calloc() 代替。

malloc does not initiate with 0's (because of performance issues... it's not always what you want)...

use calloc() instead.

风流物 2025-01-09 10:50:02

malloc 不会将分配的内存归零。如果您想在分配时用零填充矩阵,请改用calloc。对于您的情况,请将: 替换

mtrx->m = malloc(r * c * sizeof(int));

mtrx->m = calloc(r*c, sizeof(int));

回答您的后续问题:

但是malloc+memset和calloc在效率上有什么区别吗?或者calloc只是malloc+memset?

通常,对于“小”分配,calloc 相当于 malloc + memset。对于“大”分配(至少是多个页面),您的库可能会依靠一定程度的操作系统支持来完成一些更聪明的事情。一种方法是操作系统在实际使用分配的页面时对它们进行惰性零填充,而不是在分配后立即对所有页面进行零填充。

malloc does not zero out allocated memory. If you want to fill the matrix with zeros on allocation, use calloc instead. In your case, replace:

mtrx->m = malloc(r * c * sizeof(int));

with

mtrx->m = calloc(r*c, sizeof(int));

Answering your follow-up question:

However is there any difference in efficiency between malloc+memset and calloc? or is calloc simplye malloc+memset?

Typically, for "small" allocations calloc is equivalent to malloc + memset. For "large" allocations (multiple pages, at least), your library may do something more clever relying on some amount of OS support. One approach would be for the OS to lazily zero fill the allocated pages as they are actually used, rather than zero filling all of them immediately upon allocation.

温柔少女心 2025-01-09 10:50:02

这是正确的。 C 规范并没有说数组可以初始化为任何东西。你只是得到了一段内存,它将具有以前的任何值。

不过,您可以轻松地初始化为 sero:memset(mtrx->m, 0, sizeof(int) * r * c);

That is correct. The C specification does not say arrays are initilized to anything. You just get a piece of memory that will have whatever values there where before.

You can easily initialize to sero though: memset(mtrx->m, 0, sizeof(int) * r * c);

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