看下面一段程序:
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void get_string( char * p );
- int main(int argc, char **argv )
- {
- unsigned int i;
- char *p=NULL;
- get_string( p );
- /* print the string pointed to by p one character at a time */
- for( i=0; i < strlen(p); i++ ) {
- printf( "p[%d] = '%c'/n", i, p[i] );
- }
- exit( 0 );
- }
- void get_string( char * p )
- {
- /* make p point to the string "foobar" */
- memcpy( p, "foobar", strlen( "foobar")+1 );
- printf( "p's value is %p; It points to the string: /"%s/"./n", p, p);
- }
要求:用GDB找出程序中的两处BUG。
直接编译后,运行出现“段错误”,即“Segmenation fault (coredumped))”。
用GDB单步运行:
- (gdb) l
- 1 #include <stdio.h>
- 2 #include <string.h>
- 3 #include <stdlib.h>
- 4
- 5 void get_string( char * p );
- 6
- 7 int main(int argc, char **argv )
- 8 {
- 9 unsigned int i;
- 10 char *p=NULL;
- (gdb) break 9
- Breakpoint 1 at 0x8048406: file aa.c, line 9.
- (gdb) r
- Starting program: /home/ysong/a.out
- Breakpoint 1, main () at aa.c:10
- 10 char *p=NULL;
- Missing separate debuginfos, use: debuginfo-install glibc.i686
- (gdb) info locals
- i = 3215191144
- p = 0x79bff4
- (gdb) n
- 12 get_string( p );
- (gdb) info locals
- i = 3215191144
- p = 0x0
- (gdb) n
- Program received signal SIGSEGV, Segmentation fault.
- 0x0804847f in get_string (p=0x0) at aa.c:25
- 25 memcpy( p, "foobar", strlen( "foobar")+1 );
- (gdb) backtrace
因为有char *p=NULL;
所以指针被初始化为指向地址0x0。这是违规访问内存地址,所以会出现段错误。
这里用malloc函数动态分配内存空间:
- void get_string( char * p );
- int main(int argc, char **argv )
- {
- unsigned int i;
- char *p=(char*)malloc(100*sizeof(char));
- get_string(p);
- /* print the string pointed to by p one character at a time */
- for( i=0; i < strlen(p); i++ ) {
- printf( "p[%d] = '%c'/n", i, p[i] );
- }
- free(p);
- exit( 0 );
- }
- void get_string( char * p )
- {
- /* make p point to the string "foobar" */
- memcpy( p, "foobar", strlen( "foobar")+1 );
- printf( "p's value is %p; It points to the string: /"%s/"./n", p, p);
- }
编译后的运行结果为:
p's value is 0x9cdd008; It points to the string: "foobar".
p[0] = 'f'
p[1] = 'o'
p[2] = 'o'
p[3] = 'b'
p[4] = 'a'
p[5] = 'r'
当然,这里p的值是动态随机的。
参考文章:
小结如下:
(1)先看例子:
#include <iostream.h>
void main()
{
char *p,*p1="hello first!";
while((*(p++) = *(p1++)) != 0);
cout<<p<<endl;
}错处:p定义时没有初始化,p是指向不定,是一个野指针。
p++可能会引用非法的内存块。
编译时不会出错,但运行时会造成程序崩溃。
(2)把上面的p初始化为NULL
#include <iostream.h>
void main()
{
char *p=NULL,*p1="hello first!";
while((*(p++) = *(p1++)) != 0);
cout<<p<<endl;
}//也错,NULL表示没有任何指向。p++没有任何意义,运行时会造成程序崩溃。这里就应该想到不能对NULL的指针进行直接操作。
(3)现在为p初始化为" ":
void main()
{
char *p=" ",*p1="hello first!";
while((*(p++) = *(p1++)) != 0);
cout<<p<<endl;
}//错:p指向的是一个const常量的内容,可以通过*(p++)形式引用该值,但是不能改变它指向const的内容。
(4)#include <iostream.h>
#include <string.h>
void main()
{
char c[]="";
char *p=c,*p1="hello first!";
char *p_start=p;
while((*(p++) = *(p1++)) != 0);
cout<<c<<endl;
}//此时数组是一系列的变量了,也就是p一有指向,二不是指向常量的而是指向变量的。所以按理应该行的。问题出在c太小,造成了数组越界,所以错掉!把c的大小改来不比"hellofirst!"小就行了。
(5)对于的就想到用new来初始化了:
#include <iostream.h>
#include <string.h>
void main()
{
char *p,*p1="hello first!";
p=new char;
char *p_start=p;
while((*(p++) = *(p1++)) != 0);
cout<<p_start<<endl;
}//现在就可以了,哈,不过,我认为在new时最好还是把它的大小给指出来,
如newchar[strlen(p1)+1];如果不指定大小,我想p++会指向一些已用得地址,而这些地址又不能随便改,就会造成诸如野指针样程序崩溃了。
小结:对于前面的问题,不防这样来写:
#include <iostream.h>
#include <string.h>
void main()
{
char *p,*p1="hello first!";
p=new char[strlen(p1)+1];
//p=new char;//觉得最好别这样子,newchar只相当于得到一个char对
char *p_start=p;//象,分配一个字符的空间。
while((*(p++) = *(p1++)) != 0);
cout<<p_start<<endl;
}
需要包含头文件:
#include 'stdlib.h'
函数声明(函数原型):
void *malloc(int size);
说明:malloc 向系统申请分配指定size个字节的内存空间。返回类型是 void* 类型。void* 表示未确定类型的指针。C,C++规定,void* 类型可以强制转换为任何其它类型的指针。
从函数声明上可以看出。malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小。比如:
int *p;
p = new int; //返回类型为int* 类型(整数型指针),分配大小为 sizeof(int);
或:
int* parr;
parr = new int [100]; //返回类型为 int* 类型(整数型指针),分配大小为 sizeof(int) *100;
而 malloc 则必须由我们计算要字节数,并且在返回后强行转换为实际类型的指针。
int* p;
p = (int *) malloc (sizeof(int));
第一、malloc 函数返回的是 void * 类型,如果你写成:p = malloc (sizeof(int)); 则程序无法通过编译,报错:“不能将 void* 赋值给 int * 类型变量”。所以必须通过 (int *) 来将强制转换。
第二、函数的实参为 sizeof(int) ,用于指明一个整型数据需要的大小。如果你写成:
int* p = (int *) malloc (1);
代码也能通过编译,但事实上只分配了1个字节大小的内存空间,当你往里头存入一个整数,就会有3个字节无家可归,而直接“住进邻居家”!造成的结果是后面的内存中原有数据内容全部被清空。
malloc 也可以达到 new [] 的效果,申请出一段连续的内存,方法无非是指定你所需要内存大小。
比如想分配100个int类型的空间:
int* p = (int *) malloc ( sizeof(int) * 100 ); //分配可以放得下100个整数的内存空间。
另外有一点不能直接看出的区别是,malloc 只管分配内存,并不能对所得的内存进行初始化,所以得到的一片新内存中,其值将是随机的。
除了分配及最后释放的方法不一样以外,通过malloc或new得到指针,在其它操作上保持一致。
free用法
需要包含头文件(和 malloc 一样):
#include 'stdlib.h'
函数声明:
void free(void *block);
即: void free(指针变量);
之所以把形参中的指针声明为 void* ,是因为free必须可以释放任意类型的指针,而任意类型的指针都可以转换为void *。
举例:
int* p = (int *) malloc(4);
*p = 100;
free(p); //释放 p 所指的内存空间
或者:
int* p = (int *) malloc ( sizeof(int) * 100 ); //分配可以放得下100个整数的内存空间。
……
free(p);
free 不管你的指针指向多大的空间,均可以正确地进行释放,这一点释放比 delete/delete []要方便。不过,必须注意,如果你在分配指针时,用的是new或new[ ],那么抱歉,当你在释放内存时,你并不能图方便而使用free来释放。反过来,你用malloc 分配的内存,也不能用delete/delete[] 来释放。一句话,new/delete、new[]/delete[]、malloc/free 三对均需配套使用,不可混用!
int* p = new int[100];
free(p); //ERROR! p 是由new 所得。