将新char重新分配给char指针导致崩溃。 [英] Reassign new char to char pointer cause crash.

查看:150
本文介绍了将新char重新分配给char指针导致崩溃。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码非常简单。



The code is really simple.

char *foo = "000"; // foo->"000\0"
*(foo+0) = '1'; // Crash | what i expecting is foo->"100\0"
*(foo+1) = (char)'2'; // Crash | what i expecting is foo->"120\0"
*(foo+2) = '3'; // Crash | what i expecting is foo->"123\0"





IMO





IMO

char *foo = "000";



创建新的字符指针和指向字符串000\0的指针。




Create new char pointer and pointer to the string "000\0".

*(foo+0) = '1';



或只是


or just

*foo = '1';



取消引用指针并指定新的char指向指针的地址。



我尝试过:




dereference the pointer and assign new char to the address of the pointer pointing to.

What I have tried:

char *foo = malloc(4); // codes work like a charm.
*(foo+0) = '1';
*(foo+1) = '2';
*(foo+2) = '3';
*(foo+3) = '\0';



这个问题可以通过使用malloc()解决,但不是以前的代码处理堆栈,而这个处理堆?


This issue can be solve by using malloc(), but isn't the previous code dealing with "Stack", and this one dealing with "Heap"?

推荐答案

你的foo指针在堆栈上分配。但它指向的存储 - 字符串文字000 - 不是。它可能驻留在写保护的内存中。这可以通过编译器开关更改(请参阅编译器文档)。但我会说无论如何修改字符串文字的存储是一种不好的做法。使用堆栈或堆分配存储(通过malloc或new)。



下面给出了分配存储的示例:



Your foo pointer is allocated on the stack. But the storage it points to -- the string literal "000" -- is NOT. It probably resides in write-protected memory. That can possibly be changed by a compiler switch (see your compiler documentation). But I would say that it is a bad practice anyhow to modify the storage of a string literal. Use either stack or heap allocated storage (via malloc or new).

The following gives an example for allocated storage:

char buffer[10];
buffer[0] = '1';


字符串文字在c(和c ++)标准中被视为 const STR30-C。不要尝试修改字符串文字 - SEI CERT C编码标准 - CERT安全编码标准 [ ^ ]



见修改字符串文字: C ++字符串文字 [ ^ ]



另请参阅: c ++ - 为什么是字符串文字常量? - 堆栈溢出 [ ^ ]



修改字符串文字导致未定义的行为。
String literals are treated as const in the c (and c++) standard. STR30-C. Do not attempt to modify string literals - SEI CERT C Coding Standard - CERT Secure Coding Standards[^]

See "Modifying String Literals": C++ String Literals[^]

See also: c++ - Why are string literals const? - Stack Overflow[^]

Modifying a string literal results in undefined behaviour.


char *foo = "000"; // foo->"000\0"
*(foo+0) = '1'; // Crash | what i expecting is foo->"100\0"
*(foo+1) = (char)'2'; // Crash | what i expecting is foo->"120\0"
*(foo+2) = '3'; // Crash | what i expecting is foo->"123\0"



在此代码中,000是程序的一部分,并受到保护变化。如果允许的话,你最终会得到一个自修改程序。




In this code, the "000" is part of the program and is protected against changes. If it was allowed, you would end up with a self modifying program.

char *foo = malloc(4); // codes work like a charm.
*(foo+0) = '1';
*(foo+1) = '2';
*(foo+2) = '3';
*(foo+3) = '\0';



这里,你在允许更改的空间中分配一些内存,它是存储变量数据的地方。




Here , you allocate some memory in a space that allow changes, it is where your variables data is stored.

引用:

这个问题可以通过使用malloc()解决,但不是以前处理Stack的代码,而这个处理堆?

This issue can be solve by using malloc(), but isn't the previous code dealing with "Stack", and this one dealing with "Heap"?



在这两个代码中,指针都在堆栈上,但首先,它指向exe代码并且不可更改,第二个数据在堆中。


In both codes, the pointer is on stack, but in first, it is pointing to exe code and is not changeable, in second data is in heap.


这篇关于将新char重新分配给char指针导致崩溃。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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