减少堆栈负载,内存分配在C和伊斯利铸造malloc()函数的返回值 [英] Reducing stack load, memory allocation in C and easly casting malloc()'s return value

查看:113
本文介绍了减少堆栈负载,内存分配在C和伊斯利铸造malloc()函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 据了解,大局部/全局变量可能会导致堆栈溢出。
    我知道,使用指针和在内存中分配空间有助于克服这个问题。
    但它是唯一的选择?如果我发生什么(或需要)在全球范围内过于许多指针?


  2. 关于堆栈空间:是一个全球性结构类型的变量发生在栈空间,或者就像一个指针?我是否需要以减少堆栈的负载创建一个结构体变量类型的指针?


  3. 执行以下操作code也分配内存为char **命名变量 BIG


    //在头文件
    typedef结构MYSTRUCT {
        BIG [256] [256];
        INT巴兹;
    } MYSTRUCT;
    //在C文件
    MYSTRUCT * foo的;
    富=(MYSTRUCT *)malloc的(sizeof的(*富));


  4. 我怎么能轻易投的返回值的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 操作员不需要括号。

  1. 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?

  2. 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?

  3. 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) ); 
    

  4. 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 named BIG?

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 for BIG, although BIG is not of type char **. BIG is of type "array [256] of array [256] of char". In value contexts, it is equivalent to "pointer to array [256] of char".


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() returns void *, 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 of foo in the malloc() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆