我在Fedora下使用g ++来编译一个openGL项目,该项目有以下几行:
textureImage = (GLubyte**)malloc(sizeof(GLubyte*)*RESOURCE_LENGTH);
Run Code Online (Sandbox Code Playgroud)
编译时,g ++错误说:
error: ‘malloc’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
添加#include <cstdlib>不能解决错误.
我的g ++版本是: g++ (GCC) 4.4.5 20101112 (Red Hat 4.4.5-2)
假设我正在用C编写一个小库 - 比如一些数据结构.如果我无法分配内存,该怎么办?
这可能非常重要,例如我需要一些内存来初始化数据结构,或者我正在插入一个键值对并希望将它包装在一个小结构中.它也可能不太重要,例如类似于pretty_print构建内容的良好字符串表示的函数.然而,它通常比你的平均错误更严重 - 可能没有任何意义继续下去.malloc如果返回,大量的在线样本使用直接退出程序NULL.我猜很多真正的客户端代码也会这样做 - 只是弹出一些错误,或写入stderr并中止.(而且很多真正的代码可能根本不检查返回值malloc.)
有时回归是有意义的NULL,但并非总是如此.错误代码(或只是一些布尔success值),无论是返回值还是输出参数都可以正常工作,但似乎它们可能会混乱或损害API的可读性(那么,可能在C语言中有点期待?) .另一种选择是让调用者可以随后查询某种内部错误状态,例如使用get_error函数,但是你必须小心线程安全,并且可能很容易错过; 无论如何,人们往往会对检查错误松懈,如果它完全是一个单独的功能,他们可能不知道它,或者他们可能不会打扰(但我猜这是他们的问题).
(我有时看到malloc包裹在一个函数中,只是再次尝试,直到内存可用...
void *my_malloc(size_t size)
{
void *result = NULL;
while (result == NULL)
result = malloc(size);
return result;
}
Run Code Online (Sandbox Code Playgroud)
但这似乎有点愚蠢,也许很危险.)
处理这个问题的正确方法是什么?
我想知道为什么我error: flexible array member not at end of struct在调用malloc 时会一直出错.我有一个带有可变长度数组的结构,我不断收到此错误.
结构是,
typedef struct {
size_t N;
double data[];
int label[];
} s_col;
Run Code Online (Sandbox Code Playgroud)
而对malloc的调用是,
col = malloc(sizeof(s_col) + lc * (sizeof(double) + sizeof(int)));
Run Code Online (Sandbox Code Playgroud)
这是对malloc的正确调用吗?
根据C++参考,您可以通过以下方式新建一个对象:
MyClass * p1 = new MyClass;
Run Code Online (Sandbox Code Playgroud)
或者
MyClass * p2 = new (std::nothrow) MyClass;
Run Code Online (Sandbox Code Playgroud)
第二个将返回空指针而不是抛出异常.
但是,根据我的经验,我几乎看不到这个版本.
例如,谷歌不建议在他们的代码中使用异常,但我们可以看到他们没有在Chromium中使用nothrow版本.
是否有任何理由我们更喜欢默认的一个而不是一个?即使在没有使用异常的项目中?
- 编辑 -
跟进问题:我应该检查返回值malloc()吗?
看起来,相反,许多人建议检查malloc的返回值,有些人说是因为:
许多分配失败与内存不足无关.碎片可能导致分配失败,因为即使有足够的可用内存,也没有足够的连续空间可用.
这是真的?为什么我们在这种情况下对待malloc()和new()不同?
因错误而退出程序时,我应该释放所有的mallocated内存吗?
something = (char**) malloc (x * sizeof(char*));
for (i = 0; i < x; i++)
something[i] = (char*) malloc (y + 1);
...
if (anything == NULL) {
printf("Your input is wrong!");
// should I free memory of every mallocated entity now?
exit(1);
}
else {
// work with mallocated entities
...
free(something); // it must be here
system("pause);
}
Run Code Online (Sandbox Code Playgroud) 当函数返回时,通过malloc分配的内存是否被释放?或者仍然可以使用指针在main()函数中访问它?
例如.
void function(int *a)
{
a=(int *)malloc(sizeof(int));
*a=10;
}
int main()
{
int *num;
function(num);
printf("%d",*num);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
存储在a中的整数可以通过main()访问吗?
我正在发送一个文本文件 - 客户端 - 服务器将文本分解为每个512字节的数据包,但是一些数据包包含的文本小于最大大小所以在接收每个数据包的服务器端我正在调用malloc()再次构建一个字符串这是一种不好的做法吗?保持一个适合最大长度的工作缓冲区并保持迭代,复制和覆盖它的值更好吗?
好吧@nm这里是代码,这个if是在select()唤醒的for(;;)循环里面
if(nbytes==2) {
packet_size=unpack_short(short_buf);
printf("packet size is %d\n",packet_size);
receive_packet(i,packet_size,&buffer);
printf("packet=%s\n",buffer);
free(buffer);
}
//and here is receive_packet() function
int receive_packet(int fd,int p_len,char **string) {
*string = (char *)malloc(p_len-2); // 2 bytes for saving the length
char *i=*string;
int temp;
int total=0;
int remaining=p_len-2;
while(remaining>0) {
//printf("remaining=%d\n",remaining);
temp = recv(fd,*string,remaining,0);
total+=temp;
remaining=(p_len-2)-total;
(*string) += temp;
}
*string=i;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我的印象是alloc在Objective-C中(当我们调用时[anyObject alloc]实际上是在实现C函数malloc并且内存在堆中分配,但找不到任何答案).
此外,在搜索时alloc,我发现了alloca哪些在堆栈中分配内存.如果我没有错,请alloc在堆中分配内存以创建对象.
那么alloc和malloc(和alloca)之间有什么区别?任何人都可以总结一下吗?
众所周知:http://linux.die.net/man/3/malloc
默认情况下,Linux遵循乐观的内存分配策略.这意味着当malloc()返回非NULL时,无法保证内存确实可用.如果事实证明系统内存不足,那么一个或多个进程将被OOM杀手杀死.
和我们能够成功地通过使用分配1个拍字节VMA(虚拟存储区)的malloc(petabyte);:http://ideone.com/1yskmB
#include <stdio.h>
#include <stdlib.h>
int main(void) {
long long int petabyte = 1024LL * 1024LL * 1024LL * 1024LL * 1024LL; // 2^50
printf("petabyte %lld \n", petabyte);
volatile char *ptr = (volatile char *)malloc(petabyte);
printf("malloc() - success, ptr = %p \n", ptr);
ptr[petabyte - 1LL] = 10;
printf("ptr[petabyte - 1] = 10; - success \n");
printf("ptr[petabyte - 1] = %d \n", (int)(ptr[petabyte - 1LL]));
free((void*)ptr); // why the error is here? …Run Code Online (Sandbox Code Playgroud) 存在几个对齐版本的古老malloc(),例如:
#include <stdlib.h>
int posix_memalign(void **memptr, size_t alignment, size_t size);
void *aligned_alloc(size_t alignment, size_t size);
#include <malloc.h>
void *memalign(size_t alignment, size_t size);
Run Code Online (Sandbox Code Playgroud)
(分别来自POSIX,glibc和Linux libc).但是 - 我似乎无法找到任何realloc()支持对齐的版本.它真的没有实现过吗?将非对齐的功能realloc()与在对齐的malloc()变体中搜索对齐的内存块相结合似乎是微不足道的.
有关: