简体   繁体   English

无法访问C中的结构的值

[英]Unable to access the values of a struct in C

I am currently learning to code C, but I am stuck at creating a struct on the heap. 我目前正在学习编码C,但是我坚持在堆上创建一个结构。 I have got the following code: 我有以下代码:

#include <stdio.h>

typedef struct{
    int a;
    int b;
} some_struct;

int main(int argc, char ** argv) {
    printf("%i", sizeof(some_struct));
    some_struct * p_struct = malloc(sizeof(some_struct));
    p_struct->a = 600;
    p_struct->b = 100;
    return 0;
}

But when executed Visual Studio tells me: 但是执行时,Visual Studio告诉我:

Unhandled exception at 0x00007FF6286C1E39 in test.exe: 0xC0000005: Access violation writing location 0xFFFFFFFFAFBE4440. test.exe中0x00007FF6286C1E39的未处理异常:0xC0000005:访问冲突写入位置0xFFFFFFFFAFBE4440。

Also the debugger tells me the memory of both a and b is unable to be read. 调试器还告诉我a和b的内存都无法读取。 I understand that the two integers should be uninitialized, but why is their memory seemingly unreadable? 我知道这两个整数应该未初始化,但是为什么它们的内存看似不可读?

Looking at your address, it looks like the lack of prototype for malloc() is the issue. 查看您的地址,似乎是缺少malloc()原型的问题。 Do: 做:

#include <stdlib.h>

The implicit int causes the value returned by malloc() to be truncated (assuming 64 bit addresses and 32 bit ints). 隐式int导致malloc()返回的值被截断(假定64位地址和32位int)。 Hence, thus producing an invalid address. 因此,从而产生无效的地址。

In pre-C99, if a protoype is not found for a function, compiler implicitly declares a prototype with int . 在C99之前的版本中,如果未找到函数的原型,则编译器将使用int隐式声明一个原型。 But this is no longer valid since C99. 但是,自C99起,这不再有效。

You should #include <stdlib.h> which contains malloc . 您应该#include <stdlib.h>包含malloc Doesn't Visual Studio give you any warnings? Visual Studio不会给您任何警告吗?

When you don't include <stdlib.h> , malloc is given an implicit return type of int . 当您不包括<stdlib.h> ,将为malloc赋予int的隐式返回类型。 This might work on a platform where sizeof(int) == sizeof(some_struct*) , but if they're of different size (eg 32 bit ints and 64 bit pointers), the value returned by malloc is truncated before assignment. 可能sizeof(int) == sizeof(some_struct*)的平台上工作,但是如果它们的大小不同(例如32位ints和64位指针),则malloc返回的值在赋值之前会被截断。

The value in the error message ( 0xFFFFFFFFAFBE4440 ) suggests that this is the case: malloc returned a pointer with a value of 0x????????AFBE4440 , which was truncated to 0xAFBE4440 , which was sign extended to 0xFFFFFFFFAFBE4440 on assignment. 错误消息中的值( 0xFFFFFFFFAFBE4440 )表明是这种情况: malloc返回了一个值为0x????????AFBE4440的指针,该指针被截断为0xAFBE4440 ,并在赋值时被符号扩展为0xFFFFFFFFAFBE4440

It is happend because you call the function malloc but you forgot (or you did not know) to include stdlib.h. 之所以发生这种情况,是因为您调用了函数malloc,但却忘记了(或您不知道)包含stdlib.h。

Here is the working code: 这是工作代码:

#include <stdio.h>
#include<stdlib.h>

typedef struct{
    int a;
    int b;
} some_struct;

int main(void) {
    printf("%lu", sizeof(some_struct));
    some_struct * p_struct = malloc(sizeof(some_struct));
    p_struct->a = 600;
    p_struct->b = 100;
    return 0;
}

You probably noticed that i changed: 您可能注意到我改变了:

printf("%i", sizeof(some_struct)); printf(“%i”,sizeof(some_struct));

With: 带有:

printf("%lu", sizeof(some_struct)); printf(“%lu”,sizeof(some_struct));

Becouse if you turn on your compiler settings you will get: 因为如果您打开编译器设置,您将获得:

format '%i' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Werror=format=]| 格式'%i'期望的参数类型为'int',但是参数2的类型为'long unsigned int'[-Werror = format =] |

NOTE: Using %i instead of %d its ok in here but will not be the same with scanf. 注意:在这里使用%i代替%d可以,但是与scanf会不同。

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

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