分段错误(在kali中使用gcc)

segmentation error (using gcc in kali)

我正在尝试将一个数组分配给 unsigned var,然后使用该 var 打印包含。我是 virtualbox 上的 运行 kali,我一直收到 seg 错误。我正在处理书中的代码,它是这样显示的:

#include <stdio.h>

int main()  
{  
    int i;
    char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
    int int_array[5] = {1, 2, 3, 4, 5};
    unsigned int hacky_nonpointer;

    hacky_nonpointer = (unsigned int) char_array;
    for(i=0; i<5; i++)  
    {   
        // Iterate through hacky_nonpointer with the sizeof(char).  
        printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",hacky_nonpointer, *((char *) hacky_nonpointer));  
        hacky_nonpointer = hacky_nonpointer + sizeof(char);  
    }

    hacky_nonpointer = (unsigned int) int_array;
    for(i=0; i<5; i++)   
    {    
        // Iterate through hacky_nonpointer with the size_of(int) func.   
        printf("[hacky_nonpointer] points to %p, which contains the integer %d\n",hacky_nonpointer, *((int *) hacky_nonpointer));   
        hacky_nonpointer = hacky_nonpointer + sizeof(int);   
    }   
}

编译器输出:

root@Brien:~# gcc -g -o ./pointer_types5 pointer_types5.c
pointer_types5.c: In function ‘main’:
pointer_types5.c:12:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pointer_types5.c:17:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pointer_types5.c:24:21: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
pointer_types5.c:29:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

运行时输出:

root@Brien:~# ./pointer_types5
Segmentation fault
root@Brien:~#

您似乎正在使用具有 32 位 int 类型和 64 位指针类型的系统。您需要为 hacky_nonpointer 使用足够大的类型来保存整个指针值。 uintptr_tstdint.h 提供,正是出于这个目的。

感谢您以一种方式回答了这个问题。 我碰巧正在阅读 OP 从中发布代码的相同文本。作者想让你故意使用unsigned int来说明一点(基于32位机器)。 对我有用的替代方法是: "include stdint.h" ... uint64_t hacky_nonpointer; // 一个 64 位无符号整数

hacky_nonpointer = (uint64_t) char_array;

查看下面的完整代码。

    #include <stdio.h>
    #include <stdint.h>
int main() {

int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};


// unsigned int hacky_nonpointer;   <-- This code does not work on 64-bit machines with 64 bit pointers
uint64_t hacky_nonpointer;
hacky_nonpointer = (uint64_t) char_array;

for(i=0; i < 5; i++) { // Iterate through the char array with the char_pointer.
printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n", hacky_nonpointer, *((char *) hacky_nonpointer));
hacky_nonpointer = hacky_nonpointer + sizeof(char);
}

hacky_nonpointer = (uint64_t) int_array;
for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
printf("[hacky_nonpointer] points to %p, which contains the integer %d\n", hacky_nonpointer, *((int *) hacky_nonpointer));
hacky_nonpointer = hacky_nonpointer + sizeof(int);
}
}

我已经用另一种方式回答了这个问题。

我需要做的就是检查 int * 指针的字节大小。我有 8 个字节,long int 也是。如果 hacky_nonpointer 设置为 long int 那么它会按预期工作。我系统下的 int 是 4 个字节,所以我什至在打印任何内容之前就遇到了分段错误。根据作者提供的插图,我的解决方案在我的系统上运行正常,没有 uint64_t.

int i;
char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
int int_array[5] = {1, 2, 3, 4, 5};

long int hacky_nonpointer;

hacky_nonpointer = (long int) char_array;

for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
    printf("[hacky_nonpointer] points to %p, which contains the char '%c'\n",
           hacky_nonpointer, *((char *) hacky_nonpointer));
    hacky_nonpointer = hacky_nonpointer + sizeof(char);
}

hacky_nonpointer = (long int) int_array;

for(i=0; i < 5; i++) { // Iterate through the int array with the int_pointer.
    printf("[hacky_nonpointer] points to %p, which contains the integer %d\n",
           hacky_nonpointer, *((int *) hacky_nonpointer));
    hacky_nonpointer = hacky_nonpointer + sizeof(int);
}