Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
很难理解C中的分配是如何工作的_C_Heap Memory - Fatal编程技术网

很难理解C中的分配是如何工作的

很难理解C中的分配是如何工作的,c,heap-memory,C,Heap Memory,这是一项让我头疼的锻炼任务。代码中给出了步骤,但是我完全搞不清楚 “在堆上分配堆栈\u t实例”-据我所知,它需要处理malloc(),因为我必须在堆上分配一些东西。然而,“并将实例变量设置为它”的含义确实让我感到不安 /** * @brief The details of the otherwise opaque type. */ struct stack { stack_value_t *stack; ///< the data container (the instan

这是一项让我头疼的锻炼任务。代码中给出了步骤,但是我完全搞不清楚

“在堆上分配堆栈\u t实例”-据我所知,它需要处理
malloc()
,因为我必须在堆上分配一些东西。然而,“并将实例变量设置为它”的含义确实让我感到不安

/**
 * @brief  The details of the otherwise opaque type.
 */
struct stack {
    stack_value_t *stack; ///< the data container (the instance owns this)
    stack_value_t *top;   ///< the pointer into the container where the current top data resides (has to point initially to stack-1)
    stack_value_t *full;  ///< the pointer to the last entry of the conatainer
};

stack_t *stack_new(size_t max_elements)
{
    stack_t *instance = NULL;
    // 1. allocate a stack_t instance on the heap and set the instance variable to it
    // 2. call error_fatal_errno("stack_new: instance"); if failed to allocate the memory on the heap
    // 3. allocate an array of max_elements value_t's on the heap and store its address in the stack member of the stack_t instance
    // 4. call error_fatal_errno("stack_new: stack"); if failed to allocate the memory on the heap
    // 5. set the top member of the stack_t instance to the address of the "element" before the first stack array element
    // 6. set the full member of the stack_t instance to the address of the last element of the stack array

    ---- my attempt
    stack_t instance = malloc(*instance);
    
    if (instance == NULL) {
        error_fatal_errno("stack_new: instance");
    }
    
    stack_value_t value = *malloc(max_elements)
    instance->stack = value;
    
    if (value == NULL) {
        error_fatal_errno("stack_new: stack");
    }
    
    instance->top = value[0];
    instance->full = value[sizeof(value)];  

    ----

    return instance;
}
/**
*@简要说明其他不透明类型的详细信息。
*/
结构堆栈{
stack\u value\u t*stack;//<数据容器(实例拥有此容器)
stack_value_t*top;//<指向当前top数据所在容器的指针(最初必须指向stack-1)
stack_value_t*full;//<指向conatainer最后一个条目的指针
};
堆栈\u t*堆栈\u新(大小\u t最大\u元素)
{
stack_t*instance=NULL;
//1.在堆上分配一个堆栈实例,并将实例变量设置为该实例
//2.如果未能在堆上分配内存,则调用error_fatal_errno(“stack_new:instance”)
//3.在堆上分配一个max_elements value_t的数组,并将其地址存储在stack_t实例的stack成员中
//4.如果未能在堆上分配内存,则调用error_fatal_errno(“stack_new:stack”)
//5.将堆栈实例的顶部成员设置为第一个堆栈数组元素之前的“元素”地址
//6.将堆栈实例的完整成员设置为堆栈数组最后一个元素的地址
----我的尝试
堆栈实例=malloc(*实例);
if(实例==NULL){
错误\致命\错误号(“堆栈\新:实例”);
}
堆栈\u值\u t值=*malloc(最大\u元素)
实例->堆栈=值;
如果(值==NULL){
错误\致命\错误号(“堆栈\新:堆栈”);
}
实例->顶部=值[0];
实例->完整=值[sizeof(值)];
----
返回实例;
}

你能给我一些关于我该做什么的指导吗?我对Java很有经验,C对我来说很陌生。

你已经做过了。“并将实例变量设置为它”这句话就像是说“将变量实例分配给它”

但我认为你的代码是错误的。正确的方法是:

instance = (stack_t*)malloc(sizeof(stack_t));

嗯,我可能错了,但对我来说,把full看作指向堆栈第一个条目的指针更有意义,因为我认为堆栈顶部应该是最后插入的值,在这个短语中,full和top将指向同一个成员。也就是说,您需要做的第一件事是分配stack\t instance变量。类似于
instance=malloc(sizeof(stack\u t))
。下一步是分配实际的堆栈,因为您需要分配instance->stack变量,该变量的元素数为“max\u elements”,大小为stack\u value\t(不要在这里发布代码,因为您只是要求提供指导,但请记住,在使用malloc或其他分配函数时,应该有一个sizeof()某处的接线员;))。在这之后,将
实例->顶部
实例->全部
变量分配到
实例->堆栈
中的正确位置就非常容易了。希望这将指向正确的方向

一些评论:

struct stack {
stack_value_t *stack; ///< the data container (the instance owns this)
stack_value_t *top;   ///< the pointer into the container where the current top data resides (has to point initially to stack-1)
stack_value_t *full;  ///< the pointer to the last entry of the conatainer
};

stack_t *stack_new(size_t max_elements)
{
stack_t *instance = NULL;
// 1. allocate a stack_t instance on the heap and set the instance variable to it
// 2. call error_fatal_errno("stack_new: instance"); if failed to allocate the memory on the heap
// 3. allocate an array of max_elements value_t's on the heap and store its address in the stack member of the stack_t instance
// 4. call error_fatal_errno("stack_new: stack"); if failed to allocate the memory on the heap
// 5. set the top member of the stack_t instance to the address of the "element" before the first stack array element
// 6. set the full member of the stack_t instance to the address of the last element of the stack array

---- my attempt
// As spotted by @Javier you are not calling correctly
// stack_t instance = malloc(*instance); 
// malloc will return a pointer to something not a struct or other type
instance = (stack_t*malloc(sizeof(stack_t)); 

if (instance == NULL) {
    error_fatal_errno("stack_new: instance");
}

// Same problem here : 
//stack_value_t value = *malloc(max_elements)
// You must allocate max_element elements of size stay_value size
stack_value_t *value = malloc(max_elements*sizeof(stack_value_t))
instance->stack = value;

if (value == NULL) {
    error_fatal_errno("stack_new: stack");
}

instance->top = value[0]; // good for the first
// Last element of array is index max_elements -1
// sizeof(value) is extremely rarely used as an array index
//instance->full = value[sizeof(value)]; 
 instance->full = value[max_elements-1]; 

----

return instance;
}
struct堆栈{
stack\u value\u t*stack;//<数据容器(实例拥有此容器)
stack_value_t*top;//<指向当前top数据所在容器的指针(最初必须指向stack-1)
stack_value_t*full;//<指向conatainer最后一个条目的指针
};
堆栈\u t*堆栈\u新(大小\u t最大\u元素)
{
stack_t*instance=NULL;
//1.在堆上分配一个堆栈实例,并将实例变量设置为该实例
//2.如果未能在堆上分配内存,则调用error_fatal_errno(“stack_new:instance”)
//3.在堆上分配一个max_elements value_t的数组,并将其地址存储在stack_t实例的stack成员中
//4.如果未能在堆上分配内存,则调用error_fatal_errno(“stack_new:stack”)
//5.将堆栈实例的顶部成员设置为第一个堆栈数组元素之前的“元素”地址
//6.将堆栈实例的完整成员设置为堆栈数组最后一个元素的地址
----我的尝试
//正如@Javier所发现的,你打错电话了
//堆栈实例=malloc(*实例);
//malloc将返回指向非结构或其他类型的对象的指针
实例=(stack_t*malloc(sizeof(stack_t));
if(实例==NULL){
错误\致命\错误号(“堆栈\新:实例”);
}
//同样的问题:
//堆栈\u值\u t值=*malloc(最大\u元素)
//您必须分配最大元素大小保持值大小
stack\u value\u t*value=malloc(max\u elements*sizeof(stack\u value\u t))
实例->堆栈=值;
如果(值==NULL){
错误\致命\错误号(“堆栈\新:堆栈”);
}
instance->top=value[0];//第一个很好
//数组的最后一个元素是index max_elements-1
//sizeof(value)很少用作数组索引
//实例->完整=值[sizeof(值)];
实例->完整=值[max_elements-1];
----
返回实例;
}

您仍然需要与不同的类型和结构名称(堆栈与堆栈)保持一致,或者您发布的代码中可能缺少它。

首先,您定义了两次实例,这是错误的

而不是
stack\u t instance=malloc(*instance);
write
instance=malloc(*instance);

第二:

typedef struct{
stack_value_t *stack; ///< the data container (the instance owns this)
stack_value_t *top;   ///< the pointer into the container where the current top data resides (has to point initially to stack-1)
stack_value_t *full;  ///< the pointer to the last entry of the conatainer
}stack_t;
你想要

stack_value_t *value = malloc(sizoef(*value)*max_elements);
那么它也是错误的。1.您想要的是指针而不是解引用。2.
sizeof(value)
将为您提供
stack\u value\u t*
的大小(以字节为单位),而不是元素的数量

    instance->top = value[0];
    instance->full = value[sizeof(value)]; 
你想要:

    instance->top = value;
    instance->full = value + max_elements - 1; 


你对
malloc()
的研究揭示了什么?你的C语言书或教程告诉了你什么?我确信这篇作业附带了一些幻灯片或其他信息。这是正确的,而且比使用sizeof typedo要好得多
    instance->top = value;
    instance->full = value + max_elements - 1; 
    instance->top = &value[0];
    instance->full = &value[max_elements - 1];