为什么我在Valgrind中收到此代码的“内存错误”?

时间:2019-01-27 14:56:29

标签: c valgrind

我正在编写一个代码,在该代码中您应该替换句子中最长和最短的单词,而不必实际使字符串变大/变小。 我以为可以创建一个新字符串,然后将原始字符串中的文本放到指针指向最大单词的指针(从函数nadji_max返回)中,然后从最大指针写入该单词,然后继续从原始字符串,直到到达最短单词等的指针为止

所有输出均符合预期,但是我在Valgrind中遇到内存错误。 老实说,我不能完全理解Valgrind的输出,但是它告诉我第2行存在内存错误,当第2行是我的include时,这似乎很奇怪。我还尝试使用malloc作为字符串,并将其设置为“ strlen(s)* sizeof(char)”,然后释放它,但这会导致内存泄漏而不是内存错误。关于什么原因导致我的内存错误以及如何解决它的任何澄清?谢谢

我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

char *nadji_max (char *s, int *maxvel)
{
int velicina = 0;
int max = 0;
char * maxpointer = NULL;
while (*s != '\0') {
    velicina = 0;
    while ((*s == '.' || *s == ',' || *s == '!' || *s == '?' || *s == ';' || *s == ' ') && *s != '\0') s++;
    char *temp = s;

    if (*s == '\0') break;
    while (*s != '.' && *s != ',' && *s  != '!' && *s != '?' && *s != ';' && *s != ' ') {
        if (*s == '\0') break;
        velicina++;
        s++;
    }

    if (velicina > max) {
        max = velicina;
        *maxvel = velicina;
        maxpointer = temp;
    }
}
return maxpointer;
}
char *nadji_min (char *s, int *minvel)
{
int velicina = 0;
int min = INT_MAX;
char * minpointer = NULL;
while (*s != '\0') {
    velicina = 0;
    while ((*s == '.' || *s == ',' || *s == '!' || *s == '?' || *s == ';' || *s == ' ') && *s != '\0') s++;
    char *temp = s;

    if (*s == '\0') break;
    while (*s != '.' && *s != ',' && *s  != '!' && *s != '?' && *s != ';' && *s != ' ') {
        if (*s == '\0') break;
        velicina++;
        s++;
    }

    if (velicina < min) {
        min = velicina;
        *minvel = velicina;
        minpointer = temp;
    }
}
return minpointer;
}
char *zamijeni_min_max (char *s)
{
char *pocetak = s;
int maxvel = 0, minvel = 0;
char *max = nadji_max(s, &maxvel);
char *min = nadji_min(s, &minvel);
char *pokmax = max;
char *pokmin = min;
//char *string = (char *) malloc(strlen(s) * sizeof(char));
char string [10000];
//strcpy(string, s);
char *pokstring = string;
char *pocstring = string;
while (*s != '\0') {
    while ((*s == '.' || *s == ',' || *s == '!' || *s == '?' || *s == ';' || *s == ' ') && *s != '\0') {
        *pokstring = *s;
        s++;
        pokstring++;
    }
    if (*s == '\0') break;

    if (s == max) {
        while (*pokmin != '.' && *pokmin != ',' && *pokmin  != '!' && *pokmin != '?' && *pokmin != ';' && *pokmin != ' ' && *pokmin != '\0') {
            *pokstring = *pokmin;
            pokstring++;
            pokmin++;
        }
        while (*s != '.' && *s != ',' && *s  != '!' && *s != '?' && *s != ';' && *s != ' ' && *s != '\0') s++;
        if (*s == '\0') {
            *pokstring = '\0';
            break;
        }
    } else if (s == min) {
        while (*pokmax != '.' && *pokmax != ',' && *pokmax  != '!' && *pokmax != '?' && *pokmax != ';' && *pokmax != ' ' && *pokmax != '\0') {
            *pokstring = *pokmax;
            pokstring++;
            pokmax++;
        }
        while (*s != '.' && *s != ',' && *s  != '!' && *s != '?' && *s != ';' && *s != ' ' && *s != '\0') s++;
        if (*s == '\0') {
            *pokstring = '\0';
            break;
        }

    } else {
        if (*s == '\0') break;
        *pokstring = *s;
        pokstring++;
        s++;
    }

}
return pocstring;
}
int main()
{
char recenica[] = "Ovo je primjer recenice sa dugackim, kratkim rijecima.";
printf ("'%s'", zamijeni_min_max(recenica));

}

Valgrind输出:

==31760== Memcheck, a memory error detector
==31760== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==31760== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==31760== Command: bs_test_1
==31760== Parent PID: 31759
==31760== 
==31760== Invalid read of size 1
==31760== at 0x3631247D0C: vfprintf (in /lib64/libc-2.12.so)
==31760== by 0x363124F069: printf (in /lib64/libc-2.12.so)
==31760== by 0x400C44: main (bs_test_1.c:124)
==31760== Address 0xffeffe4d0 is on thread 1's stack
==31760== 8112 bytes below stack pointer
==31760== 
==31760== Invalid read of size 1
==31760== at 0x4A0CDB0: mempcpy (vg_replace_strmem.c:1517)
==31760== by 0x36312717DE: _IO_file_xsputn@@GLIBC_2.2.5 (in /lib64/libc-2.12.so)
==31760== by 0x363124806F: vfprintf (in /lib64/libc-2.12.so)
==31760== by 0x363124F069: printf (in /lib64/libc-2.12.so)
==31760== by 0x400C44: main (bs_test_1.c:124)
==31760== Address 0xffeffe4d0 is on thread 1's stack
==31760== 8032 bytes below stack pointer
==31760== 
==31760== Invalid read of size 1
==31760== at 0x4A0CDBE: mempcpy (vg_replace_strmem.c:1517)
==31760== by 0x36312717DE: _IO_file_xsputn@@GLIBC_2.2.5 (in /lib64/libc-2.12.so)
==31760== by 0x363124806F: vfprintf (in /lib64/libc-2.12.so)
==31760== by 0x363124F069: printf (in /lib64/libc-2.12.so)
==31760== by 0x400C44: main (bs_test_1.c:124)
==31760== Address 0xffeffe4d2 is on thread 1's stack
==31760== 8030 bytes below stack pointer
==31760== 
==31760== 
==31760== HEAP SUMMARY:
==31760== in use at exit: 0 bytes in 0 blocks
==31760== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==31760== 
==31760== All heap blocks were freed -- no leaks are possible
==31760== 
==31760== For counts of detected and suppressed errors, rerun with: -v
==31760== ERROR SUMMARY: 109 errors from 3 contexts (suppressed: 4 from 4)

1 个答案:

答案 0 :(得分:0)

在函数zamijeni_min_max()中,您将返回本地定义的变量的地址,该变量在函数返回后将不复存在。

char string [10000];
//...
char *pocstring = string;
//...
return pocstring;

这是编译器警告所揭示的

  

C4172:本地变量的返回地址或临时变量:字符串

我建议您使用以下方式分配内存

char *string = malloc(10000);

并记得完成后free