c - 为什么我在 Valgrind 中得到此代码的 "memory error"?

标签 c valgrind

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

所有输出均符合预期,但我在 Valgrind 中遇到内存错误。 老实说,我不能很好地理解 Valgrind 的输出,但它告诉我第 2 行有一个内存错误,当第 2 行是我的包含时,这看起来很奇怪。我还尝试对字符串使用 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)

最佳答案

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

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

这是由编译器警告揭示的

C4172: returning address of local variable or temporary: string

我建议你分配内存

char *string = malloc(10000);

并记得在使用完后释放它。

关于c - 为什么我在 Valgrind 中得到此代码的 "memory error"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54389467/

相关文章:

c - 为什么 Windows 要求导入 DLL 数据?

c - 我的串联函数 mystrcat(char*, char*, char*) 有什么问题?

c - 在 Swift 和 C 中格式化字符串会产生不同的结果

c - 使用 Valgrind 调试我的程序

opengl-es-2.0 - valgrind 这里的 "jump to invalid address"是什么意思?

c - 联盟和字节顺序

c - 为什么缺少终止“字符是一个警告?

C - 内存泄漏故障排除

c - Profile C代码执行百分比(逐行注释)

c++ - Callgrind 内联函数