简体   繁体   English

使用malloc将内存分配给结构

[英]allocating memory to a struct using malloc

I have a struct called State: 我有一个叫做State的结构:

typedef struct State{
    char alphabets[2][6]; 
    struct State *PREV; /*this points to the previous state it came from*/
    struct State *NEXT; /*this points to the next state in the linked list*/
    int cost; /*Number of moves done to get to this position*/
    int zero_index;/*this holds the index to the empty postion*/
} State;

Here's my memAllocator() method: 这是我的memAllocator()方法:

memAllocator(){
struct State *p = (State*) malloc(sizeof(State));
if (p==NULL){
        printf("Malloc for a new position failed");
        exit(1);
}
return p;

} Here's my main method. 这是我的主要方法。

main(){
State *start_state_pointer=memAllocator();
State start_state;
start_state.zero_index=15;
start_state.PREV = NULL;
start_state.alphabets[0][0]='C';
start_state.alphabets[0][1]='A';
start_state.alphabets[0][2]='N';
start_state.alphabets[0][3]='A';
start_state.alphabets[0][4]='M';
start_state.alphabets[0][5]='A';
start_state.alphabets[1][0]='P';
start_state.alphabets[1][1]='A';
start_state.alphabets[1][2]='N';
start_state.alphabets[1][3]='A';
start_state.alphabets[1][4]='L';
start_state.alphabets[1][5]='_';
start_state_pointer=&(start_state);
/*start_state=*start_state_pointer;*/

} }

I think the statement start_state_pointer=&(start_state); 我认为语句start_state_pointer =&(start_state); is just assigning the pointer start_state_pointer to to the small amount of temporary space created during State start_state, rather than to the space I allocated. 只是将指针start_state_pointer分配给State start_state期间创建的少量临时空间,而不是分配给我分配的空间。 But when I try the commented out statement start_state=*start_state_pointer to deference the pointer and allocate the space to start state. 但是,当我尝试使用注释掉的语句start_state = * start_state_pointer来引用指针并为启动状态分配空间时。 It gives me a segmentation fault. 它给了我一个分割错误。

I am just starting out in C. Can some one help me with this? 我刚从C开始。有人可以帮助我吗?

Your memAllocator and main functions don't have explicit return types. 您的memAllocatormain函数没有显式的返回类型。 This style of code has been deprecated for over 10 years. 这种样式的代码已被弃用了10多年。 Functions in C should always have a return type. C中的函数应始终具有返回类型。 For main , the return type should be int , and for your memAllocator function, it should be State * . 对于main ,返回类型应该为int ,对于您的memAllocator函数,返回类型应该为State *

The second issue is that you allocate space for a State struct, but then fill a different State struct and overwrite the pointer to the previously allocated State struct using start_state_pointer = &(start_state); 第二个问题是,您为State结构分配了空间,然后填充了另一个 State结构,并使用start_state_pointer = &(start_state);覆盖了先前分配的State结构的指针start_state_pointer = &(start_state); .

To use the memory that you just allocated, you want to use something like this: 要使用刚刚分配的内存,您需要使用以下代码:

State *start_state = memAllocator();
start_state->zero_index = 15;
start_state->PREV = NULL;
start_state->alphabets[0][0] = 'C';
// etc.

There is no need to create two State structs. 无需创建两个State结构。 When you use State start_start; 当您使用State start_start; in your original code, you are creating a struct with something called automatic storage . 在您的原始代码中,您正在创建带有自动存储的结构。 This means the space for this struct is allocated automatically and is deallocated automatically for you at the end of the scope it is declared in. If you take the address of this struct and pass it around other parts of your program, then you will be passing around a pointer to a deallocated struct, and this could be why your program is crashing. 这意味着该结构的空间是自动分配的,并在声明它的作用域的末尾自动为您释放。如果您采用该结构的地址并将其传递给程序的其他部分,那么您将传递围绕指向已释放结构的指针,这可能就是程序崩溃的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM