简体   繁体   English

为什么不使用malloc就可以使用char指针?

[英]Why can I use a char pointer without malloc?

I've programmed something similar and I'm wondering why it works... 我已经编写了类似的东西,我想知道为什么它起作用...

char* produceAString(void){
    char* myString;
    while(somethingIsGoingOn){
        //fill myString with a random amountof chars
    }
    return myString;
}

The theory tells me that I should use malloc to allocate space, when I'm using pointers. 理论告诉我,当我使用指针时,应该使用malloc分配空间。 But in this case I don't know how much space I need for myString, therefore I just skipped it. 但是在这种情况下,我不知道myString需要多少空间,因此我只是跳过了它。 But why does this work? 但是为什么这样做呢? Is it just bad code, which luckily worked for me, or is there something special behind char pointers? 仅仅是对我有用的不好的代码,还是char指针后面有什么特别的东西?

It worked due to pure chance. 它的工作完全是偶然的。 It might not work the next time you try it. 下次尝试时可能不起作用。 Uninitialized pointers can point anywhere in memory. 未初始化的指针可以指向内存中的任何地方。 Writing to them can cause an instant access violation, or a problem that will manifest later, or nothing at all. 对其进行写操作可能会导致立即访问冲突,或者稍后会出现问题,或者一无所获。

This is generally bad code, yes. 这通常是错误的代码,是的。 Also whatever compiler you use is probably not very intelligent or warnings turned off since they usually throw an error or at least a warning like "variable used uninitialized" which is completely true. 同样,无论您使用什么编译器,都可能不是很聪明,或者关闭了警告,因为它们通常会引发错误或至少发出警告,例如“变量未初始化未使用的变量”,这完全是事实。 You are in ( bad ) luck that when the code runs the point is garbage and somehow the OS allows the write ( or read ), probably you are running in debug mode? 不幸的是,当代码运行时,点很垃圾,而操作系统以某种方式允许写入(或读取),可能是在调试模式下运行? My personal experience is that in some cases its predictable what the OS will do, but you should never ever rely on those things, one example is if you build with MinGW in debug mode, the unintialized values are usualy follow a pattern or zero, in release build its usually complete random junk. 我的个人经验是,在某些情况下,它可以预测操作系统的运行方式,但是您永远都不应依赖这些东西,例如,如果您在调试模式下使用MinGW进行构建,则非初始化值通常遵循一个模式或为零,在这种情况下,发布其通常完整的随机垃圾。

Since you "point to a memory location" it must point to a valid location whenever it is an another variable ( pointing to another variable ) or allocating space at run time ( malloc ) what you are doing is neither so you basically read/write a random memory block and because of some black magic the app doesn't crash because of this, are you running on windows? 由于您“指向内存位置”,因此无论何时它是另一个变量(指向另一个变量)或在运行时分配空间(malloc),它都必须指向有效位置,因此您基本上都不是读/写一个随机内存块,并且由于某些黑魔法,该应用不会因此而崩溃,您是否正在Windows上运行? Windows 2000 or XP? Windows 2000或XP? since I know those are not as restrictive as windows since Vista, I remember that back in the day I did similar thing under Windows XP and nothing happened when it was supposed to crash. 由于我知道自Windows Vista以来,这些限制并不像Windows那样严格,因此我记得当时我在Windows XP下做了类似的事情,当它崩溃时什么也发生。

So generally, allocate or point to a memory block you want to use before you use the pointer in case you dont know how much memory you need use realloc or just simply figure out a good strategy that has the smallest footprint for your specific case. 因此,通常,在使用指针之前先分配或指向要使用的内存块,以防不知道需要使用多少内存来重新分配内存,或者只是想出一种针对特定情况占用最小内存的好的策略。

One way to see what C actually does is to change this line 了解C实际作用的一种方法是更改​​此行

char* myString;

into 进入

char* myString=(char*)0;

and break before that line with a debugger and watch the myString variable, it'll junk and if it intalizes the variable it'll be 0 then the rest of your code fail with access violation because you point "nowhere". 并在调试器的该行之前中断并观看myString变量,该变量将为垃圾,如果将变量加为零,则其余代码将失败,并出现访问冲突,因为您指向“ nowhere”。 The normal operation would be 正常操作是

char* myString=(char*)malloc(125); // whatever amount you want

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

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