GVKun编程网logo

Valgrind给getline()给出错误(valgrind invalid read)

20

关于Valgrind给getline和给出错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于C++ifstream.getline()比Java的BufferedReader.readL

关于Valgrind给getline给出错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于C ++ ifstream.getline()比Java的BufferedReader.readLine()慢得多?、c – Valgrind在空的析构函数中给出“无效写入大小为8”、c – Valgrind报告说getaddrinfo正在泄漏内存?、c – valgrind给出了未初始化的错误大小等相关知识的信息别忘了在本站进行查找喔。

本文目录一览:

Valgrind给getline()给出错误(valgrind invalid read)

Valgrind给getline()给出错误(valgrind invalid read)

我有一个函数调用getline() ,它读取标准文本文件中的行。 这些行以rn结尾,因为这是规范所要求的,因为它是VCARD文件的“互联网标准”。

无论如何,我在OpenSUSE 11.3上通过Valgrind运行这个宝贝,并带有最新的GCC和Valgrind版本。 没有错误。 然而,由于与这个问题没有关系的原因,我把发行版切换到Back Track 4(基于Ubuntu),现在我得到了一吨Valgrind错误:

==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x40164A1: (within /lib/ld-2.11.1.so) ==21536== by 0x4007C04: (within /lib/ld-2.11.1.so) ==21536== by 0x4002A2C: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x40164AC: (within /lib/ld-2.11.1.so) ==21536== by 0x4007C04: (within /lib/ld-2.11.1.so) ==21536== by 0x4002A2C: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) --21536-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x55 --21536-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x55 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 --21536-- DWARF2 CFI reader: unhandled CFI instruction 0:10 ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x400B217: (within /lib/ld-2.11.1.so) ==21536== by 0x40031D0: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x400AF0F: (within /lib/ld-2.11.1.so) ==21536== by 0x40031D0: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x400B27A: (within /lib/ld-2.11.1.so) ==21536== by 0x40031D0: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x400A5DE: (within /lib/ld-2.11.1.so) ==21536== by 0x40030FE: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x400A5E6: (within /lib/ld-2.11.1.so) ==21536== by 0x40030FE: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x400AF0F: (within /lib/ld-2.11.1.so) ==21536== by 0x40030FE: (within /lib/ld-2.11.1.so) ==21536== by 0x4014206: (within /lib/ld-2.11.1.so) ==21536== by 0x4000C6C: (within /lib/ld-2.11.1.so) ==21536== by 0x4000856: (within /lib/ld-2.11.1.so) ==21536== ==21536== Invalid read of size 8 ==21536== at 0x40B4785: (within /lib/tls/i686/cmov/libc-2.11.1.so) ==21536== by 0x8049EFB: main (vcutil.c:496) ==21536== Address 0x419d1b0 is 8 bytes before a block of size 120 alloc'd ==21536== at 0x4025D2E: malloc (vg_replace_malloc.c:207) ==21536== by 0x409DF9D: getdelim (in /lib/tls/i686/cmov/libc-2.11.1.so) ==21536== by 0x408F3A2: getline (in /lib/tls/i686/cmov/libc-2.11.1.so) ==21536== by 0x804947C: getUnfolded (vcutil.c:299) ==21536== by 0x8049EFB: main (vcutil.c:496) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x40B47AD: (within /lib/tls/i686/cmov/libc-2.11.1.so) ==21536== by 0x8049EFB: main (vcutil.c:496) ==21536== ==21536== Conditional jump or move depends on uninitialised value(s) ==21536== at 0x8049524: getUnfolded (vcutil.c:307) ==21536== by 0x8049EFB: main (vcutil.c:496)

我特别不知道这些线是指什么

--21536-- Warning: DWARF2 CFI reader: unhandled DW_OP_ opcode 0x55

意思。 自从我改变发行版之后,这些是新的。

用于C ++的VisualWorks Smalltalk IDE(Linux)

在输出文件上做“rm”之后,C程序在哪里写输出?

Linux何时/如何将共享库加载到地址空间?

文件描述符在哪里存储在进程内存中?

如何获得文件的扩展属性(UNIX / C)?

无论如何,作为参考,这里是我的问题:

VcStatus getUnfolded (FILE *const vcf,char **const buff) { //int const buffSize = 30; size_t linesize = 0; int done = 0; //Set to one if the next line checked does not entail unfolding static char* queue = NULL; //A queue to store the next line read in static int lineNum = 0; char* buffer = NULL; //The dynamic memory that will be linked to buf int valid_line = 0; //Set to one if the line contains characters other then whitespace char* temp; //Used to point to the location of rn in the buffer VcStatus error; //Initialize VcStatus with appropriate values error.code = OK; error.linefrom = lineNum; error.lineto = 0; //Special case for resetting getUnfolded() if (vcf == NULL){ lineNum = 0; if (queue != NULL) queue = NULL; error.linefrom = 0; error.lineto = 0; return error; } //copies any string contained in queue from last time into buffer if (queue != NULL) { buffer = malloc (strlen (queue)+1); strcpy (buffer,queue); strcpy (queue,""); } //If the end of file has been reached,last line is returned. if (feof (vcf) != 0) { queue = NULL; lineNum += 1; if (buffer != NULL) { if (strcmp (buffer,"") != 0)*buff = buffer; } else *buff = NULL; return error; } while (done == 0) { //Gets a new line from the vcf file,and returns null if there is no more data to be returned getline (&queue,&linesize,vcf); if (ferror(vcf) != 0) { error.code = IOERR; return error; } //Sets valid_line to 1 if the line read in contains any characters other then whitespace for (int i = 0; i < strlen (queue); i++){ if (queue[i] != 'r' && queue[i] != 'n' && queue[i] != ' ' && queue[i] != 't') valid_line = 1; } if (feof (vcf) && valid_line == 0) { queue = NULL; break; } if (valid_line == 1){ //Finds the 'internet standard' newline and replaces it with a null terminator temp = strstr (queue,"rn"); if (temp != NULL) strcpy (temp,""); //Sets the buffer equal to contents of queue and resets queue if buffer is empty if (buffer == NULL) { buffer = malloc (strlen (queue)+1); lineNum = lineNum + 1; strcpy (buffer,""); //Checks for leading whitespace to indicate a folded line,and unfolds }else if (queue [0] == ' ' || queue [0] == 't') { lineNum = lineNum + 1; buffer = realloc (buffer,strlen (buffer)+strlen (queue)+2); strcat (buffer,queue+1); strcpy (queue,""); //If both conditions above are false,buffer contains the next valid line }else{ error.lineto = lineNum; lineNum += 1; done = 1; } } } *buff = buffer; return error; }

…以及将testing数据发送给它的虚拟函数:

int main () { FILE* fp = fopen ("test.vcf","r"); char * buff; getUnfolded (fp,&buff); }

test.vcf文件真的只是一个简单的文本文件

任何帮助将是惊人的。

如何决定在哪里存储每个用户的状态? 注册? 应用程序数据? 孤立的存储?

确保语句只有在按住某个键时才执行。

Linux堆分配

RegQueryValueEx REG_SZ

由于PROTOBUF服务器中的string数据typesvariables而导致段错误,并通过recv端的套接字来实现通信

您正在使用的valgrind版本已过时,因此不会抑制正确的输出。 这些错误仍然存​​在,只是因为它们在系统代码中而被压制。

您需要手动安装最新版本的valgrind

这是我使用的论坛帖子

http://ubuntuforums.org/showthread.PHP?t=1608001

C ++ ifstream.getline()比Java的BufferedReader.readLine()慢得多?

C ++ ifstream.getline()比Java的BufferedReader.readLine()慢得多?

我正在重写我的一个Android应用程序以利用NDK,并且它每次要做的第一件事之一是打开一个1.5MB的文本文件(大约15万行),并将每一行放入数据中结构体。当我使用Java的BufferedReader.readLine()进行此操作时,从SD卡读取文件大约需要2.5秒。这是我用于此的代码:

try {    BufferedReader br = new BufferedReader(new FileReader("/sdcard/testfile.txt"));    String thisLine;    while ((thisLine = br.readLine()) != null) {        Log.d(TAG, thisLine);    }} catch (IOException e) {    //Log error}

将C 与ifstream一起使用需要更长的时间…对于同一文件大约需要3分钟。这是我在C 中使用的代码:

char buffer[256];ifstream ifs;ifs.open("/sdcard/testfile.txt", ifstream::in);if (ifs.is_open()) {    while (!ifs.eof()) {        ifs.getline (buffer,100);        LOGD(buffer);    }}

我对C
相当生疏,但是对于增加的读取时间却无法想到任何合理的解释。有一阵子我以为可能是LOGD函数,但是我尝试将其全部删除,而读取时间并没有真正帮助。有人对这个问题可能有什么想法吗?有没有更快的方法在C
中逐行读取文件?谢谢。

答案1

小编典典

一种想法是stdio同步可能会拖慢您的速度。可以关闭。我不知道这是否可以解决所有差异,但是您可以尝试。另外,您使用不eof()正确。最后,我将使用std
:: string版本的getline()

std::ios::sync_with_stdio(false);ifstream ifs("/sdcard/testfile.txt");std::string line;while (getline(ifs, line)){    LOGD(line);}

我尚未测试此代码,但是您可以尝试一下,看看它是否有所不同。

c – Valgrind在空的析构函数中给出“无效写入大小为8”

c – Valgrind在空的析构函数中给出“无效写入大小为8”

我宣布了一个:

std::map<unsigned int,MyClass> *myMap;

到某个A类.这个地图是在A的构造函数中创建的:

myMap = new std::map<unsigned int,MyClass>;

MyClass类基本上是一个用一些getter / setter存储一些数据的结构. MyClass中没有任何指针或新实例,只有一对枚举值,一个unsigned int和一个bool.所以MyClass析构函数是空的.

另一方面,进入A的析构函数我正在删除地图:

A::~A(){
    if(myMap!=NULL){
        delete myMap;
        myMap = NULL;
    }
}

Valgrind在删除行上告诉我“地址0x4c389b0是一个大小为48的块中的16个字节[PID:6077]”.

此外,在MyClass析构函数中,即使是空的,我也得到“无效写入大小8 [PID:6077]”

我不明白问题是什么.我一直以为在向量或映射上调用delete会自动调用每个元素对应的析构函数,在这种情况下析构函数无关.

有帮助吗?

编辑:添加了A的构造函数:

A::A(unsigned int someValue){
    m_someValue = someValue;
    initializeMap();
}

void A::initializeMap(){
myMap = new std::map<unsigned int,MyClass>;
    for(unsigned int i=1; i<=20; i++)
        mymap->insert(std::make_pair(i,MyClass(i)));
}

另外,MyClass构造函数:

SvAvailabitlity::SvAvailabitlity(unsigned int index){
    m_index = index; //unsigned int
    m_Flag = false; //bool
        m_enumData1 = NOT_OK; //enum MyEnum
    m_enumData2 = NOT_OK; //enum MyEnum
}

其中MyEnum定义为:

typedef enum {
    OK = 0,NOT_OK = 1,} MyEnum;

顺便说一句,我不理解反对票.有人也可以请解释一下,以防万一有人用这个问题冒犯我认为根据stackoverflow规则是正确的.

解决方法

问题是三违规.您的对象的任何副本都将导致双重免费.

c – Valgrind报告说getaddrinfo正在泄漏内存?

c – Valgrind报告说getaddrinfo正在泄漏内存?

我一直在我们的项目上运行Valgrind,并且valgrind已经报告说,尽管在函数的底部调用了freeaddrinfo,但是从getaddrinfo的调用中已经丢失了内存.知道可能导致这种情况的原因吗?

int tcp_connect(char *address,char *port)
{
//printf("%s\n ",address);
    int status,sockfd;
    struct addrinfo hints,*servinfo,*p;

    memset(&hints,sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype= SOCK_STREAM;
    status = getaddrinfo(address,port,&hints,&servinfo);
    if(status != 0){
        printf("tcpconnect: getaddrinfo Failed\n");
    }
    for(p = servinfo; p != NULL; p = p->ai_next) {
        sockfd = socket(p->ai_family,p->ai_socktype,p->ai_protocol);
        if(sockfd == -1){
            printf("tcpconnect: socket Failed\n");
            continue;
        }

        status = connect(sockfd,p->ai_addr,p->ai_addrlen);
        if(status == -1){
            close(sockfd);
            printf("tcpconnect: connect Failed\n");
            continue;
        }
        break;
    }

    if (p == NULL){
        printf("tcpconnect: Could not get a valid socket file descriptor\n");
        return -1;
    }

    freeaddrinfo(servinfo);
    //return socket
    return sockfd;
}

valgrind输出在这里:

==7803== 384 bytes in 8 blocks are definitely lost in loss record 30 of 37
==7803==    at 0x4023CC8: malloc (vg_replace_malloc.c:236)
==7803==    by 0x413FE46: gaih_inet (in /lib/libc-2.7.so)
==7803==    by 0x4141CA1: getaddrinfo (in /lib/libc-2.7.so)
==7803==    by 0x804D20D: tcp_connect (tcpconnect.c:18)
==7803==    by 0x804D505: update_radio_data_t (radiodata.c:69)
==7803==    by 0x4073368: start_thread (in /lib/libpthread-2.7.so)
==7803==    by 0x4159CFD: clone (in /lib/libc-2.7.so)

我对此内存泄漏感到困惑,因为我确保在底部调用freeaddrinfo.这可能是Valgrind或freeaddrinfo中的错误吗?

解决方法

你的失败路径(如果(p == NULL))没有调用freeaddrinfo,我会在检查之前调用它.

c – valgrind给出了未初始化的错误大小

c – valgrind给出了未初始化的错误大小

我在博客中看到的sample2.c代码用valgrind来说明未初始化的值.这是代码:
sample2.c:

1  #include <stdio.h>
 2  
 3  int main()
 4  {
 5      int a[5];
 6      int i,s;
 7      a[0]=a[1]=a[3]=a[4]=0;
 8      
 9      printf("%d\n",a[2]);
10      for(i=0;i<5;i++)
11      s += a[i];
12      
13      if(s == 377)
14      printf("sum is %d\n",s);
15      return 0;
16  }

使用gcc -g -o sample2 sample2.c

valgrind –leak-check = full –track-originins = yes ./sample2

我在下面获得了valgrind检查信息.

我不明白为什么在我的机器中由于sizeof(int)== 4而使用大小为8的未初始化值?我在x64机器上运行程序,所以这是指针吗?

==31419== Use of uninitialised value of size 8
==31419==    at 0x4E7F1CB: _itoa_word (in /usr/lib64/libc-2.17.so)
==31419==    by 0x4E83450: vfprintf (in /usr/lib64/libc-2.17.so)
==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
==31419==    by 0x400561: main (sample2.c:9)
==31419==  Uninitialised value was created by a stack allocation
==31419==    at 0x40052D: main (sample2.c:4)

完整检查信息如下:

==31419== Memcheck,a memory error detector
    ==31419== copyright (C) 2002-2017,and GNU GPL'd,by Julian Seward et al.
    ==31419== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
    ==31419== Command: ./sample2
    ==31419== 
    ==31419== Conditional jump or move depends on uninitialised value(s)
    ==31419==    at 0x4E81AFE: vfprintf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x400561: main (sample2.c:9)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    ==31419== Use of uninitialised value of size 8
    ==31419==    at 0x4E7F1CB: _itoa_word (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E83450: vfprintf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x400561: main (sample2.c:9)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    ==31419== Conditional jump or move depends on uninitialised value(s)
    ==31419==    at 0x4E7F1D5: _itoa_word (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E83450: vfprintf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x400561: main (sample2.c:9)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    ==31419== Conditional jump or move depends on uninitialised value(s)
    ==31419==    at 0x4E8349F: vfprintf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x400561: main (sample2.c:9)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    ==31419== Conditional jump or move depends on uninitialised value(s)
    ==31419==    at 0x4E81BCB: vfprintf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x400561: main (sample2.c:9)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    ==31419== Conditional jump or move depends on uninitialised value(s)
    ==31419==    at 0x4E81C4E: vfprintf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x4E8A338: printf (in /usr/lib64/libc-2.17.so)
    ==31419==    by 0x400561: main (sample2.c:9)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    4195392
    ==31419== Conditional jump or move depends on uninitialised value(s)
    ==31419==    at 0x400588: main (sample2.c:13)
    ==31419==  Uninitialised value was created by a stack allocation
    ==31419==    at 0x40052D: main (sample2.c:4)
    ==31419== 
    ==31419== 
    ==31419== HEAP SUMMARY:
    ==31419==     in use at exit: 0 bytes in 0 blocks
    ==31419==   total heap usage: 0 allocs,0 frees,0 bytes allocated
    ==31419== 
    ==31419== All heap blocks were freed -- no leaks are possible
    ==31419== 
    ==31419== For counts of detected and suppressed errors,rerun with: -v

解决方法

你有一个64位处理器.即使值本身是32位,它也会在64位寄存器中传递给printf;这来自x86-64的System-V调用约定.

然而,它似乎没有触发错误,直到后来,valgrind似乎只知道该值被符号扩展为64位并且最初来自您的main函数(甚至行号在那里是错误的.

如果使用-m32编译它并以某种方式设法在valgrind下运行它(我需要在Ubuntu上安装其他32位软件包),你可以在这里看到4:

==4854== Use of uninitialised value of size 4
==4854==    at 0x48A3CAB: _itoa_word (_itoa.c:179)
==4854==    by 0x48A85C5: vfprintf (vfprintf.c:1642)
==4854==    by 0x48AF2F5: printf (printf.c:33)
==4854==    by 0x1085D0: main (sample2.c:9)
==4854== 
==4854== Conditional jump or move depends on uninitialised value(s)
==4854==    at 0x48A3CB3: _itoa_word (_itoa.c:179)
==4854==    by 0x48A85C5: vfprintf (vfprintf.c:1642)
==4854==    by 0x48AF2F5: printf (printf.c:33)
==4854==    by 0x1085D0: main (sample2.c:9)
==4854==

这里需要注意的一点是,int [2]中的值是不确定的,但是没有int类型的陷阱值,并且它的地址被取而代之,因此将它传递给函数时没有未定义的行为 – 但是在库函数中使用的确如此.

关于Valgrind给getline给出错误的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于C ++ ifstream.getline()比Java的BufferedReader.readLine()慢得多?、c – Valgrind在空的析构函数中给出“无效写入大小为8”、c – Valgrind报告说getaddrinfo正在泄漏内存?、c – valgrind给出了未初始化的错误大小等相关知识的信息别忘了在本站进行查找喔。

本文标签: