
我有下面的程序:
int main(){
char* one = "computer"
char two[] = "another"
two[1]='b'
one[1]='b'
return 0
}
段错误发生在 "one[1]='b'"这一行上,这是有道理的,因为这个one指针指向的内存时只读内存。然而,问题是为什么 "two[1]='b'"这行没有发生段错误呢?查看gcc的汇编输出:
.file "one.c"
.section .rodata
.LC0:
.string "computer"
.LC1:
.string "another"
.text
.globl main
.type main, @function
main:
我们看到两个字符串都在 只读数据区域。那么为什么 “two[1]='b'”这行没有发生段错误呢?
one指针指向的stirng在只读页。另一方面,two是一个分配在栈上的数组,使用一些常量数据进行初始化。在运行期间,在只读区域的string会被拷贝一份到栈上。你修改的是string在栈上的拷贝,不是只读内存页。
更高层次的看法,从语言的视角看。“abcd”是一个const char*而不是 char*。因此,修改这样的指针值的结果是未定义的。这个语句char* one = "something",存储了一个指向字符串的指针(不安全的,它把const属性转换没了)。char two[] = "something"则是完全不同的。它实际上声明了一个数组并初始化了它,就像这样 int a[] = {1,2,3}。这里的引号里面的string是一个初始化表达式。
你看到的“another”在只读区域会被拷贝到数组 two中,在数组初始化的时候。另一方面,字符串“computer”的地址会被分配给one。
因此,one是指向只读区域(在这里写会发生段错误),two会分配在栈上然后拷贝一份“another”。
第二个形式会创建一个数组并拷贝这个字符串:
它等同于:
char two[] = {'a', 'n', 'o', 't', 'h'. 'e', r', '\0'}
你可以使用变量初始化字符数组,像这样:
char c = 'a'
char two[] = {'a', 'n', c, '\0'}
/*************************************文件名: server.c
linux 下socket网络编程简例 - 服务端程序
服务器端口设为 0x8888 (端口和地址可根据实际情况更改,或者使用参数传入)
服务器地址设为 192.168.1.104
作者:kikilizhm#163.com (将#换为@)
*/
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
int main()
{
int sfp,nfp/* 定义两个描述符 */
struct sockaddr_in s_add,c_add
int sin_size
unsigned short portnum=0x8888/* 服务端使用端口 */
printf("Hello,welcome to my server !\r\n")
sfp = socket(AF_INET, SOCK_STREAM, 0)
if(-1 == sfp)
{
printf("socket fail ! \r\n")
return -1
}
printf("socket ok !\r\n")
/* 填充服务器端口地址信息,以便下面使用此地址和端口监听 */
bzero(&s_add,sizeof(struct sockaddr_in))
s_add.sin_family=AF_INET
s_add.sin_addr.s_addr=htonl(INADDR_ANY)/* 这里地址使用全0,即所有 */
s_add.sin_port=htons(portnum)
/* 使用bind进行绑定端口 */
if(-1 == bind(sfp,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))
{
printf("bind fail !\r\n")
return -1
}
printf("bind ok !\r\n")
/* 开始监听相应的端口 */
if(-1 == listen(sfp,5))
{
printf("listen fail !\r\n")
return -1
}
printf("listen ok\r\n")
char gc
while(1)
{
sin_size = sizeof(struct sockaddr_in)
/* accept服务端使用函数,调用时即进入阻塞状态,等待用户进行连接,在没有客户端进行连接时,程序停止在此处,
不会看到后面的打印,当有客户端进行连接时,程序马上执行一次,然后再次循环到此处继续等待。
此处accept的第二个参数用于获取客户端的端口和地址信息。
*/
nfp = accept(sfp, (struct sockaddr *)(&c_add), &sin_size)
if(-1 == nfp)
{
printf("accept fail !\r\n")
return -1
}
printf("accept ok!\r\nServer start get connect from %#x : %#x\r\n",ntohl(c_add.sin_addr.s_addr),ntohs(c_add.sin_port))
/* 这里使用write向客户端发送信息,也可以尝试使用其他函数实现 */
if(-1 == write(nfp,"hello,welcome to my server \r\n",32))
{
printf("write fail!\r\n")
return -1
}
while((gc = getchar()) != EOF) /* 按ctrl + z 键结束,或者给定一个特殊字符,如'#',即 hile((gc = getchar()) != '#') */
{
write(nfp,gc,1)
}
printf("write ok!\r\n")
close(nfp)
}
close(sfp)
return 0
}
我这里是ubuntu x86-64,照着抄你的代码运行非常正常。编译运行:
as run.s -o run.o
ld run.o -o run
./run
GDB调试也没问题
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from run...done.
(gdb) b _start
Breakpoint 1 at 0x400078: file run.s, line 6.
(gdb) r
Starting program: /home/×××××××/Desktop/run
Breakpoint 1, _start () at run.s:6
6movl $1, %eax
(gdb) ni
7movl $0, %ebx
(gdb) ni
8int $0x80
(gdb) ni
[Inferior 1 (process 10617) exited normally]
(gdb) b _start
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)