const char *内存释放问题

WYLLO535825003 2011-10-15 04:23:55
今天我const char *first=new char(30),如何释放这些分配的内存
...全文
621 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
caddor2011 2011-10-16
  • 打赏
  • 举报
回复
看5楼


vc 6 无法通过


vs是可以通过的




[Quote=引用 9 楼 wyllo535825003 的回复:]
pid_t pid;
pid=fork();
//const char *first_web=new char(30); //为子进程分配内存块
//first_web=_web[0].c_str();
char *first_web=new char(30);
first_web=const_cast<char *>(_web[0].c_str());
if(pid<0)
{
c……
[/Quote]
WYLLO535825003 2011-10-15
  • 打赏
  • 举报
回复
pid_t pid;
pid=fork();
//const char *first_web=new char(30); //为子进程分配内存块
//first_web=_web[0].c_str();
char *first_web=new char(30);
first_web=const_cast<char *>(_web[0].c_str());
if(pid<0)
{
cout<<"线程1无法启动子进程"<<endl;
}
if(pid==0)
{
if(execlp("./ping_thread1","./ping_thread1",first_web,_sletime,NULL)==-1)
{
cout<<"子进程1启动失败"<<endl;
}
}
if(pid>0)
{
delete []first_web;
_id.push_back(pid);
}
源代码大家看下,就是内存回收不了
星羽 2011-10-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 quwei197874 的回复:]

const char* p=new char(65);

去掉const,加上表示p指的内容是固定的,你是不能delete的,或用const_cast<char*>转换一下
[/Quote]

谁说const char* 不能delete的 - -b
quwei197874 2011-10-15
  • 打赏
  • 举报
回复
const char* p=new char(65);

去掉const,加上表示p指的内容是固定的,你是不能delete的,或用const_cast<char*>转换一下
WYLLO535825003 2011-10-15
  • 打赏
  • 举报
回复
还是不行啊
caddor2011 2011-10-15
  • 打赏
  • 举报
回复
const char* p=new char(65);

cout<<*p<<endl;

delete p;

p=NULL;

p=new const char(66);

cout<<*p<<endl;

delete p;

p=NULL;
Vincent_Song 2011-10-15
  • 打赏
  • 举报
回复
用delete如果是多个内存单元需要使用[]前置符。

希望能够帮到你!
b_duan 2011-10-15
  • 打赏
  • 举报
回复
new和delete对应
星羽 2011-10-15
  • 打赏
  • 举报
回复
delete first;

你这个new 有问题吧,你是想new 字符串吗
那么应该是
new char[30]
而不是new char(30)
MYMGrub 2011-10-15
  • 打赏
  • 举报
回复
直接delete[] first;不可以吗??

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧