Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3977272
  • 博文数量: 536
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4825
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(536)

文章存档

2024年(3)

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(252)

2006年(73)

分类: C/C++

2011-04-24 09:39:47

[oracle@asm test]$ cat b.c
#include

int main(int argc, char **argv)
{
  char  *s = "abc";
  char  *s1;

  s1    = s;
  s[0]  = 'W';
 
  printf("s = %s, s1=%s\n", s, s1);

  return (0);
}
[oracle@asm test]$ gcc -Wall -o b b.c
[oracle@asm test]$ ./b
Segmentation fault

#因为"abc" 静态存储区,是常量区,不可改, s指针在栈区,但是指向的内容是静态存储区的内容,s[0]企图通过s这个指针改变常量区的内容,所以不对。

[oracle@asm test]$ vi b.c
[oracle@asm test]$ cat b.c
#include

int main(int argc, char **argv)
{
  char  s[] = "abc";
  char  *s1;

  s1    = s;
  s[0]  = 'W';
 
  printf("s = %s, s1=%s\n", s, s1);

  return (0);
}
[oracle@asm test]$ gcc -Wall -o b b.c
[oracle@asm test]$ ./b
s = Wbc, s1=Wbc

#"abc" 在静态存储区,s[]在栈区, 赋值给s[]这个变量后,改变s[0],是改变栈区的内容,所以不出错。


[oracle@asm test]$ uname -a
Linux asm 2.6.9-22.EL #1 Mon Sep 19 18:20:28 EDT 2005 i686 i686 i386 GNU/Linux
[oracle@asm test]$ gcc -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.4/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux
Thread model: posix
gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)
阅读(795) | 评论(0) | 转发(0) |
0

上一篇:Oracle9iR2 NF:压缩表技术

下一篇:堆与栈

给主人留下些什么吧!~~