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

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

问题描述

我习惯了 PHP,但我开始学习 C.我正在尝试创建一个程序,该程序可以逐行读取文件并将每一行存储到一个数组中.

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

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

我想我可以让它循环遍历文件,每次只增加一个整数并使用它,但我觉得可能有更简单的方法.

有什么想法甚至是正确方向的提示吗?感谢您的帮助.

解决方案

动态分配二维数组:

char **p;int i、dim1、dim2;/* 分配第一个维度,实际上是一个指向char指针的指针*/p = malloc (sizeof (char *) * dim1);/* 然后将上一步分配的每个指针分配给chars的指针数组* 在这些数组中的每一个中都是字符*/for (i = 0; i 

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

我认为还有另一种方式.以块为单位分配数组,并在出现溢出时将它们链接起来.

struct _lines {字符**行;国际n;结构 _lines *next;} *文件;文件 = malloc (sizeof (struct _lines));file->line = malloc (sizeof (char *) * LINE_MAX);文件->n = 0;头 = 文件;

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

/* 将行放入缓冲区 */file.line[n] = malloc (sizeof (char) * (strlen (buffer) + 1));n++;

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

struct _lines *temp;temp = malloc (sizeof (struct _lines));temp->line = malloc (sizeof (char *) * LINE_MAX);温度->n = 0;文件->下一个=临时;文件=文件->下一步;

类似的东西.

当一个块的n变为0时,释放它,并将当前块指针file更新到前一个.可以从单链表开始遍历,也可以从头开始遍历,也可以使用双链表.

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.

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.

解决方案

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);

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++;

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;

Something like this.

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.

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

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