为什么我可以在不使用malloc的情况下使用char指针? [英] Why can I use a char pointer without malloc?

查看:182
本文介绍了为什么我可以在不使用malloc的情况下使用char指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了类似的东西,我想知道为什么它起作用...

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;
}

理论告诉我,当我使用指针时,应该使用malloc分配空间.但是在这种情况下,我不知道myString需要多少空间,因此我只是跳过了它. 但是为什么这样做呢?仅仅是对我有用的不好的代码,还是char指针后面有什么特别的东西?

The theory tells me that I should use malloc to allocate space, when I'm using pointers. But in this case I don't know how much space I need for myString, therefore I just skipped it. But why does this work? Is it just bad code, which luckily worked for me, or is there something special behind char pointers?

推荐答案

这通常是错误的代码,是的.同样,无论您使用什么编译器,都可能不是很聪明,或者关闭了警告,因为它们通常会引发错误,或者至少会发出警告,例如变量未初始化未使用的变量",这是完全正确的. 不幸的是,当代码运行时,点很垃圾,而操作系统以某种方式允许进行写(或读)操作,这很可能是在调试模式下运行的吗? 我的个人经验是,在某些情况下,它可以预测操作系统的工作方式,但是您永远不要依赖这些东西,例如,如果您在调试模式下使用MinGW进行构建,则非初始化的值通常遵循一个模式或为零,在这种情况下,发布通常会完全随机的垃圾.

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.

由于您指向内存位置",因此无论何时它是另一个变量(指向另一个变量)或在运行时分配空间(malloc),它都必须指向有效位置,因此您基本上都不会读/编写一个随机的内存块,由于某种黑魔法,该应用程序不会因此而崩溃,您是否正在Windows上运行? Windows 2000或XP?因为我知道自Windows Vista以来,这些限制不如Windows严格,所以我记得当时我在Windows XP下做过类似的事情,什么也没发生,当它崩溃时,没有发生.

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? Windows 2000 or 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.

因此,通常,在使用指针之前先分配或指向要使用的内存块,以防万一您不知道需要使用多少内存重新分配,或者只是想出一种针对特定内存占用最少的好策略情况.

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.

查看C实际作用的一种方法是更改​​此行

One way to see what C actually does is to change this line

char* myString;

进入

char* myString=(char*)0;

并在调试器的该行之前中断并观看myString变量,该变量将为垃圾,如果将变量加为0,则其余代码将失败,并出现访问冲突,因为您指向"nowhere". 正常操作是

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". The normal operation would be

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

这篇关于为什么我可以在不使用malloc的情况下使用char指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆