Thread: int * p = (void *) 1 seg fault

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    2

    int * p = (void *) 1 seg fault

    int * p = (void *) 1; /* seg fault. why is this happening ? */

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you actually expect "1" to be a valid memory address?

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,732
    The first MiByte of RAM in modern PCs is always reserved by the system for special purposes (old device support, for example).
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    2
    /*more specific*/
    Code:
    int * ptr = (void*) 1;
    printf("The value of ptr is %i\n",ptr);   /*why this is legal*/
    printf("The value of ptr is %ls\n",ptr); /*why this is seg fault*/

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,314
    Are you trying to learn C just by trying stuff to see what crashes and what doesn't?
    The problem with this approach is there is no guarantee of failure when you do something wrong.

    > printf("The value of ptr is %ls\n",ptr); /*why this is seg fault*/
    Not only is it dumb to try and dereference an int pointer to random memory locations, it's even dumber still to try and print it as a string.

    This about summarises what you can do with an int pointer in a typical user-space program running on any common OS.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ( ) {
        struct foo {
            int bar;
        } structvar;
        int arrayvar[10];
        int intvar;
        int *ptr;
    
        ptr = NULL;             // valid, but you can't dereference it
        ptr = &intvar;          // *ptr is valid
    
        ptr = arrayvar;         // *ptr is valid, as is ptr[0] tru ptr[9]
        ptr = &arrayvar[0];     // *ptr is valid, as is ptr[0] tru ptr[9]
        ptr = &arrayvar[5];     // *ptr is valid, as is ptr[0] tru ptr[4]
        ptr = &arrayvar[10];    // valid, but you can't dereference it.
                                // You can however use <, != comparisons with another
                                // ptr to the same array
    
        ptr = &structvar.bar;   // *ptr is valid
    
        ptr = malloc( sizeof(*ptr));    // *ptr is valid
        free(ptr);              // Only free what malloc returned.
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid conversion from 'const void*' to 'void*'
    By Wahidin Wahid in forum C++ Programming
    Replies: 10
    Last Post: 04-17-2012, 02:17 AM
  2. error invalid conversion from ‘const void*’ to ‘void*’
    By Wahidin Wahid in forum C Programming
    Replies: 3
    Last Post: 03-27-2012, 08:18 PM
  3. Replies: 3
    Last Post: 10-18-2011, 11:15 PM
  4. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM

Tags for this Thread