简体   繁体   English

create_family function中malloc返回NULL应该返回什么?

[英]What should I return when malloc returns NULL in the create_family function?

I have tried this:我试过这个:

#include <stdio.h>
#include <stdlib.h>
person *create_family(int generations)
{
    // TODO: Allocate memory for new person
    person *new = malloc(sizeof(person));
    if (new == NULL)
    {
        free(new);
        return 1;
    }
    // Other code stuffs
}

If return 1' in if (new == NULL) block, compile result is↓如果在if (new == NULL)块中return 1' ,编译结果为↓

cs50/labs/lab5/inheritance/ $ make inheritance inheritance.c:47:16:
error: incompatible integer to pointer conversion returning 'int' from
a function with result type 'person *' (aka 'struct person *')
[-Werror,-Wint-conversion]
        return 1;
               ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 2 errors generated.
make: *** [<builtin>: inheritance] Error 1

When the Standard was written, there were two common ways that programs could use malloc() , and the authors of the Standard wanted to accommodate both.编写标准时,程序可以通过两种常见方式使用malloc() ,而标准的作者希望兼顾这两种方式。

Some programs would use malloc() when they needed storage, and would expect that either there would be enough memory to satisfy all their needs, or else they would exit with a message saying that insufficient memory was available.一些程序在需要存储时会使用malloc() ,并期望要么有足够的 memory 来满足它们的所有需求,要么它们会退出并显示一条消息说 memory 可用。 There was no need to have programs try to get by with less memory than they would want to use.没有必要让程序尝试使用比他们想要使用的更少的 memory。

Some programs would use malloc() repeatedly to acquire all the memory they could get, and then use their own memory management code to subdivide that into pieces the application could use.一些程序会重复使用malloc()来获取所有他们可以获得的 memory,然后使用他们自己的 memory 管理代码将其细分为应用程序可以使用的部分。 On systems that would only have one program loaded into memory at once, interactive programs using this approach could keep users informed of how much memory was available, and could limit user actions in low-memory conditions to ensure that parts of the code that critically needed memory would always be able to get it.在一次只能将一个程序加载到 memory 的系统上,使用这种方法的交互式程序可以让用户了解 memory 有多少可用,并且可以限制用户在低内存条件下的操作以确保关键需要的代码部分memory 总能拿到。

To make the second approach work, it's necessary that unsuccessful calls to malloc() exit cleanly without side effects, since such calls would be expected to occur.为了使第二种方法起作用,对malloc()的不成功调用必须干净利落地退出而没有副作用,因为预计会发生此类调用。 On the other hand, when using the first approach, it would be more convenient to have functions that can't allocate the memory they need call a configurable error-trap routine, which might in turn call longjmp() or exit() , than to have them return NULL and require that their caller accommodate that possibility.另一方面,当使用第一种方法时,让无法分配 memory 的函数调用一个可配置的错误陷阱例程会更方便,该例程可能会依次调用longjmp()exit() ,而不是让他们返回 NULL 并要求他们的呼叫者适应这种可能性。

While the Standard would suggest that malloc() would either fail without side effects or else yield a usable pointer, many Linux systems introduce a third possibility: malloc() may yield a pointer which, though not null, won't actually be usable to write storage.虽然标准建议malloc()要么失败而没有副作用,要么产生一个可用的指针,许多 Linux 系统引入了第三种可能性: malloc()可能产生一个指针,虽然不是 null,但实际上不会用于写入存储。 If the system would have only two megs of heap storage available just before a program requests to five allocations of 500K each, a Linux system might have all five allocations return seemingly-valid pointers, even though their total size exceeds the total storage available, in the hope that either code won't actually attempt to use all of the allocated storage, or more memory will somehow become available before too much of the storage gets written.如果系统在程序请求五个分配,每个分配 500K 之前只有两兆的堆存储可用,那么 Linux 系统可能会让所有五个分配返回看似有效的指针,即使它们的总大小超过可用的总存储,在希望这两个代码实际上不会尝试使用所有分配的存储空间,或者更多 memory 将在写入太多存储空间之前以某种方式变得可用。

On a system where there's no way of knowing whether an allocation actually succeeded, there will be no generally-reliable way of handling allocation failures without terminating the program, so code to try to recover such failures gracefully will often offer little value.在一个无法知道分配是否真的成功的系统上,在不终止程序的情况下将没有普遍可靠的方法来处理分配失败,因此尝试优雅地恢复此类失败的代码通常没有什么价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM