Tag: 分段 故障

在内存中执行机器代码

我正在试图弄清楚如何执行存储在内存中的机器代码。 我有以下代码: #include #include int main(int argc, char* argv[]) { FILE* f = fopen(argv[1], “rb”); fseek(f, 0, SEEK_END); unsigned int len = ftell(f); fseek(f, 0, SEEK_SET); char* bin = (char*)malloc(len); fread(bin, 1, len, f); fclose(f); return ((int (*)(int, char *)) bin)(argc-1, argv[1]); } 上面的代码在GCC中编译得很好,但是当我尝试从命令行执行程序时,如下所示: ./my_prog /bin/echo hello 程序段错误。 我已经发现问题出在最后一行,因为评论它会阻止段错误。 我不认为我做得很对,因为我仍然在考虑function指针。 这个问题是错误的演员,还是其他什么?

当我取消引用malloc中的NULL指针时,为什么我的程序不会出错?

我一直使用这种malloc风格 int *rc = 0; rc = malloc(sizeof(*rc)); 但是,即使我调用sizeof(*rc)我假设rc==0 ,并且我取消引用NULL指针,它也不会出错。

大数组在C中给出了分段错误

我是C的新手,所以如果这是一个绝对的初学者问题,我很抱歉,但是当我构建大型数组时,我遇到了分段错误,我正在做的相关内容是: unsigned long long ust_limit; unsigned long long arr_size; /* ust_limit gets value around here … */ arr_size = ((ust_limit + 1) / 2) – 1; unsigned long long numbs[(int)arr_size]; 这适用于某些ust_limit值,但是当它超过大约4.000.000时会发生分段错误。 我想要的是检测可能的段错误并优雅地失败。 如何知道哪些值会导致分段错误。 而且,这是平台依赖的吗?

btree实现中的分段错误

任何人都可以帮助删除此分段错误。 我正在研究这段代码一周仍无法调试。 此代码是Btree实现。 插入部分工作正常但删除时存在分段错误。 我无法调试它,有人可以帮忙吗? 我根据此链接给出了输入(已将字母值转换为ASCII值) http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html 当我删除第一个H (等效的ASCII值)时它可以正常工作,但是当我删除T (等效的ASCII值)时,我会得到一个分段错误。 #include #include #define M 5 struct node{ int n; /* n < M No. of keys in node will always less than order of B tree */ int keys[M-1]; /*array of keys*/ struct node *p[M]; /* (n+1 pointers will be in use) */ }*root=NULL; enum […]

使用scanf进行分段错误

noob问题:我正在尝试编写一个简单的菜单界面,但我不断收到分段错误,我无法弄清楚原因。 #include #include int flush(); int add(char *name, char *password, char *type); int delete(char *name); int edit(char *name, char *password, char *type, char *newName, char *newPassword, char *newType); int verify(char *name, char *password); int menu(){ int input; char *name, *password, *type, *newName, *newPassword, *newType; printf(“MAIN MENU \n ============\n”); printf(“1. ADD\n”); printf(“2. DELETE\n”); printf(“3. EDIT\n”); printf(“4. […]