将内存块分配给结构
Assigning memory block to structures
请看下面的代码:
#include <stdio.h>
#include <stdlib.h>
typedef int item_t;
typedef struct {item_t *base; item_t *top; int size;} stack_t;
stack_t *create_stack(int size)
{ stack_t *st;
/* Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?
*/
st = (stack_t *) malloc( sizeof(stack_t) );
/* How is this related to the above allocation of memory to st?
this code moves the pointer address to base of the stack. How does the
above syntax related to this?
*/
st->base = (item_t *) malloc( size * sizeof(item_t) );
st->size = size;
st->top = st->base;
return( st );
}
int main()
{ stack_t *st;
char nextop;
st = create_stack(50);
}
请参阅我提出问题的代码中的注释。
这只是用来在使用前给st指针分配一些内存吗?这个内存分配有必要吗?
st = (stack_t *) malloc( sizeof(stack_t) );
是的,需要分配st
。在使用指针之前,它必须指向某物。您可以将它设置为某个已声明变量的地址,使用 malloc()
分配内存,或复制另一个指向它的指针。
如果程序中只需要固定数量的栈,可以声明为全局变量而不是动态分配。在函数之外你可以声明:
stack_t the_stack;
然后您可以在 create_stack
中使用 st = &the_stack
。
关于base
,您确实需要分配它。除非您想硬编码堆栈的最大大小,否则您需要能够在该数组变满时重新分配它。这只能使用动态分配来完成。
Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?
是的,这个是用来给st
分配内存再使用的。再次是的,这是必要的,因为没有这个分配,st
指向任何地方。
this code moves the pointer address to base of the stack.
不,不是这样的。只是简单的给st
中的base
成员变量分配内存。同样,如果没有此分配,st->base
无处可去。
注意事项:请do not castmalloc()
和家人的结果
您的第一个问题(在评论中),malloc()
为定义堆栈的结构分配内存,但该结构尚未初始化以供使用。
第二个问题(在注释中),malloc()
为 size
个整数堆栈分配内存(来自 typedef),将最大堆栈大小设置为 size
,并设置栈顶到底部,即栈为空。
我对该方法的看法是,您应该存储堆叠的项目数和堆叠大小,或当前堆栈指针和最大栈顶指针,但这两者都不是。
指针的大小是一个int的大小,指针只是存储一个内存地址。您使用 malloc 函数分配的内存将分配等于结构大小的内存,并将内存开头的地址分配给 int 大小的指针。因此,当您第一次分配内存时,它将分配 3*(sizeof(int)) 这么多内存(2 个指针和 1 个 int 变量)。现在这两个指针不指向任何地方。所以在第二个 malloc 调用中,你分配的内存等于 int 的大小,并将分配的内存的位置分配给指针。
请看下面的代码:
#include <stdio.h>
#include <stdlib.h>
typedef int item_t;
typedef struct {item_t *base; item_t *top; int size;} stack_t;
stack_t *create_stack(int size)
{ stack_t *st;
/* Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?
*/
st = (stack_t *) malloc( sizeof(stack_t) );
/* How is this related to the above allocation of memory to st?
this code moves the pointer address to base of the stack. How does the
above syntax related to this?
*/
st->base = (item_t *) malloc( size * sizeof(item_t) );
st->size = size;
st->top = st->base;
return( st );
}
int main()
{ stack_t *st;
char nextop;
st = create_stack(50);
}
请参阅我提出问题的代码中的注释。
这只是用来在使用前给st指针分配一些内存吗?这个内存分配有必要吗?
st = (stack_t *) malloc( sizeof(stack_t) );
是的,需要分配st
。在使用指针之前,它必须指向某物。您可以将它设置为某个已声明变量的地址,使用 malloc()
分配内存,或复制另一个指向它的指针。
如果程序中只需要固定数量的栈,可以声明为全局变量而不是动态分配。在函数之外你可以声明:
stack_t the_stack;
然后您可以在 create_stack
中使用 st = &the_stack
。
关于base
,您确实需要分配它。除非您想硬编码堆栈的最大大小,否则您需要能够在该数组变满时重新分配它。这只能使用动态分配来完成。
Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?
是的,这个是用来给st
分配内存再使用的。再次是的,这是必要的,因为没有这个分配,st
指向任何地方。
this code moves the pointer address to base of the stack.
不,不是这样的。只是简单的给st
中的base
成员变量分配内存。同样,如果没有此分配,st->base
无处可去。
注意事项:请do not castmalloc()
和家人的结果
您的第一个问题(在评论中),malloc()
为定义堆栈的结构分配内存,但该结构尚未初始化以供使用。
第二个问题(在注释中),malloc()
为 size
个整数堆栈分配内存(来自 typedef),将最大堆栈大小设置为 size
,并设置栈顶到底部,即栈为空。
我对该方法的看法是,您应该存储堆叠的项目数和堆叠大小,或当前堆栈指针和最大栈顶指针,但这两者都不是。
指针的大小是一个int的大小,指针只是存储一个内存地址。您使用 malloc 函数分配的内存将分配等于结构大小的内存,并将内存开头的地址分配给 int 大小的指针。因此,当您第一次分配内存时,它将分配 3*(sizeof(int)) 这么多内存(2 个指针和 1 个 int 变量)。现在这两个指针不指向任何地方。所以在第二个 malloc 调用中,你分配的内存等于 int 的大小,并将分配的内存的位置分配给指针。