作为团队作业的一部分,我必须创建一个程序来读取汇编源文件并为特定架构生成二进制代码。
我创建了函数 tokenize 以根据提供的模式将字符串拆分为标记。
我遇到的问题是,当从 main() 调用 toks_print() 时,最后两行(标记)难以辨认,而当 toks_print() 从 read_assembly_file() 调用结果是一致的。
这是打印到标准输出上的输出:
这是读取的文件:
ldr r0,=0x20200004
ldr r2,[r0]
cmp r2,r0
andeq r0,r0,r0
打印 4 个 token :
ldr r0,=0x20200004
ldr r2,[r0]
cmp r2,r0
andeq r0,r0,r0
打印 4 个 token :
ldr r0,=0x20200004
ldr r2,[r0]
c ¿
\370\277_\377¿
我的问题是:为什么会这样? 我确定它与指针有关 但对于我的生活,我无法弄清楚。 我尝试过的任何其他文件也会发生这种情况:最后几行丢失或 无法辨认。
为了完整起见,这是 gpio_0.s 的内容:
ldr r0,=0x20200004
ldr r2,[r0]
cmp r2,r0
andeq r0,r0,r0
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////
typedef struct Tokens
{
char **toks;
unsigned int tokno;
} Tokens;
Tokens *toks_new()
{
Tokens *tokens = malloc(sizeof(Tokens));
tokens->toks = malloc(sizeof(char **));
return tokens;
}
void toks_free(Tokens *tokens)
{
free(tokens);
free(tokens->toks);
}
void toks_print(Tokens *tokens)
{
printf("Printing %i tokens:\n", tokens->tokno);
for (int i = 0; i < tokens->tokno; i++)
{
printf("%s\n", tokens->toks[i]);
}
printf("\n\n");
}
Tokens *tokenize(char *str, const char *delim)
{
Tokens *tokens = toks_new();
for (int n = 0; ; n++)
{
if (n != 0) str = NULL;
char *token = strtok(str, delim);
if (token == NULL)
{
tokens->tokno = n;
break;
}
tokens->toks[n] = token;
}
return tokens;
}
////////////////////////////////////////////////////////////////////////////////
Tokens *program = NULL;
////////////////////////////////////////////////////////////////////////////////
void read_assembly_program(const char *filepath)
{
FILE *file = fopen(filepath, "rt");
fseek(file, 0, SEEK_END);
long bytes = ftell(file);
rewind(file);
char buffer[bytes];
fread(buffer, 1, bytes, file);
// Without this I get an indecipherable line at the end... But why?
buffer[bytes-1] = '\0';
// What is printed is exactly what I expect, the whole content of the file
printf("This is the file read:\n%s\n\n\n", buffer);
program = tokenize(buffer, "\n");
// This prints the tokens as expected
toks_print(program);
}
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
const char * file = "gpio_0.s";
read_assembly_program(file);
// But here the last two lines messed up!
toks_print(program);
return EXIT_SUCCESS;
}
最佳答案
tokenize()
将对 buffer
的引用存储到 program
引用的数据中。
buffer
被声明为 read_assembly_program()
的本地内存,因此在 read_assembly_program()
被保留后内存不再有效。
为了绕过这个传递给 read_assembly_program()
一个对 buffer
的引用或者使用 malloc 在堆上分配
。buffer
()
更新
两个(不太好)替代解决方案:
- 全局定义缓冲区。
- 将“本地”缓冲区声明为
static
。
关于c - 从不同地方调用相同功能时的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043839/