gpt4 book ai didi

C - 试图使字符串小写,不断出现段错误,valgrind 说地址映射内存的权限错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:37:35 24 4
gpt4 key购买 nike

这个功能给我带来了很多麻烦,我认为这会比结果更容易。

lowerString 应该接受一些 char * 使字符串中的所有字母小写并返回它。

我已经阅读了尽可能多的帖子,并使用了解决方案中的代码来编写我的代码,但我无法让它工作。我检查了搜索引擎结果中 valgrind 问题的各种答案,但我无法弄清楚出了什么问题。

我认为这可能是一些字符串文字问题,但我只使用 char*

来自头文件 functions.h

char * lowerString(char *str);

来自 functions.c:

/* Makes everything in the string lowercase */
char * lowerString(char *str){
char *p;
p = str;
for( ; *p; ++p){
*p = tolower(atoi(p));
}
return p;
}

在我从 b.c 运行的代码中:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"

int main(void){
char *a, *b, *c, *p;

a = "The cat in le Hat";
b = "Meow meow meow meow";
c = "This an$example12 mail@rutgers";

p = lowerString(a);
printf("%s\n",p);
p = lowerString(b);
printf("%s\n",p);
p = lowerString(c);
printf("%s\n",p);

return 0;
}

我编译: gcc -Wall -Werror -pedantic -std=c99 -g -o b b.c functions.c

标志是不可协商的=/

Valgrind 运行时使用 valgrind --leak-check=full --show-leak-kinds=all -v ./b:

--6277-- REDIR: 0x4019ca0 (strlen) redirected to 0x38068331 (???)
--6277-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so
--6277-- Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
--6277-- .. CRC mismatch (computed 329d6860 wanted c0186920)
--6277-- object doesn't have a symbol table
--6277-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64- linux.so
--6277-- Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
--6277-- .. CRC mismatch (computed 1fb85af8 wanted 2e9e3c16)
--6277-- object doesn't have a symbol table
==6277== WARNING: new redirection conflicts with existing -- ignoring it
--6277-- old: 0x04019ca0 (strlen ) R-> (0000.0) 0x38068331 ???
--6277-- new: 0x04019ca0 (strlen ) R-> (2007.0) 0x04c2e1a0 strlen
--6277-- REDIR: 0x4019a50 (index) redirected to 0x4c2dd50 (index)
--6277-- REDIR: 0x4019c70 (strcmp) redirected to 0x4c2f2f0 (strcmp)
--6277-- REDIR: 0x401a9c0 (mempcpy) redirected to 0x4c31da0 (mempcpy)
--6277-- Reading syms from /lib/x86_64-linux-gnu/libc-2.19.so
--6277-- Considering /lib/x86_64-linux-gnu/libc-2.19.so ..
--6277-- .. CRC mismatch (computed dc620abc wanted 148cbd6e)
--6277-- Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so ..
--6277-- .. CRC is valid
--6277-- REDIR: 0x4ec3d60 (strcasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--6277-- REDIR: 0x4ec6050 (strncasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--6277-- REDIR: 0x4ec3530 (memcpy@GLIBC_2.2.5) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--6277-- REDIR: 0x4ec17c0 (rindex) redirected to 0x4c2da30 (rindex)
==6277==
==6277== Process terminating with default action of signal 11 (SIGSEGV)
==6277== Bad permissions for mapped region at address 0x401C68
==6277== at 0x401A02: lowerString (functions.c:303)
==6277== by 0x400D08: main (b.c:13)
--6277-- REDIR: 0x4eb9df0 (free) redirected to 0x4c2bd80 (free)
==6277==
==6277== HEAP SUMMARY:
==6277== in use at exit: 0 bytes in 0 blocks
==6277== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6277==
==6277== All heap blocks were freed -- no leaks are possible
==6277==
==6277== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==6277== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

最佳答案

abc 都指向存储在潜在只读内存中某处的字符串文字。 您不得修改它们。使用 strdup*()(或您平台上的等效项)为它们创建一个新的 char[]居住在。

a = strdup("The cat in le Hat");

...

free(a);

关于C - 试图使字符串小写,不断出现段错误,valgrind 说地址映射内存的权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com