Thread: Segmentation fault while writing value at given address.

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    2

    Segmentation fault while writing value at given address.

    Hi All,

    I am trying to write a value at given address location, But i am getting segmentation fault, Following is code, Please have look and help to find out the error cause.

    Code:
    #include <stdio.h> 
    
    
    int main(){
    
    
      *(volatile int*)0x879844156 = 1; // write
      int var = *(volatile int*)0x879844156; // read 
      printf("var=%d\n",var);
    
    
      return 0; 
    }
    
    
    ~/trial/pointer 307$ gcc pointer_conversion_1.c -w
    ~/trial/pointer 308$ ./a.out
    Segmentation fault

    Thanks and Regards,
    Rahul Kumar

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What system are you running this on?
    Why do you think you should have access to this address?
    What are you trying to do?

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    2
    Quote Originally Posted by algorism View Post
    What system are you running this on?
    Why do you think you should have access to this address?
    What are you trying to do?

    It is just example code, I am trying to write at particular location/address and read back the same value from that address.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Unless you know that you can write to specific address ranges on your platform, it's a bad idea to try. Modern computers randomize addresses now. You're not supposed to care what the addresses are.

    malloc() and free() still work in 90% of places.
    Code:
    int *var = malloc(sizeof *var);
    *var = 12;
    printf("*var=%d\n", *var);
    free(var);

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by rahkuma View Post
    It is just example code, I am trying to write at particular location/address and read back the same value from that address.
    Even if that would work it's an insane thing to do. What if that address happened to be in the middle of your program code (and you were allowed to write to it)? It could cause your program to do anything.

    You didn't bother to mention what system you are running this on, which neglect indicates Windows. But the following is true of Linux and macOS, too.

    Firstly, addresses are "virtual", which means that the numeric value that your program sees are not the same as the actual underlying location in RAM. Virtual Memory

    Secondly, you cannot access an arbitrary address, since only certain ranges of virtual addresses are mapped into your process. Anatomy of a Program in Memory

    Thirdly, even those ranges mapped into your process will only allow certain kinds of access. Some will be read-only, for example. Some may only be executable.

    Finally, as whiteflags said, for security reasons address ranges may be randomized, so you can't be sure what their actual (virtual) values will be. ASLR

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault: Cannot access memory at address
    By Joelito in forum Linux Programming
    Replies: 2
    Last Post: 05-15-2014, 07:00 PM
  2. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  3. Replies: 4
    Last Post: 10-27-2011, 05:17 AM
  4. Replies: 4
    Last Post: 05-05-2009, 05:35 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread