gpt4 book ai didi

c - 在结构中分配指针不起作用,为什么值没有改变?

转载 作者:行者123 更新时间:2023-12-04 05:04:23 26 4
gpt4 key购买 nike

对于学校项目,我应该仅使用链表结构来实现 UNIX 文件系统的简化版本。我目前的 mkfs() 函数有问题,它应该只是初始化文件系统。

创建我正在使用的结构的头文件在这里:

typedef struct Lines {
char line[82];
struct Lines *next;
} Lines;

typedef struct Node {
char *name;
int id;
struct Node *parent;
struct Node *next;
union {
char line[82];
struct Node *children;
} contents;
} Node;

typedef struct Filesystem {
char *name;
struct Node *root;
struct Node *current;
} Filesystem;

这是我的单独文件中的方法,其中 #includes 这个头文件:
void mkfs(Filesystem *files) {
Node *root = NULL; /* Creates a pointer to the directory we will use as
* the root directory for this filesystem*/

files = (Filesystem *)malloc(sizeof(*files)); /* Allocates space for the the
* filesystem structure */

if(files == NULL){ /* If there is no memory available, prints error message
* and does nothing else */

printf("Memory allocation failed!\n");

} else {

root = (Node *)malloc(sizeof(*root)); /* Allocates space for the root
* directory of the filesystem. */


if(root == NULL) { /* If there is no memory available, prints error
* message and frees memory obtained thus far, but then
* does nothing else */

printf("Memory allocation failed!\n");
free(files);

} else {

/* Allocates space for the root directory's name string */
root->name= (char *)malloc(sizeof(char)*(strlen("/")+1));

if(root->name == NULL) { /* If there is no memory available, prints error
* message and frees memory obtained thus far,
* but then does nothing else */

printf("Memory allocation failed!\n");

free(files);
free(root);

} else {

root->name = "/"; /* Defines the root directory as being named by the
* forward slash */ /* DO STR CPY HERE ITS CHANGING THE ADDRESS */
root->contents.children = NULL;
root->next = NULL;
root->parent = NULL; /* UHH CHECK ON THIS NOOO CLUE IF ITS RIGHT FUUU*/

files->root = root; /* The filesystems pointer to a directory is set
* to point to the root directory we just allocated
* space for and set up */

files->current = root; /* Sets the filesystems current directory to
* point to the root directory as well, because
* it is the only directory in existence for this
* filesystem at this point. */
}
}
}
}

我遇到的问题是,当我运行 gdb 并逐步执行每一行时,最后两行分配行不会更改 file->root 和 file->current 的内容。
比如我这里打印files->root的内容,运行files->root = root这一行,然后再打印一次,可以看到地址没有变。但是,如果我只是打印 root,我试图将它分配给它,它显然具有不同的值,files->root 应该设置为:
(gdb) print files->root
$12 = (struct Node *) 0x400660
(gdb) step
(gdb) print files->root
$13 = (struct Node *) 0x400660
(gdb) print root
$14 = (Node *) 0x602030

有没有人知道为什么在这种情况下作业可能不起作用?这目前正在破坏我的整个项目,因此任何见解将不胜感激。谢谢!!!

最佳答案

它看起来像你的 mkfs函数正在接受一个指向已经存在的指针 Filesystem然后你试图为一个新的 Filesystem 分配内存在一个新的内存位置。像这样的函数有两种常见的约定:要么不接受参数并返回指向结构的指针,要么接受指向已分配结构的指针并填充该结构。看起来数据没有改变的原因是您实际上正在创建和填充第二个结构,而调用者的结构保持不变。

这是第一种情况的示例,将函数简化为仅内存分配部分:

Filesystem * mkfs() {
Filesystem *files = (Filesystem *)malloc(sizeof(Filesystem));

// (error handing omitted for brevity)

// populate the files struct as appropriate...
Node *root = (Node *)malloc(sizeof(Node));
files->root = root;
// etc, etc as you currently have

return files;
}

// In this case you should also provide a way for the caller to free a filesystem,
// which will free everything you allocated during mkfs:
void freefs(Filessystem *files) {

// first free any buffers you allocated inside the struct. For example:
free(files->root);

// then free the main filesystem struct
free(files);
}

然后调用者使用这两个函数处理这个对象。例如:
int main() {
Filesystem *files = mkfs();
// now "files" is ready to use
freefs(files); // free the objects when we're done with them.
}

这是第二种情况的示例,它假设调用者已经分配了适当的缓冲区,并且只需要填充它:
void mkfs(Filesystem *files) {

// populate the files struct as appropriate...
Node *root = (Node *)malloc(sizeof(Node));
files->root = root;
// etc, etc as you currently have

}

void freefs(Filesystem *files) {
// still need to clean up all of the ancillary objects
free(files->root);
// etc, etc
}

在这种情况下,调用函数还有一些工作要做。例如:
int main() {
Filesystem *files = (Filesystem *)malloc(sizeof(Filesystem));
mkfs(files);
// now "files" is ready to use
freefs(files); // free the objects when we're done with them.
}

两种模式都是有效的;如果您希望调用者需要能够控制内存的分配方式,则前者很有用。例如,调用者可能决定在堆栈而不是堆上分配文件系统:
int main() {
Filesystem files;
mkfs(&files);
// now "files" is ready to use
freefs(&files); // free the ancillary objects when we're done with them.
// "files" is still allocated here, but it's no longer valid
}

后者代表调用者处理分配。由于您的函数在堆上分配了更多结构,因此在这两种情况下都必须包含清理函数。

关于c - 在结构中分配指针不起作用,为什么值没有改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15714838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com