在 realloc 中进行 realloc

发布于 2024-10-02 06:39:20 字数 100 浏览 0 评论 0原文

在C语言中,可以在realloc内部进行realloc吗?例如,当您需要 malloc 和 realloc 两者时,结构体内部的结构体。如果是,有人可以提供一个简单的例子吗? 先感谢您。

In C can you have realloc inside realloc? For example, a struct inside a struct when you need to malloc both of them and realloc both of them. If yes can someone please provide a simple example?
Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第七度阳光i 2024-10-09 06:39:20

您的问题并不十分清楚,但是...

是的,给定的动态分配的结构(例如,结构数组)本身可以包含指向已分配数据的指针(例如已分配结构的各种其他数组),并且您可以重新分配各部分独立。

然而,当您重新分配其中一个结构时,系统不会为您调用realloc();您必须单独编程各种调整大小操作。

嵌套数据结构示例:

struct line { char *info; size_t length; };

struct section { size_t num_lines; struct line *lines; };

您可以分配一个节数组,并在需要时重新分配该数组。每个部分都包含一个行数组,并且每个行数组也可以独立地重新分配。


因此:(

size_t num_sections = 0;
size_t max_sections = 0;
struct section *sections = 0;

if (num_sections == max_sections)
{
     size_t new_max = (max_sections + 1) * 2;
     struct section *new_sections;
     if (sections == 0)
         new_sections = malloc(new_max * sizeof(*new_sections));
     else
         new_sections = realloc(sections, new_max * sizeof(*new_sections));
     if (new_sections == 0)
         ...out of memory error...
     sections = new_sections;
     max_sections = new_max;
 }
 struct section *section = §ions[num_sections++];  // Newly available section
 section->num_lines = 0;
 section->lines = 0;
 return section;

我假设 C99 - 在我想要的地方声明变量。)

类似的过程适用于节内的行数组,除了节结构没有分配的行数和行数的单独值实际使用的线路数。当然,每一行也有自己为字符串分配的内存......

Your question is not dreadfully clear, but...

Yes, a given dynamically allocated structure (for example, an array of structures) can itself contain pointers to allocated data (such as various other arrays of allocated structures), and you can reallocate the various parts independently.

However, the system will not call realloc() for you while you are reallocating one of the structures; you would have to separately program the various resizing operations.

Example nested data structures:

struct line { char *info; size_t length; };

struct section { size_t num_lines; struct line *lines; };

You could allocate an array of sections, and reallocate that array when needed. Each section contains an array of lines, and each of those arrays of lines can be independently reallocated too.


Hence:

size_t num_sections = 0;
size_t max_sections = 0;
struct section *sections = 0;

if (num_sections == max_sections)
{
     size_t new_max = (max_sections + 1) * 2;
     struct section *new_sections;
     if (sections == 0)
         new_sections = malloc(new_max * sizeof(*new_sections));
     else
         new_sections = realloc(sections, new_max * sizeof(*new_sections));
     if (new_sections == 0)
         ...out of memory error...
     sections = new_sections;
     max_sections = new_max;
 }
 struct section *section = §ions[num_sections++];  // Newly available section
 section->num_lines = 0;
 section->lines = 0;
 return section;

(I'm assuming C99 - with variable declarations where I want them.)

A similar process applies for the array of lines within a section, except the section structure doesn't have separate values for the number of allocated lines and the number of lines actually in use. Each line also has its own allocated memory for the string of characters, of course...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文