相关疑难解决方法(0)

我是否施放了malloc的结果?

这个问题,有人建议意见,我应该不会投的结果malloc,即

int *sieve = malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)

而不是:

int *sieve = (int *) malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)

为什么会这样呢?

c malloc casting

2318
推荐指数
27
解决办法
22万
查看次数

在迭代2D数组时,为什么循环的顺序会影响性能?

可能重复:
这两个for循环中的哪一个在时间和缓存性能方面更有效

下面是两个几乎相同的程序,除了我切换ij变量.它们都运行在不同的时间.有人能解释为什么会这样吗?

版本1

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

main () {
  int i,j;
  static int x[4000][4000];
  for (i = 0; i < 4000; i++) {
    for (j = 0; j < 4000; j++) {
      x[j][i] = i + j; }
  }
}
Run Code Online (Sandbox Code Playgroud)

版本2

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

main () {
  int i,j;
  static int x[4000][4000];
  for (j = 0; j < 4000; j++) {
     for (i = 0; i < 4000; i++) {
       x[j][i] = i …
Run Code Online (Sandbox Code Playgroud)

c optimization performance for-loop cpu-cache

350
推荐指数
6
解决办法
4万
查看次数

创建指向二维数组的指针

我需要一个指向静态二维数组的指针.这是怎么做到的?

static uint8_t l_matrix[10][20];

void test(){
   uint8_t **matrix_ptr = l_matrix; //wrong idea 
}
Run Code Online (Sandbox Code Playgroud)

我得到各种错误,如:

  • 警告:从不兼容的指针类型分配
  • 下标值既不是数组也不是指针
  • 错误:无效使用灵活的数组成员

c arrays pointers

110
推荐指数
5
解决办法
21万
查看次数

动态分配矩阵的功能

我想创建一个函数来分配(带malloc/ calloc)声明为双指针的矩阵.我理解双指针矩阵如何工作以及如何分配它malloc,但当我传递我的矩阵(声明main()并初始化NULL)时,我的程序崩溃了.我想错误与我的allocMatrix()功能有关,因为如果我在main所有工作中顺利分配矩阵.谢谢 :-)

主要:

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

#define ROW 5
#define COL 5

int main(void) {

    int i,j, ret;
    int nRow, nCol;
    int **mat=NULL; // double pointer matrix

    nRow = 0;
    nCol = 0;

    //Insert n of row and columns
    printf("Insert n of rows and columns:\n");
    scanf("%d %d", &nRow, &nCol);

    //Functions to allocate matrix
    ret=allocMatrix(mat, nRow, nCol);
    printf("Return value: %d\n",ret);

    /*
    this code works …
Run Code Online (Sandbox Code Playgroud)

c malloc pointers dynamic matrix

3
推荐指数
2
解决办法
5363
查看次数