English 中文(简体)
为什么Maloc难道不是Always Needed with Pointers to Char?
原标题:Why Malloc Is Not Always Needed With Pointers to Char?

为什么以下法典发挥作用:

char *p;

p="hello";

printf("%s
",p);

虽然如此,

char *p;

strcpy(p,"hello");

printf("%s
",p);

我知道,在第二例中增加p=malloc(4)将使守则发挥作用,但这正是我的问题。 为什么在第二个例子中需要小额保险,而不是第一个例子?

我对关于SO的类似问题作了研究,但没有回答。

最佳回答

<代码>p为查询人。 您需要指出thing。 在第一个案件中,

 p = "hello";

http://www.un.org/Depts/DGACM/index_french.htm

在您的二例中,您确实指出“条形状”(>p>/ 编号)是任何内容,这样就在什么地方发现“条形”(>p>/ 编号)无效。

p = malloc(some_size);

http://www.un.org/Depts/DGACM/index_french.htm 如果你保留足够的话,你可以做如下事情:strcpy(p,“hello”)是因为p<>。 确实是一个有效的记忆区,因此,通过<代码>p复制为之。 注:some_size 至少必须像你想要复制的一样大,包括<编码> string terminator。

指出:

p = "hello";
strcpy(p, "bye");

由于<代码>可储存在只读存储器上,因此你可以超标。

问题回答

<代码>p=“hello”; 指明字面“hello”地址至p,而scanf则需要一个位置来提供扫描材料,因此,你需要为之分配一些记忆(静态、动态或自动)。

strcpy(p,"hello");

关于打字字字字面<代码>hello, 即copied,p必须注明一个有效的记忆地点,在此情况下为t。

由于<代码>p的价值是随机的,任何复制该编码的企图都将造成坠毁。 通过使用小型用户,确保<代码>p的数值为制作复印件(只要缓冲量大得多)

"hello" is a string literal and will live in part of your output program. The memory is allocated by the compiler, at compile time, in the same place as the actual code*

也就是说,<代码>hello>的类型为const char[6],自动改为char *。 (Using const char* und char* for stringlichs is a good habit to goging

在第二种情况下,<代码>p在请上strcpy时,即为无内证,因此结果没有界定。 p= Smalloc(4)>is not sufficient to fix this or, the string "hello" is made of 6 natures - 5 from the term hello se + /code> to end the string.

<> 实际上,在不太真实的现代制度中,它有near。 a 与法典相同。

我发现,在类似局势中,情况是暗淡的。

请允许我把上述两条原则结合起来:

char *p = "Hello";
char *q;

strcpy(q, "goodbye");
printf("p = %s, q = %s
", p, q);

缩略语

Item        Address           0x00  0x01  0x02  0x03
----        -------           ----  ----  ----  ----
  "Hello"   0x00080000         H     e     l     l 
            0x00080004         o    0x00  0x??  0x??
"goodbye"   0x00080008         g     o     o     d 
            0x0008000C         b     y     e    0x00
            ...
        p   0x01010000        0x00  0x08  0x00  0x00
        q   0x01010004        0x??  0x??  0x??  0x??

Hello" and goodbye>> are stringlichs, which are ranges of char/code> (const char in C++), Deposit in such as a way that they areactive over the life of the program. 字面<代码>Hello"从0x00080000地址开始储存,字面“正比”从0x00080008地址开始储存。

<代码>pq>为char/code>,auto ,意指在所申报的整块的有效期上只存在。 在这种情况下,他们分别位于0-x01010000和0x01010004号地址(此处改用32个轨道点)。

如上面的记忆图所示,在你撰写<代码>char*p = “Hello”;Hello>时,阵列的表述被转换成点表示,其价值是阵列第1部分的位置,点值被复制到p

如<代码>0x?所示,<代码>q的初始价值为“t1。 这种价值可能或可能与可仲裁的地址相对应;奇怪的是,该数值为t。 因此,在你撰写<代码>strcpy(q,“Goodbye”)时,你重新尝试将<代码>内容“odbye”贴在字面上的随机地点。 这往往会导致时间错误。

如果你为扼杀工作分配缓冲,缓冲必须足够长,以储存整个护堤加零点;仅仅分配4个 by,就足够了,因为这样,你的扼杀就会蔓延到你们的记忆中,不会“own”,可能掩盖一些重要的东西(从技术上说,行为是没有定义的,意味着任何事情都会发生)。 你们不必为点人(在你宣布点子时已经做过)分配记忆,你必须给大家留下记忆。

如果我们把我们的法典改成像我们这样的东西。

char *p = "Hello";
char *q;

q = malloc(10);
strcpy(q, "goodbye");
printf("p = %s, q = %s
", p, q);

那么,我们的记忆图将看这样的情况:

Item        Address           0x00  0x01  0x02  0x03
----        -------           ----  ----  ----  ----
  "Hello"   0x00080000         H     e     l     l 
            0x00080004         o    0x00  0x??  0x??
"goodbye"   0x00080008         g     o     o     d 
            0x0008000C         b     y     e    0x00
            ...
        p   0x01010000        0x00  0x08  0x00  0x00
        q   0x01010004        0x40  0x00  0x00  0x00
            ...
<dynamic>   0x40000000         g     o     o     d 
            0x40000004         b     y     e    0x00
            0x40000008        0x??  0x??

在这种情形下, Smalloc在0x40000美元开始的记忆中留出10个缩略语,并在q上填写。 电话strcpy 然后,将“货物”字面的“货物”内容复制到该地点。


1If q had been declared as static or at file scope (outside of any function) , then it would have been initialized to 0 (0x00000000).




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...