在不使用malloc的情况下创建动态数组 [英] Creating Dynamic Array without malloc

查看:57
本文介绍了在不使用malloc的情况下创建动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶地看到这段代码正在工作.我不知道为什么

I was amazed to see that this code is working. I couldn't figure out why

#include<stdio.h>
int main(){
  int row,col,i,j;
  scanf("%d %d",&row,&col);
  int a[row][col];
  for(i=0;i<row;i++)
      for(j=0;j<col;j++)
           scanf("%d",&a[i][j]);
   for(i=0;i<row;i++){
      for(j=0;j<col;j++)
           printf("%d ",a[i][j]);
       printf("\n");
   }
}

由于C是一种编译语言,那么它如何为数组a [row] [col]分配内存?由于在编译时不知道row和column的值,那么它如何制作机器代码并设置程序的地址空间?为什么这可以作为解释器语言工作,如果可以用这种方法创建动态数组,那么为什么要教我们使用 malloc在C语言中创建动态数组.

Since C is a compiled language then How it is allocating memory for the array a[row][col] ? Since at the time of compilation the value of row and column are not known, then how it is able to make machine code and set the address space for the program? Why this is working as an interpreter language would have worked, If this is a way to create a dynamic array then why are we taught to use malloc for creating dynamic array in C.

推荐答案

从C99开始,可变长度数组一直是C的标准功能.

Variable-length arrays have been a standard feature of C since C99.

它如何为数组 a [row] [col]

一旦知道 row col 的值,编译后的代码就可以在自动存储区域中进行此分配(通常称为堆栈").

Once the value of row and col is known, there is no problem for the compiled code to make this allocation in the automatic storage area (commonly referred to as "the stack").

如何制作机器代码并设置程序的地址空间?

how it is able to make machine code and set the address space for the program?

编译器松散 row col 的值以供其生成的机器代码内部使用.引用 a [i] [j] 的指令将查找大小,进行数学运算,添加偏移量并获取地址,就像 row col在编译时是已知的.

The compiler squirrels away the values of row and col for internal use by machine code that it generates. Instructions that reference a[i][j] look up the sizes, do the math, add the offset, and get the address, as if row and col were known at compile time.

此功能确实会改变其他一些事情-例如, sizeof 不再是纯粹的编译时操作,因为VLA的大小需要在运行时进行计算.

The feature does change a few other things - for example, sizeof is no longer a purely compile-time operation, because the size of VLAs need to be computed at run-time.

为什么要教我们使用 malloc 在C语言中创建动态数组.

why are we taught to use malloc for creating dynamic array in C.

malloc 为您提供了比VLA更大的灵活性.例如,您可以从函数中返回 malloc 创建的数组,而函数完成后,VLA会自动释放.此外, malloc 不太可能在内存不足的情况下运行程序,因为动态存储区的大小比自动存储区大得多.

malloc gives you more flexibility than a VLA. For example, you can return a malloc-created array from your function, while VLA gets automatically deallocated as soon as the function finishes. In addition, malloc is less likely to run your program out of memory, because dynamic storage area is more generously sized than the automatic one.

例如,如果您尝试为程序输入 2000 2000 ,则可能会崩溃.但是使用 malloc 分配相同数量的内存将没有问题:

For example, if you try to enter 2000 2000 for your program, it is likely going to crash. Yet it would have no problem allocating the same amount of memory with malloc:

int (*a)[col] = malloc(row*col*sizeof(int));

这篇关于在不使用malloc的情况下创建动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆