找回密码
 注册
查看: 1530|回复: 10

赋值给常量的char *怎么释放?

[复制链接]
发表于 2005-7-12 09:08:01 | 显示全部楼层 |阅读模式

  1. #define AAA "aaa"
  2. char * temp = NULL;
  3. temp = (char *)malloc(sizeof(AAA));
  4. temp = AAA;
  5. ...;
  6. free(temp);
  7. temp = NULL;
复制代码


执行到free时报告segmentation fault
是不是因为free常量AAA的代码段空间啊?

怎么解决呢?
发表于 2005-7-12 09:14:40 | 显示全部楼层
不可以用: temp = AAA;
要用:strcpy(temp, AAA);
然后就应该可以free了。
回复

使用道具 举报

 楼主| 发表于 2005-7-12 09:41:47 | 显示全部楼层
啊,茅塞顿开

多谢!!!
回复

使用道具 举报

 楼主| 发表于 2005-7-12 09:49:57 | 显示全部楼层
可以free了

每次都是malloc、strcpy、使用、free、再malloc供下次用。这次打印define的AAA,下次打印定义的BBB、CCC、……

但是只是第一个strcpy打印正确,后边的strcpy会多出一些奇怪的字符
回复

使用道具 举报

发表于 2005-7-12 11:35:47 | 显示全部楼层
malloc时不要用sizeof(), 要用strlen()!
sizeof()得出的是变量的大小,不是字符串的长度
比如:
[code:1]
const char *strA = "Hello World!";
const char *strB = "Apple";
[/code:1]
sizeof(strA)和sizeof(strB)是一样大的!
而strlen(strA)是12,strlen(strB)是5。

另外给字符串malloc时一定要malloc“多一个”字符。
比如:
[code:1]
const char *source = "Hello";
char *temp = (char *)malloc(strlen(source) + 1);
[/code:1]
这样在strcpy时:
[code:1]
strcpy(temp, source);
[/code:1]
strcpy会自动在temp的最后面加上一个'\0', 这样打印时就不会多出一些奇怪的字符了。
回复

使用道具 举报

发表于 2005-7-12 11:45:36 | 显示全部楼层
如此,如此:
[code:1]
#define AAA "aaa"
#define BBB "bbbbb"
#define CCC "ccccccccc"

char *temp = NULL;
temp = (char *)malloc(strlen(AAA) + 1);
strcpy(temp, AAA);
printf("%s\n", temp);
free(temp);

temp = (char *)malloc(strlen(BBB) + 1);
strcpy(temp, BBB);
printf("%s\n", temp);
free(temp);

temp = (char *)malloc(strlen(CCC) + 1);
strcpy(temp, CCC);
printf("%s\n", temp);
free(temp);
[/code:1]
回复

使用道具 举报

发表于 2005-7-12 12:52:02 | 显示全部楼层

Re: 赋值给常量的char *怎么释放?

[quote:b7e96bce23="macarthor"][code:1]
#define AAA "aaa"
char * temp = NULL;
temp = (char *)malloc(sizeof(AAA));
temp = AAA;
...;
free(temp);
temp = NULL;
[/code:1]

执行到free时报告segmentation fault
是不是因为free常量AAA的代码段空间啊?

怎么解决呢?[/quote]

free(temp)其实就是free("aaa"), 能不segment fault吗?
回复

使用道具 举报

 楼主| 发表于 2005-7-12 13:56:45 | 显示全部楼层
非常感谢!!!

再请教一下:

1、define的字符串是不是写成const char *比较好啊?

2、free之后还要置为NULL吗?
回复

使用道具 举报

发表于 2005-7-12 14:11:55 | 显示全部楼层
1. 没有多大区别
2. free之后置为NULL是个好习惯^^
回复

使用道具 举报

发表于 2005-7-14 09:23:16 | 显示全部楼层
看了以上的对话真是受益匪浅,我也对 malloc 和 free 不是太清楚怎么使用,这下清楚多了,谢谢 玛宁、macarthor、dongni386 。

既然所到malloc 和 free ,我就像问一问 new 和 delete 的用法。


void Play::GetPoker(char *client_name,DBlist & dblist)   
{

        Poker  * poker;
        char *p_name =NULL;

//poker初始化       
        do{
                poker=new Poker("AA");
               
        }while(poker ==NULL);
       
//为p_name申请空间       
        do{
                p_name=(char *)malloc(sizeof(poker->GetPoker()));  //poker->GetPoker() 传进来一个名字
                       
        }while(p_name == NULL);       
       
        strcpy(p_name,poker->GetPoker());        //得到新牌
               
          .
          .
          .
                
         free(p_name);       
         char *p_name =NULL;
}


1. 请看一下,我的malloc 和 free 用的对不对 ?
2. poker=new Poker("AA");  这里的poker 什么时候该 delete 掉,怎样 delete 掉?
3. p_name=(char *)malloc(sizeof(poker->GetPoker()));  这么用是不是不太好,可程序没有出错, 是不是要给成这样?
      p_name=(char *)malloc(strlen(poker->GetPoker())+1);
回复

使用道具 举报

发表于 2005-7-14 13:28:27 | 显示全部楼层
总体来讲malloc/free和new/delete的用法是一样的(malloc/free对应C,new/delete对应C++)。
比如:
[code:1]
const char *theStr = "Hello World!";
// 再C下面
char *tempStr1 = malloc(strlen(theStr) + 1);
    /* 等等, 等等 */
free(tempStr1);
tempStr1 = NULL;

// 再C++下面
char *tempStr2 = new char*[strlen(theStr) + 1];
    /* 等等, 等等 */
delete tempStr2;
tempStr2 = NULL;
[/code:1]

至于楼上的问题 ...
1.  请看一下,我的malloc 和 free 用的对不对 ?
这句可能有问题:
[code:1]
p_name=(char *)malloc(sizeof(poker->GetPoker()));
[/code:1]
因为转换为(char *)的话,除非有很好的理由,一般都是用strlen,而不是sizeof,原因看更上楼的贴。
还有,转换为(char *)的话,malloc的大小一般都要"+1",因为再字符串的最后还有一个‘\0’。

这句绝对有问题:
[code:1]
free(p_name);
char *p_name =NULL;  <<------ 问题在这里!!
[/code:1]
"p_name =NULL"就好,不用重新declear新的p_name!!!

2. poker=new Poker("AA"); 这里的poker 什么时候该 delete 掉,怎样 delete 掉?
一般是在function的最后删除(或则在你认为必要的时候)。
怎样 delete 掉:
[code:1]
delete poker;
poker = NULL;
[/code:1]

3. p_name=(char *)malloc(sizeof(poker->GetPoker())); 这么用是不是不太好,可程序没有出错, 是不是要给成这样?
p_name=(char *)malloc(strlen(poker->GetPoker())+1);

然也(当然你还得确定poker->GetPoker() return字符串才行)。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2025-2-8 05:51 , Processed in 0.047380 second(s), 16 queries .

© 2001-2025 Discuz! Team. Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表