gpt4 book ai didi

c - RET 时出现段错误

转载 作者:行者123 更新时间:2023-11-30 16:33:50 25 4
gpt4 key购买 nike

我有以下头文件:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
/** **/

// size: 1B
typedef enum string_proc_func_type_t{
REVERSIBLE = 0,
IRREVERSIBLE = 1
} __attribute__((__packed__)) string_proc_func_type;


// size: 12B
typedef struct string_proc_key_t {
uint32_t length; // 4B
char* value; // 8B (ptr)
} __attribute__((__packed__)) string_proc_key;
typedef void (*string_proc_func) (string_proc_key*);

// size 24B
typedef struct string_proc_list_t {
char* name; // 8B (ptr)
struct string_proc_node_t* first; // 8B (ptr)
struct string_proc_node_t* last; // 8B (ptr)
} __attribute__((__packed__)) string_proc_list;

// size 33B
typedef struct string_proc_node_t {
struct string_proc_node_t* next; // 8B (ptr)
struct string_proc_node_t* previous; // 8B (ptr)
string_proc_func f; // 8B (ptr)
string_proc_func g; // 8B (ptr)
string_proc_func_type type; // 1B
} __attribute__((__packed__)) string_proc_node;

string_proc_list* string_proc_list_create(char* name);
void string_proc_list_destroy(string_proc_list* list);

// Aux functions
uint32_t str_len(char* a);
char* str_copy(char* a);

我已经实现了辅助功能,如下所示:

uint32_t str_len(char* a) { 
uint32_t cont = 0;
while(a != NULL && *a != '\0') {
cont++;
a++;
}
return cont;
}

char* str_copy(char* a) {
uint32_t length = str_len(a);
char* copy = malloc(length);
for (uint32_t i = 0; i < length; ++i) {
copy[i] = a[i];
}
return copy;
}

现在我想在 Assembly 上实现 string_proc_list_create 函数,我最终得到了这个:

; C functions
extern malloc
extern free
extern fopen
extern fclose
extern fprintf
extern str_copy

; structs sizes
%define STRUCT_STRING_PROC_LIST_SIZE 24

; structs offsets
%define STRUCT_STRING_PROC_LIST_NAME_OFFSET 0
%define STRUCT_STRING_PROC_LIST_FIRST_OFFSET 8
%define STRUCT_STRING_PROC_LIST_LAST_OFFSET 16
section .data


section .text

global string_proc_list_create
string_proc_list_create:
push rbp
mov rbp, rsp

push rdi ; save ptr to name

; allocate mem for struct
mov rdi, STRUCT_STRING_PROC_LIST_SIZE
sub rsp, 8 ; align stack
call malloc
add rsp, 8 ; unalign stack
; get ptr to struct at RAX

pop rdi ; get ptr to name en RDI

; save ptr to struct at RBX
push rbx
mov rbx, rax

; copy name
add rsp, 8 ; align stack
call str_copy
add rsp, 8 ; unalign stack
; get ptr to name (copy) at RAX

; initialize struct (ptr to struct at RBX, ptr to name (copy) at RAX)
mov qword [rbx + STRUCT_STRING_PROC_LIST_NAME_OFFSET], rax
mov qword [rbx + STRUCT_STRING_PROC_LIST_FIRST_OFFSET], 0
mov qword [rbx + STRUCT_STRING_PROC_LIST_LAST_OFFSET], 0

pop rbx
pop rbp
ret

然后,我尝试执行这一行:

string_proc_list* list =  string_proc_list_create("aName");

但是我在 RET 时遇到段错误。我认为我可能破坏了堆栈框架,但我找不到我的代码有什么问题。有什么帮助吗?

最佳答案

问题不在于您发布的代码,它是完全正确的(如果您所说的就是您的意思),但很可能是您使用 str_copy 的地方。问题是您得到的字符串不是以空字符结尾的字符串。你

uint32_t length = str_len(a);
char* copy = malloc(length);

当您必须malloc(length+1);以允许最终的\0字符时。然后,将原始字符串中精确长度的字符复制到副本中。然后您应该在字符串中添加最后一个 \0 字符,这样就完成了。

您的代码没有问题,但它生成的不是字符串,而是一个包含原始字符串有值(value)内容的 char 数组。为了使其成为正确的字符串,您需要为空字符获取空间,并将其添加到字符串中,如下所示:

char* str_copy(char* a) { 
uint32_t length_plus_1 = str_len(a) + 1; // enough for null to be included
char* copy = malloc(length_plus_1);
for (uint32_t i = 0; i < length_plus_1; ++i) {
copy[i] = a[i];
}
return copy;
}

关于c - RET 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49566377/

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