Redian新闻
>
这个C++程序为什么不能运行
avatar
这个C++程序为什么不能运行# Programming - 葵花宝典
b*n
1
int main(int argc, char* argv[])
{
char *a="abcde";
a[0] = 'f';
cout<return 0;
}
compile没问题(Visual Studio 6)
如果定义char a[]="abcde";
就没问题
C++里面char *a和char a[]不都是一样的吗?
谢谢
avatar
z*i
2
Could it be:
char *a ---> pointer to read only section
char a[]---> array in stack, initialized with "abcd", which is in read only
section.

【在 b*********n 的大作中提到】
: int main(int argc, char* argv[])
: {
: char *a="abcde";
: a[0] = 'f';
: cout<: return 0;
: }
: compile没问题(Visual Studio 6)
: 如果定义char a[]="abcde";
: 就没问题

avatar
m*s
3

是不是abcde被看成只读常量?

【在 b*********n 的大作中提到】
: int main(int argc, char* argv[])
: {
: char *a="abcde";
: a[0] = 'f';
: cout<: return 0;
: }
: compile没问题(Visual Studio 6)
: 如果定义char a[]="abcde";
: 就没问题

avatar
j*k
4
allocate memory for a first before you assign it with some value

【在 b*********n 的大作中提到】
: int main(int argc, char* argv[])
: {
: char *a="abcde";
: a[0] = 'f';
: cout<: return 0;
: }
: compile没问题(Visual Studio 6)
: 如果定义char a[]="abcde";
: 就没问题

avatar
d*a
5
c++? if you really want c++, use string instead of char * and you'll get rid
of most of this kind trouble.
first basic things to use c++ instead of c:
alloc, malloc/free -> new/delete
char [], char* -> string
array -> vector
avatar
s9
6
a[0] = 'f'出错是因为它试图去修改一个不允许修改的字符串常量。
char *a="abcde"; a是一个处于堆栈的指针,其值是"abcde"这个字符串常量的地址。
长度是4(32 bit PC)。
char a[]="abcde" a是一个处于堆栈的数组,其长度取决于其所包含的字符串的长度。

【在 b*********n 的大作中提到】
: int main(int argc, char* argv[])
: {
: char *a="abcde";
: a[0] = 'f';
: cout<: return 0;
: }
: compile没问题(Visual Studio 6)
: 如果定义char a[]="abcde";
: 就没问题

avatar
c*t
7
check this link
http://c-faq.com/decl/strlitinit.html

【在 b*********n 的大作中提到】
: int main(int argc, char* argv[])
: {
: char *a="abcde";
: a[0] = 'f';
: cout<: return 0;
: }
: compile没问题(Visual Studio 6)
: 如果定义char a[]="abcde";
: 就没问题

avatar
s*e
8
应该有Warning吧?应该这么写
const char* str="abcd";
avatar
d*y
9
char *a;是一个指针,没有ASSIGN存储空间.
char *a="abcde"; 是给这个指针一个值"abcde","abcde"可能会被TRUNCTED,depending
on your system.
指针和数组是有本质区别的.
avatar
c*n
10
I just did some test. See the program below:
===============================
#include
#include
#include
int main(int argc, char* argv[])
{
char *a="abcde";
char b[]="abcde";
char* c=(char*)malloc(6);
strcpy(c, a);
printf("%lx, %lx, %lx\n", a, b, c);
printf("%c %c %c\n", a[0], b[0], c[0]);
a[0] = 'f';
printf("%s\n", a);
return 0;
}
==============================
output:
80486d0, bfc74b96, 804b008
a a a
Segmentation fault
char *a="abcde", the compiler generate a str
avatar
k*f
11
C老问题拉,去精华区看看吧
sizeof(a)和sizeof(b)是不一样的

【在 c****n 的大作中提到】
: I just did some test. See the program below:
: ===============================
: #include
: #include
: #include
: int main(int argc, char* argv[])
: {
: char *a="abcde";
: char b[]="abcde";
: char* c=(char*)malloc(6);

avatar
b*y
12
It is a pure C question rather than C++.
char *a = "abcde";
a is not in stack or heap. It is in the memory which is read only. Any
attempt to change the content of a is wrong.
avatar
b*n
13
a is just a pointer. It is initialised to point to read-only memory. Changin
g a is fine, changing the content of memory pointed by a is not okay

【在 b**y 的大作中提到】
: It is a pure C question rather than C++.
: char *a = "abcde";
: a is not in stack or heap. It is in the memory which is read only. Any
: attempt to change the content of a is wrong.

avatar
y*a
14
下面是汇编代码,string "abcde" is in readonly section (.rodata). 除非你修改
gcc default linker script, you cannot write to this address
.file "l.c"
.section .rodata
.LC0:
.string "abcde"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
avatar
d*a
15
niu!

【在 y*****a 的大作中提到】
: 下面是汇编代码,string "abcde" is in readonly section (.rodata). 除非你修改
: gcc default linker script, you cannot write to this address
: .file "l.c"
: .section .rodata
: .LC0:
: .string "abcde"
: .text
: .globl main
: .type main, @function
: main:

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。