Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing specific memory locations in C

Tags:

c

assembly

In assembly language we have instructions like:

movl ax, [1000]

This allows us to access specific memory locations.

But in C can we do something similar to this?

I know inline assembly code using asm() will allow you to do this, but I would like to know about some C specific technique to achieve this.

I tried the following code and got segmentation error:

int *ptr=0xFE1DB124;
*ptr;

This again was confusing as the memory location was identified by the code given below:

int var;
printf("\nThe Address is %x",&var);

So the memory location is available, but I am still getting a segmentation fault.

Why?

like image 1000
Deepu Avatar asked Mar 26 '13 13:03

Deepu


People also ask

How do you access the memory location of a variable in C?

In C, we can get the memory address of any variable or member field (of struct). To do so, we use the address of (&) operator, the %p specifier to print it and a casting of (void*) on the address.

How is memory accessed in C?

When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.

How do you access the memory address of a variable?

Usually memory addresses are represented in hexadecimal. In c++ you can get the memory address of a variable by using the & operator, like: cout << &i << endl; The output of that cout is the memory address of the first byte of the variable i we just created.

How do you find the memory location?

To access the memory location either you must know the memory location by its unique name or it is required to provide a unique address to each memory location. The memory locations are addressed from 0 to 2K-1 i.e. a memory has 2K addressable locations. And thus the address space of the computer has 2K addresses.


1 Answers

Common C compilers will allow you to set a pointer from an integer and to access memory with that, and they will give you the expected results. However, this is an extension beyond the C standard, so you should check your compiler documentation to ensure it supports it. This feature is not uncommonly used in kernel code that must access memory at specific addresses. It is generally not useful in user programs.

As comments have mentioned, one problem you may be having is that your operating system loads programs into a randomized location each time a program is loaded. Therefore, the address you discover on one run will not be the address used in another run. Also, changing the source and recompiling may yield different addresses.

To demonstrate that you can use a pointer to access an address specified numerically, you can retrieve the address and use it within a single program execution:

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>


int main(void)
{
    //  Create an int.
    int x = 0;

    //  Find its address.
    char buf[100];
    sprintf(buf, "%" PRIuPTR, (uintptr_t) &x);
    printf("The address of x is %s.\n", buf);

    //  Read the address.
    uintptr_t u;
    sscanf(buf, "%" SCNuPTR, &u);

    //  Convert the integer value to an address.
    int *p = (int *) u;

    //  Modify the int through the new pointer.
    *p = 123;

    //  Display the int.
    printf("x = %d\n", x);

    return 0;
}

Obviously, this is not useful in a normal program; it is just a demonstration. You would use this sort of behavior only when you have a special need to access certain addresses.

like image 155
Eric Postpischil Avatar answered Sep 29 '22 20:09

Eric Postpischil