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:返回局部变量或临时地址:字符串
我建议你分配内存
char *string = malloc(10000);
完成后记住free
它。
推荐阅读
- python - 梯度下降中的 Inf 和 Nan
- android - Android Camera2 StateCallback onOpened 抛出随机错误
- browser - 将电报“tg”链接与浏览器扩展或 WEB-UI 关联
- c++ - 使用 std::format 将 time_point 转换为字符串,格式包括日期、时间和亚秒
- mongodb - 如何使用 multer 在 MongoDB 中上传多个图像?
- ethereum - 如何存储要在智能合约中使用的数据?
- magento2 - 前端显示的产品图片错误,而不是后端 magento 2.3 中上传的产品图片
- sql - 向 h2 列添加唯一的、不区分大小写的索引
- html - VBA Excel,Selenium - 循环遍历单选按钮值以查找所选值并打印它
- java - TestContainers SQL 连接数过多异常