如何为2D数组分配和取消分配堆内存? [英] How to allocate and deallocate heap memory for 2D array?

查看:78
本文介绍了如何为2D数组分配和取消分配堆内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经习惯了PHP,但是我开始学习C.我试图创建一个程序来逐行读取文件并将每一行存储到一个数组中.

I'm used to PHP, but I'm starting to learn C. I'm trying to create a program that reads a file line by line and stores each line to an array.

到目前为止,我有一个程序可以逐行读取文件,甚至可以逐行打印文件,但是现在我只需要将每行添加到数组中即可.

So far I have a program that reads the file line by line, and even prints each line as it goes, but now I just need to add each line to an array.

昨晚我的好友告诉了我一些事情.他说我必须在C中使用多维数组,因此基本上是array[x][y]. [y]部分本身很容易,因为我知道每一行的最大字节数.但是,我不知道文件有多少.

My buddy last night was telling me a bit about it. He said I'd have to use a multidimensional array in C, so basically array[x][y]. The [y] part itself is easy, because I know the maximum amount of bytes that each line will be. However, I don't know how many lines the file will be.

我认为我可以使它遍历文件,每次都增加一个整数并使用它,但是我觉得可能有更简单的方法来实现它.

I figure I can make it loop through the file and just increment an integer each time and use that, but I feel that there might be a more simple way of doing it.

任何想法或正确方向的暗示?感谢您的帮助.

Any ideas or even a hint in the right direction? I appreciate any help.

推荐答案

要动态分配2D数组,请执行以下操作:

To dynamically allocate a 2D array:

char **p;
int i, dim1, dim2;


/* Allocate the first dimension, which is actually a pointer to pointer to char   */
p = malloc (sizeof (char *) * dim1);

/* Then allocate each of the pointers allocated in previous step arrays of pointer to chars
 * within each of these arrays are chars
 */
for (i = 0; i < dim1; i++)
  {
    *(p + i) = malloc (sizeof (char) * dim2);
   /* or p[i] =  malloc (sizeof (char) * dim2); */
  }

 /* Do work */

/* Deallocate the allocated array. Start deallocation from the lowest level.
 * that is in the reverse order of which we did the allocation
 */
for (i = 0; i < dim1; i++)
{
  free (p[i]);
}
free (p);

修改以上方法.当您需要添加另一行时,请执行*(p + i) = malloc (sizeof (char) * dim2);并更新i.在这种情况下,您需要预测文件中由dim1变量指示的最大行数,我们将首次为其分配p数组.这样只会分配(sizeof (int *) * dim1)个字节,因此比char p[dim1][dim2](在c99中)要好得多.

Modify the above method. When you need another line to be added do *(p + i) = malloc (sizeof (char) * dim2); and update i. In this case you need to predict the max numbers of lines in the file which is indicated by the dim1 variable, for which we allocate the p array first time. This will only allocate the (sizeof (int *) * dim1) bytes, thus much better option than char p[dim1][dim2] (in c99).

我有另一种想法.在块中分配数组,并在发生溢出时将它们链接起来.

There is another way i think. Allocate arrays in blocks and chain them when there is an overflow.

struct _lines {
   char **line;
   int n;
   struct _lines *next;
} *file;

file = malloc (sizeof (struct _lines));
file->line = malloc (sizeof (char *) * LINE_MAX);
file->n = 0;
head = file;

此后,第一个块就可以使用了.当您需要插入一行时,只需执行以下操作:

After this the first block is ready to use. When you need to insert a line just do:

/* get line into buffer */
file.line[n] = malloc (sizeof (char) * (strlen (buffer) + 1));
n++;

nLINE_MAX时,分配另一个块并将其链接到该块.

When n is LINE_MAX allocate another block and link it to this one.

struct _lines *temp;

temp = malloc (sizeof (struct _lines));
temp->line = malloc (sizeof (char *) * LINE_MAX);
temp->n = 0;
file->next = temp;
file = file->next;

类似这样的东西.

当一个块的n变为0时,请对其进行分配,然后将当前块指针file更新为上一个.您可以从单链列表的开头开始遍历,也可以从双链开始遍历.

When one block's n becomes 0, deallocate it, and update the current block pointer file to the previous one. You can either traverse from beginning single linked list and traverse from the start or use double links.

这篇关于如何为2D数组分配和取消分配堆内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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