【问题标题】:C matrix, allocating doesnt zero all elements?C矩阵,分配不为零的所有元素?
【发布时间】:2012-02-27 09:03:21
【问题描述】:

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

但是我得到的矩阵初始化有一些错误: 索引 (0,0) 和 (0.1) 而不是 0 的奇怪值。

也许有这样的东西: 矩阵 *mtrx = malloc(sizeof(matrix)); 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。

【问题讨论】:

  • 所有,它现在可以工作了。但是 malloc+memset 和 calloc 之间的效率有什么区别吗?还是 calloc 只是 malloc+memset?

标签: c memory-management matrix initialization malloc


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      请改用 calloc()。

      【讨论】:

        【解决方案5】:

        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。对于“大”分配(至少是多页),您的库可能会依靠一定数量的操作系统支持来做一些更聪明的事情。一种方法是让操作系统在实际使用分配的页面时懒惰地对其进行零填充,而不是在分配时立即将它们全部归零。

        【讨论】:

          【解决方案6】:

          没错。 C 规范没有说数组被初始化为任何东西。您只需获得一段内存,该内存将具有以前的任何值。

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

          【讨论】:

            猜你喜欢
            • 2015-09-20
            • 1970-01-01
            • 2015-03-19
            • 2014-03-21
            • 1970-01-01
            • 1970-01-01
            • 2014-07-06
            • 2017-11-27
            • 2016-11-13
            相关资源
            最近更新 更多