简体   繁体   English

这是指针吗? (如果是这样,它是如何初始化的?)

[英]Is this a pointer? (And if so, how was it initialized?)

There's a header file, esUtil.h, with a definition for a structure called ESContext, and one of its members is userData. 有一个头文件esU​​til.h,其中包含一个名为ESContext的结构的定义,其成员之一是userData。 userData is a pointer to void. userData是指向void的指针。

The body of a program using it is this, briefly: 简而言之,使用它的程序的主体是这样的:

#include "esUtil.h"

typedef struct {
 GLuint programObject;
} UserData;

int DoSomething(ESContext *esContext) {
 UserData *userData = esContext->userData;

 ...
}

int main(int argc, char *argv[]) {
 ESContext esContext;
 UserData userData;

 esStart(&esContext);
 esContext.userData = &userData;

 ...
 if(!DoSomething(&esContext))
  return 0;

 ...
}

I'm confused by what "*userData" is in the statement: UserData *userData = esContext->userData; 我对语句中的“ * userData”感到困惑:UserData * userData = esContext-> userData;

And if it is a pointer, how it came into being without being declared. 如果它是一个指针,它是如何在没有声明的情况下产生的。 Thanks for any explanations. 感谢您的任何解释。

Yes, it is a pointer. 是的,它是一个指针。 The line 线

UserData *userData = esContext->userData;

declares a variable called userData with type UserData * (a pointer to UserData ) and initializes it with the value esContext->userData . 声明一个类型为UserData * (指向UserData的指针)的名为userData的变量,并使用esContext->userDataesContext->userData初始化。

Some where in the code the member userData of ESContext might be initialized with the structure UserData and thats why it is assigned to UserData pointer in DoSomething function. 在代码中的某些地方,ESContext的成员userData可能使用结构UserData初始化,这就是为什么在DoSomething函数中将其分配给UserData指针的原因。

It may be a void pointer but still it might hold the address of UserData object 它可能是一个空指针,但仍然可以保存UserData对象的地址

userData is indeed a pointer. userData确实是一个指针。 It's declared as a pointer type (the star in the type name is a tip-off), and this code presumably compiles, so it must be a pointer type. 它被声明为指针类型(类型名称中的星星是尖尖的),并且该代码可能会编译,因此它必须是指针类型。 And unless any of the code you didn't post deals with userData (in main), yes userData is uninitialized. 除非任何没有用张贴交易代码的userData (主),是userData的初始化。

You ask "how it came into being without being initialized." 您问“它是如何在没有初始化的情况下产生的”。 Well, in mainstream C/C++ implementations, pointers are really just integers whose size is equal to the word size on the system (32 bits on a 32-bit system, 64 bits on a 64-bit system). 好吧,在主流的C / C ++实现中,指针实际上只是整数,其大小等于系统上的字长(32位系统上为32位,64位系统上为64位)。 If you dereference a pointer, the CPU goes out to the address that the pointer represents, and fetches the associated data. 如果取消引用指针,则CPU会转到该指针表示的地址,并获取关联的数据。 If the data isn't there, your program goes kaboom (on *nix, segfaults) if you're lucky and does weird things if you aren't. 如果数据不存在,那么如果幸运的话,程序将运行kaboom(在* nix上出现segfaults),如果不存在,则程序将执行奇怪的操作。 In this case, main's userData is declared , so the data actually is there. 在这种情况下,main的userData被声明,因此数据实际上在那里。 However, it's not initialized, so the contents of userData could be anything. 但是,它尚未初始化,因此userData的内容可以是任何内容。 As long as DoSomething doesn't use the contents of userData , it's OK because it's just manipulating that address. 只要DoSomething不使用userData的内容,就可以了,因为它只是在操纵该地址。 But if it tries to dereference userData , the CPU will go out to memory and pull in garbage, at which point the program could do all kinds of weird things because any bit pattern is possible inside userData , so any calculations on userData could end up with all kinds of different results. 但是,如果尝试取消对userData的引用,则CPU将进入内存并放入垃圾,这时程序可能会做各种奇怪的事情,因为userData内可能存在任何位模式,因此对userData任何计算都可能最终导致各种不同的结果。

The C/C++ language specs say that using uninitialized values gives undefined behavior, which really is undefined , meaning that the compiler could insert code to erase your hard drive, or send threatening messages to Kernighan and Ritchie, or do anything it likes whenever you use uninitialized values. C / C ++语言规范指出,使用未初始化的值会产生未定义的行为,这实际上是未定义的 ,这意味着编译器可以插入代码以擦除硬盘驱动器,或向Kernighan和Ritchie发送威胁性消息,或者在您使用时执行任何喜欢的操作未初始化的值。 But the code that real compilers generate just gives garbage bit patterns (usually leftover stack data from previous function calls). 但是,真正的编译器生成的代码仅给出垃圾位模式(通常是先前函数调用中剩下的堆栈数据)。 So, while using uninitialized values is wrong , it won't kill you. 因此,尽管使用未初始化的值是错误的 ,但它不会杀死您。

All of this weirdness about uninitialized values and bad pointers is one of the most visible ways in which C and C++ are unsafe languages. 所有关于未初始化值和错误指针的怪异都是C和C ++是不安全语言的最明显的方式之一。 Eric Lippert explains another way in which C and C++ are unsafe here . 埃里克利珀解释了另一种方式的C和C ++是不安全的位置

它是一个指针,并由以下行初始化:

esContext.userData = &userData;

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

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