Segmentation error, running kali Virualbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • futSecGuy1990
    New Member
    • Apr 2015
    • 15

    Segmentation error, running kali Virualbox

    #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_nonpointe r;

    hacky_nonpointe r = (unsigned int) char_array;

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

    // char_pointer = (void *) ((char *) char_pointer + 1);
    // ==> inc by char* data_typ then conv back to void *

    hacky_nonpointe r = (unsigned int) int_array;

    for(i=0; i<5; i++)
    { // Iterate through hacky_nonpointe r with the size_of(int) func.
    printf("[hacky_nonpointe r] points to %p, which contains the integer %d\n",
    hacky_nonpointe r, *((int *) hacky_nonpointe r));
    hacky_nonpointe r = hacky_nonpointe r + 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:~#
  • futSecGuy1990
    New Member
    • Apr 2015
    • 15

    #2
    ignore the comment b/w the two for loop

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You might read this. Then post again.

      Comment

      • futSecGuy1990
        New Member
        • Apr 2015
        • 15

        #4
        didnt help with what im trying to do, did you see an error?
        im trying to assign the mem address to the unsigned var and your the type casting to display it and sizeof(data_typ e) to iterate through the array

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          The code you posted compiles and runs on my machine using Visual Studio 2013.

          Comment

          Working...