当前位置:首页
开发技术指南» 文章正文
    引言:
 

 

    摘要: 我建了一个网站,其中在运行过程中,由于多个导出excel的程序,在导出excel中产生一大堆的 临时文件,那些文件存在一个文件夹(excel)中, 请问要怎么处理这些临时文件,能告诉我具体要怎么做吗? ......
 ·找出所有内存泄露,少许。    »显示摘要«
    摘要: 一个程序n万行,找出所有内存泄露并解决之,散分少许。以做日期标记。 ......


这两个函数错在哪啊搞了两天了.痛痛痛.我想第一个copyfrom是没有错的.但是用第二个就错.

char   *copyfrom(char   *source,int   starts,int   len)  
  {  
  int   i;  
  char   *temp=new   char[];  
  memset(temp,0,1);  
   
  for   (i=starts;i<=len-1;i++)  
  {  
  *(temp+i-starts)=*(source+i);  
  }  
  //MessageBoxA(0,temp,"copyfrom",MB_OK);  
  return   temp;  
   
  }  
   
  char   *stringreplace(char   *source,char   *oldsub,char   *newsub)  
  {  
  char   *temp   =new   char[];  
  memset(temp,0,1);  
  strcpy(temp,source);  
   
  char   *temp1=new   char[];  
  memset(temp1,0,1);  
   
  char   *temp2;  
   
  if   (strstr(temp,oldsub)==0x00)  
  return   temp;  
   
          while   (   (temp2=strstr(temp,oldsub))!=0x00)  
  {  
  temp1=copyfrom(temp,0,temp2-temp+1);  
  strcat(temp1,newsub);  
  strcpy(temp,temp2+strlen(oldsub));  
  }  
  strcat(temp1,temp);  
  return   temp1;  
  }  
  还有,老是看见那些帖子说要释放,一个new一个delete,吓得我怕怕.delphi下面管理得很好呀.为什么C++这麽麻烦.是不是真的要手动释放.

NO.1   作者: nonocast

对啊,你new出来的东西没有释放!  
  在程序的结尾加上  
  delete[]   temp;  
  应该就可以了吧  
  因为new出来的东西不会自动释放,除非这个进程死掉。  
  不过看上去好像没什么,但是一旦不释放,就必定会造成内存泄漏。  
  new方便是方便,可一定要当心啊,也就是说要一个new一个delete。  
  May   you   succeed!  
 

NO.2   作者: AthlonxpX86

mark

NO.3   作者: sunyard

你还是用delphi的观念来编程,程序错误很多阿  
   
  char   *copyfrom(char   *source,int   starts,int   len)  
  {  
                    if   (!len)  
                    {  
                          return   NULL;  
                    }  
  int   i;  
  char   *temp=new   char[len+1];//需要指定一个长度  
  memset(temp,0,1);                     //不需要  
   
  for   (i=starts;i<=len-1;i++)  
  {  
  *(temp+i-starts)=*(source+i);  
  }  
                    temp[len]   =   \0;                   //加上  
  //MessageBoxA(0,temp,"copyfrom",MB_OK);  
  return   temp;  
   
  }  
   
  char   *stringreplace(char   *source,char   *oldsub,char   *newsub)  
  {  
  char   *temp   =new   char[strlen(source)   +   1];//修改  
  //memset(temp,0,1);                       //不需要  
  strcpy(temp,source);  
   
  char   *temp1=new   char[1000];//长度不好确定,因为替换后的长度未知,最好是能够用CString  
  memset(temp1,0,1000);  
   
  char   *temp2,*temp3;  
   
  if   (strstr(temp,oldsub)==0x00)  
  return   temp;  
   
          while   (   (temp2=strstr(temp,oldsub))!=0x00)  
  {  
  temp3   =copyfrom(temp,0,temp2-temp);//  
                                      strcat(temp1,temp3);         //  
  strcat(temp1,newsub);       //  
  delete[]   temp3;             //  
  strcpy(temp,temp2+strlen(oldsub));  
  }  
  strcat(temp1,temp);  
  delete[]   temp;                                   //  
  return   temp1;  
  }

NO.4   作者: JennyVenus

老大,我服了你了,深夜不休息研究学问,向你学习。  
   
  new   对应   delete,但是不需要这里new,就在这里delete,可以到别处去delete,你new没有指定长度。

NO.5   作者: loopyifly

char   *copyfrom(char   *strDest,   const   char   *source,int   starts,int   len)  
  {  
                    assert(   strDest!=NULL   &&   source!=NULL);  
  int   i;  
  char   *temp;  
  temp   =   strDest;  
   
  for   (i=0;i<=len-1;i++)  
  {  
  (*temp++)   =   (*source+starts)++;  
  }  
  //MessageBoxA(0,temp,"copyfrom",MB_OK);  
  return   temp;  
   
  }  
   
  另一个也这样改,要把目的串也传进去  
 


    摘要: 代码: //------------------------------------------------------------- // concatenate.cpp : defines the entry point for the console application. // #include "stdafx.h" char concatenate(char......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE