由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这个C++程序为什么不能运行
相关主题
C++ 初级再初级问题C的argc问题
A aimple C++ questionconst 指针类型转换
大侠们救命, C++ operator new 问题问个fork cow的问题
奇怪的问题:关于一个简单的malloc()小程序 (转载)Use Visual .NET for C++ programming
char ** pt1和 char * pt2[] 的区别在哪?三个C syntax 弱问题
What is wrong with the code?a question on C++ string
为什么foo1可以而foo2不行?请问C++中局部未使用的变量在优化的时候会去掉么?
菜鸟求教,一个c++的困惑A helloworld OpenMP question?
相关话题的讨论汇总
话题: char话题: abcde话题: c++话题: eax话题: main
进入Programming版参与讨论
1 (共1页)
b*********n
发帖数: 1258
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[]不都是一样的吗?
谢谢
z******i
发帖数: 59
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";
: 就没问题

m**s
发帖数: 221
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";
: 就没问题

j*****k
发帖数: 1198
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";
: 就没问题

d*****a
发帖数: 110
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
s9
发帖数: 1192
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";
: 就没问题

c*********t
发帖数: 2921
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";
: 就没问题

s******e
发帖数: 431
8
应该有Warning吧?应该这么写
const char* str="abcd";
d****y
发帖数: 701
9
char *a;是一个指针,没有ASSIGN存储空间.
char *a="abcde"; 是给这个指针一个值"abcde","abcde"可能会被TRUNCTED,depending
on your system.
指针和数组是有本质区别的.
c****n
发帖数: 105
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
相关主题
What is wrong with the code?C的argc问题
为什么foo1可以而foo2不行?const 指针类型转换
菜鸟求教,一个c++的困惑问个fork cow的问题
进入Programming版参与讨论
k****f
发帖数: 3794
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);

b**y
发帖数: 74
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.
b******n
发帖数: 592
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.

y*****a
发帖数: 171
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
d*****a
发帖数: 110
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:

1 (共1页)
进入Programming版参与讨论
相关主题
A helloworld OpenMP question? char ** pt1和 char * pt2[] 的区别在哪?
请教C++问题What is wrong with the code?
还是咱们这儿,亲。为什么foo1可以而foo2不行?
没有经过构造函数???菜鸟求教,一个c++的困惑
C++ 初级再初级问题C的argc问题
A aimple C++ questionconst 指针类型转换
大侠们救命, C++ operator new 问题问个fork cow的问题
奇怪的问题:关于一个简单的malloc()小程序 (转载)Use Visual .NET for C++ programming
相关话题的讨论汇总
话题: char话题: abcde话题: c++话题: eax话题: main