- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C 和内存管理的新手,并尝试使用 valgrind 查找内存泄漏,我在 lexer->src_size
中发现了所有问题我不知道为什么当我使用 strlen()
瓦尔格林德:
==49058== Memcheck, a memory error detector
==49058== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==49058== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==49058== Command: ./loop -s
==49058==
==49058== Invalid read of size 1
==49058== at 0x483FF54: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x1093D1: Lexer_Init (lexer.c:11)
==49058== by 0x109A44: Loop_Compile (loop.c:6)
==49058== by 0x109AD4: main (in /home/ali/Desktop/loop/loop)
==49058== Address 0x4a502be is 0 bytes after a block of size 30 alloc'd
==49058== at 0x483C7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x109366: IO_ReadFile (io.c:17)
==49058== by 0x109ACC: main (in /home/ali/Desktop/loop/loop)
==49058==
==49058== Conditional jump or move depends on uninitialised value(s)
==49058== at 0x483FC57: strcat (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x10955A: Lexer_LexID (lexer.c:54)
==49058== by 0x109691: Lexer_NextToken (lexer.c:82)
==49058== by 0x109A80: Loop_Compile (loop.c:8)
==49058== by 0x109AD4: main (in /home/ali/Desktop/loop/loop)
==49058== Uninitialised value was created by a heap allocation
==49058== at 0x483C7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x1093B6: Lexer_Init (lexer.c:9)
==49058== by 0x109A44: Loop_Compile (loop.c:6)
==49058== by 0x109AD4: main (in /home/ali/Desktop/loop/loop)
==49058==
void: 0
main: 0
): 8
{: 9
==49058== Conditional jump or move depends on uninitialised value(s)
==49058== at 0x483FC57: strcat (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x1095F3: Lexer_LexNumber (lexer.c:69)
==49058== by 0x1096DD: Lexer_NextToken (lexer.c:86)
==49058== by 0x109A80: Loop_Compile (loop.c:8)
==49058== by 0x109AD4: main (in /home/ali/Desktop/loop/loop)
==49058== Uninitialised value was created by a heap allocation
==49058== at 0x483C7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x1093B6: Lexer_Init (lexer.c:9)
==49058== by 0x109A44: Loop_Compile (loop.c:6)
==49058== by 0x109AD4: main (in /home/ali/Desktop/loop/loop)
==49058==
1: 2
+: 4
2: 2
==49058== Invalid read of size 1
==49058== at 0x109454: Lexer_Advance (lexer.c:22)
==49058== by 0x109480: Lexer_AdvanceWithToken (lexer.c:28)
==49058== by 0x10991A: Lexer_NextToken (lexer.c:115)
==49058== by 0x109A80: Loop_Compile (loop.c:8)
==49058== by 0x109AD4: main (in /home/ali/Desktop/loop/loop)
==49058== Address 0x4a502be is 0 bytes after a block of size 30 alloc'd
==49058== at 0x483C7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==49058== by 0x109366: IO_ReadFile (io.c:17)
==49058== by 0x109ACC: main (in /home/ali/Desktop/loop/loop)
==49058==
}: 10
==49058==
==49058== HEAP SUMMARY:
==49058== in use at exit: 234 bytes in 15 blocks
==49058== total heap usage: 28 allocs, 13 frees, 5,884 bytes allocated
==49058==
==49058== LEAK SUMMARY:
==49058== definitely lost: 168 bytes in 10 blocks
==49058== indirectly lost: 66 bytes in 5 blocks
==49058== possibly lost: 0 bytes in 0 blocks
==49058== still reachable: 0 bytes in 0 blocks
==49058== suppressed: 0 bytes in 0 blocks
==49058== Rerun with --leak-check=full to see details of leaked memory
==49058==
==49058== For lists of detected and suppressed errors, rerun with: -s
==49058== ERROR SUMMARY: 12 errors from 4 contexts (suppressed: 0 from 0)
io.c:
char *IO_ReadFile(const char *filename)
{
char *buffer = 0;
long length;
FILE *f = fopen(filename, "rb");
if (f)
{
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(length);
if (buffer)
{
fread(buffer, 1, length, f);
}
fclose(f);
}
return buffer;
}
循环.c
void Loop_Compile(char *src)
{
lexer_t *lexer = Lexer_Init(src);
token_t *token = 0;
while ((token = Lexer_NextToken(lexer))->kind != TOKEN_EOF)
{
printf("%s: %d\n", token->value, token->kind);
}
}
词法分析器.c
lexer_t *Lexer_Init(char *src)
{
lexer_t *lexer = malloc(sizeof(lexer_t));
lexer->src = src;
lexer->src_size = strlen(src); // <----- Here
lexer->index = 0;
lexer->cc = src[lexer->index];
return lexer;
};
和词法分析器结构类型:
typedef struct LEXER_STRUCT
{
char *src;
size_t src_size;
char cc; // cc: current char
unsigned int index;
} lexer_t;
主.c
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Please Add A File\n");
return 1;
}
Loop_Compile(IO_ReadFile("./examples/main.loop"));
return 0;
}
最佳答案
strlen(src);
失败,因为 src
不一定指向 string。
IO_ReadFile()
不会返回指向字符串 的指针,因为该函数无法分配或追加空字符。 p>
尝试
//buffer = malloc(length);
buffer = malloc(length + 1);
if (buffer)
{
fread(buffer, 1, length, f);
buffer[length] = 0; // add
}
可能存在其他问题。
关于c - 使用 strlen() 时内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67034995/
有一个 strlen 和一个 wcslen 函数,但是有一个模板字符数组长度函数,所以你可以做一些类似 strlen 的事情或 strlen ? 如果没有,那我想我会自己写。 最佳答案 你有 std:
我目前正在编写一个需要频繁比较字符串长度的 C 程序,所以我编写了以下帮助函数: int strlonger(char *s1, char *s2) { return strlen(s1) -
我有一些代码获取一个文件,将每一行读入一个新的字符串数组(并向每个字符添加 128),然后将每个数组分配给一个指针数组,然后打印每个数组。尝试运行代码时,我收到一条错误消息,指出由于以下原因导致的段错
假设我有一个大小相同的字符串数组。 char strings[][MAX_LENGTH]; strlen(strings) 和 strlen(*strings) 之间有什么区别? 我知道 string
我不知道是什么原因导致这个问题...感谢任何帮助!我已经尝试了很多 strlen 代码,但这是唯一一个我可以实现且只有 1 个错误的代码。使用此代码,我尝试从文件中读取字符串,将其分解为由空格分隔的单
我有这个代码: int main() { char ch[15]; cout<
所以我正在学习嵌入式系统类(class),我们正在使用 C 语言。现在是第 2 周,所以我们只是刷新我们的 C 代码内存。 这段代码是如何打印出数字 6 的?幕后发生了什么? int main (vo
这个问题在这里已经有了答案: What do the parentheses around a function name mean? (3 个答案) 关闭 8 年前。 在 bstrlib.c(bs
编码 strlen($a); } “正确”的解决方案,在所有版本中都能正常工作(至少自从引入了飞船运算符(operator)以来)。 https://3v4l.org/6XRYW 关于php - P
我说strlen没用出于效率目的。因为如果你使用strlen那么你已经迭代了一个字符串,并且最好的算法总是迭代给定的容器不超过一次。所以请帮助我思考如何实现一个功能 bool contains ( c
这个问题已经有答案了: error: conflicting types for built-in function ‘tolower’ [-Werror] (2 个回答) 已关闭 4 年前。 我正在
我很困惑。有什么区别: char *someFunction(char *src) { char str[strlen(src) + 1]; ... return str; }
我有以下来自数据库的字符串:Let's Get Functional 如果我通过 strlen 运行它,它会返回 25 个字符而不是预期的 20 个字符。var 转储显示字符串看起来像上面的字符串(没
我正在使用 C 字符串库的 strlen 函数。我传递了一个 NULL字符串并找到神秘的结果。我知道我不应该传递 NULL 字符串,但我需要一个解释。代码看起来像这样 main() { int k
此代码返回 n=11,第 10 个和第 11 个字符为 ' ' 和 '@' 这是如何运作的? strlen函数怎么把它当成11个字符?在某些编译器中,它似乎将字符串长度设为 12 个字符。 #incl
以下代码能够确定 DWORD 的一个或多个字节是否设置为 0。 mov eax, value mov edx, 07EFEFEFFh add edx, eax xor eax, 0FFFFFFFFh
我在这里找到解决方案时遇到问题。我正在为使用 for() 的客户开发 WordPress 主题。循环遍历页面标题,以便将其包装在 中s 并垂直显示.. 循环使用 strlen()找到标题的长度,但由
我在理解 strlen 和/或 memcpy 时遇到问题。这是片段: char * restP; char * str; //this returns a pointer restP = strrst
好的,我正在检查一个字符串是否至少有 4 个字符长且至少有 25 个字符短 我试过这样使用strlen $userNameSignupLength = strlen($userNameSignup);
#include #include #include int main(void) { char qq[] = {'a' , 'b' , 'c' , 'd'}; char qqq
我是一名优秀的程序员,十分优秀!