Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串内存分配中可能存在内存泄漏_C++_String_Memory Leaks_Stl - Fatal编程技术网

C++ 字符串内存分配中可能存在内存泄漏

C++ 字符串内存分配中可能存在内存泄漏,c++,string,memory-leaks,stl,C++,String,Memory Leaks,Stl,这是我的代码: #include <string> #include <iostream> #include <cstdio> #include <cstdlib> std::string & fileread(const char * name) { FILE *fp = fopen(name,"rb"); size_t sz; int i; char *buff; fseek(fp, 0, S

这是我的代码:

#include <string>
#include <iostream>

#include <cstdio>
#include <cstdlib>

std::string & fileread(const char * name)
{
    FILE *fp = fopen(name,"rb");
    size_t sz;
    int i;
    char *buff;
    fseek(fp, 0, SEEK_END);  
    sz = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    buff = (char *)malloc(sizeof(char)*(sz+1));
    buff[sz] = '\0';
    fread(buff,sz,1,fp); 
    std::string * rtstr = new std::string(buff);
    free(buff);
    fclose(fp);
    return * rtstr;
}

int main(int argc,char * argv[])
{
    std::string file_info(fileread(argv[1]));
    std::cout<<file_info << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
标准::字符串和文件读取(常量字符*名称)
{
文件*fp=fopen(名称,“rb”);
大小(四);;
int i;
字符*浅黄色;
fseek(fp,0,SEEK_END);
sz=ftell(fp);
fseek(fp,0,SEEK_集);
buff=(char*)malloc(sizeof(char)*(sz+1));
buff[sz]='\0';
fread(buff,sz,1,fp);
std::string*rtstr=新的std::string(buff);
免费(buff);
fclose(fp);
返回*rtstr;
}
int main(int argc,char*argv[])
{
std::string file_info(fileread(argv[1]);

STD::纳瓦兹指出:“不要在C++中对C进行编码。使用STD::IFString和STD::String(不是STD::String *)。这里是C++中的代码,以避免所有问题:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main () {

  string line;
  ifstream myfile ("myfile.txt");

  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
弦线;
ifstream myfile(“myfile.txt”);
如果(myfile.is_open())
{
while(getline(myfile,line))
{

CUT< P>返回STD::按值串。不用担心,C++将负责不冗余地复制对象(除非你有一个非常老的编译器)。 以下是代码,已修复:

#include <string>
#include <iostream>

#include <cstdio>
#include <cstdlib>

std::string fileread(const char * name)
{
    FILE *fp = fopen(name,"rb");
    size_t sz;
    int i;
    char *buff;
    fseek(fp, 0, SEEK_END);  
    sz = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    buff = (char *)malloc(sizeof(char)*(sz+1));
    buff[sz] = '\0';
    fread(buff,sz,1,fp); 
    std::string rtstr(buff);
    free(buff);
    fclose(fp);
    return * rtstr;
}

int main(int argc,char * argv[])
{
    std::string file_info(fileread(argv[1]));
    std::cout<<file_info << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
字符串文件读取(常量字符*名称)
{
文件*fp=fopen(名称,“rb”);
大小(四);;
int i;
字符*浅黄色;
fseek(fp,0,SEEK_END);
sz=ftell(fp);
fseek(fp,0,SEEK_集);
buff=(char*)malloc(sizeof(char)*(sz+1));
buff[sz]='\0';
fread(buff,sz,1,fp);
std::字符串rtstr(buff);
免费(buff);
fclose(fp);
返回*rtstr;
}
int main(int argc,char*argv[])
{
std::string file_info(fileread(argv[1]);

STD::C++中的CutDoE代码C。使用<代码> STD::IFStry和 STD::String (不<代码> STD::Stry*<代码>)。<代码> Frad()/Case>与 STD::IFStr::Read()一样快所以,不要认为C在性能上是胜利者。C++同样好,在某些情况下,比C.@纳瓦兹更好的性能,请分享一些“比C更好的性能”的好链接。@ SaulsAsEngC++类型系统鼓励静态类型检查和值语义,这两种方法都会导致更好的优化。考虑<代码> STD::/SCOV> VS <代码> QSORT()/Cug >:例如C是基于函数指针(CPU管道的最大敌人之一),C++是基于模板和函子的。(AcaAccess内嵌所有).@ SaulsAlEn:比较任何使用代码的指针(通常是代码> Vo> */Cube和函数指针),使用通用的C++代码,使用模板。C++最有可能比C快。一个这样的例子是:代码> STD::排序< /COD>(来自C++标准库)vs VS> QQueS/<代码>(来自C标准库)。请注意,您不必使用
myfile.close()
。此外,
if(myfile)
if(myfile.is\u open())更好
@Nawaz我用它来正确地关闭它。这样做不是一种好的做法吗?当流对象超出范围时,它将关闭文件。它是RAII对象。所以不用担心。@SahilSareen:关于这个确切的问题,请参阅。
std::getline
如果他打算读取二进制数据,可能不适合[yu@argcvex]$g++test.cc--std=c++11 test.cc:在函数“std::string fileread(const char*)”中:test.cc:22:12:错误:与“operator*”不匹配(操作数类型为“std::string{aka std::basic_string}”)return*rtstr;^
如果只返回rtstr,它将返回一个局部值。@YuJing:它返回一个副本,而不是对局部变量的引用。@Nawaz,而且,我可能会从文件中读取1GB或更多的
二进制
数据,请您给出更多更好的建议好吗?行
return*rtstr;
需要替换为
return rtstr;
任何现代编译器都将执行返回值优化,因此不会进行复制。