减少堆栈负载,内存分配在C和伊斯利铸造malloc()函数的返回值 [英] Reducing stack load, memory allocation in C and easly casting malloc()'s return value
问题描述
-
据了解,大局部/全局变量可能会导致堆栈溢出。
我知道,使用指针和在内存中分配空间有助于克服这个问题。
但它是唯一的选择?如果我发生什么(或需要)在全球范围内过于许多指针? -
关于堆栈空间:是一个全球性结构类型的变量发生在栈空间,或者就像一个指针?我是否需要以减少堆栈的负载创建一个结构体变量类型的指针?
-
执行以下操作code也分配内存为char **命名变量
BIG
?
//在头文件
typedef结构MYSTRUCT {
BIG [256] [256];
INT巴兹;
} MYSTRUCT;
//在C文件
MYSTRUCT * foo的;
富=(MYSTRUCT *)malloc的(sizeof的(*富)); -
我怎么能轻易投
的返回值的malloc()
?在问题3我写的:富=(MYSTRUCT *)malloc的(sizeof的(*富));
但我preFER写的是这样的:
富=(富)的malloc(sizeof的(*富)); //编译器会报告错误
编辑code时,这将缓解疼痛(改变的类型时,
富
)。
英语不是我的母语因此任何缺乏明确的遗憾。
尼尔已经回答了你的问题,这里有我在你的第三和第四个问题的意见。
执行以下操作code也内存分配给
的char **
命名变量BIG
?typdef结构MYSTRUCT {
焦大[256] [256];
INT巴兹;
} MYSTRUCT;MYSTRUCT * foo的;富=(MYSTRUCT *)malloc的(sizeof的(*富));
块引用>(我固定的错误
BIG
的类型)。这将为BIG分配空间
,虽然BIG
的类型是不是的char **
。BIG
的类型是字符
数组[256]数组[256]。在值上下文中,它相当于指针数组[256]字符
我怎么能轻易投的malloc的返回值()?在问题3我写的:
富=(MYSTRUCT *)malloc的(sizeof的(*富));
但我preFER写的是这样的:
富=(富)的malloc(sizeof的(*富)); //编译器会报告错误
块引用>你在你想做的事几乎正确,但正确的方式,需要更少的打字!最地道,易于阅读,和型变化安全的方法是:
的#include<&stdlib.h中GT;
富=的malloc(sizeof的* foo的);或者更一般地,
的#include<&stdlib.h中GT;
T *富=的malloc(大小* sizeof的* foo的);无需进行强制类型转换,因为
的malloc()
收益无效*
,它可以转换为任何指针类型来回没有任何信息损失。请注意,在malloc的这个形式不使用
调用,因此它的类型无关。此外,如果它是一个对象上使用(但如果它在一个类型中使用他们需要的),富
的类型()的sizeof
操作员不需要括号。
It's known that big local/global variables may cause to a stack overflow. I know that using pointers and allocating space in memory helps to overcome this problem. But is it the only option? What happens if I have (or need) too-many pointers in global scope?
Regarding the stack space: Is a global struct-type variable takes space in the stack, or acts like a pointer? Do I need to create a pointer of a struct variable type in order to reduce the stack load?
Does the following code allocates memory also to the char** variable named
BIG
?// in the header file typedef struct myStruct { BIG[256][256]; int baz; } myStruct; // in the c file myStruct* foo; foo = (myStruct*) malloc( sizeof(*foo) );How can I easily cast the return value of
malloc()
? In question #3 I wrote:
foo = (myStruct*) malloc( sizeof(*foo) );
But I prefer to write something like:
foo = (foo) malloc( sizeof(*foo) ); // the compiler reports an error
Which will ease the pain when editing the code (when changing the type of
foo
).English isn't my native language so sorry for any lack of clarity.
解决方案Neil already answered your questions, here are my comments on your 3rd and 4th questions.
Does the following code allocates memory also to the
char**
variable namedBIG
?typdef struct myStruct { char BIG[256][256]; int baz; } myStruct; myStruct* foo; foo = (myStruct*) malloc( sizeof(*foo) );
(I fixed the error in
BIG
's type.) This will allocate space forBIG
, althoughBIG
is not of typechar **
.BIG
is of type "array [256] of array [256] ofchar
". In value contexts, it is equivalent to "pointer to array [256] ofchar
".
How can I easily cast the return value of malloc()? In question #3 I wrote:
foo = (myStruct*) malloc( sizeof(*foo) );
But I prefer to write something like:
foo = (foo) malloc( sizeof(*foo) ); // the compiler reports an error
You're almost correct in what you want to do, but the right way requires even less typing! The most idiomatic, easy to read, and "type-change safe" way is:
#include <stdlib.h> foo = malloc(sizeof *foo);
Or more generally,
#include <stdlib.h> T *foo = malloc(size * sizeof *foo);
No casts are needed, since
malloc()
returnsvoid *
, which can be converted to any pointer type back and forth without any loss of information. Note that this form does not use the type offoo
in themalloc()
call, so it is type-agnostic. Also,sizeof
operator doesn't need parentheses if it's used on an object (but they're needed if it's used on a type).这篇关于减少堆栈负载,内存分配在C和伊斯利铸造malloc()函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!