SmootQ
SmootQ

Reputation: 2122

An observation about variable memory addresses in C

I have a question rather than a problem (witch maybe arises a memory question).. I've written this simple program:

#include <stdio.h>
#include <stdlib.h>

int multi(int x, int y);


int main(){
    int x;
    int y;
    printf("Enter the first number x: \n");
    scanf("%d",&x);
    printf("Enter the second number y: \n");
    scanf("%d",&y);
    int z=multi(x,y);
    printf("The Result of the multiplication is : %d\n",z,"\n");
    printf("The Memory adresse of x is : %d\n",&x);
    printf("The Memory adresse of y is : %d\n",&y);
    printf("The Memory adresse of z is : %d\n",&z);
    getchar();
    return 0;
}

int multi(int x,int y){
    int c=x*y;
    printf("The Memory adresse of c is : %d\n",&c);
    return c;  
}

As you can see (if you develop in C), this program inputs 2 int variables, then multiplies them with the multi function:

after getting the result , it displays the location of each variable in the memory (c,x,y and z).

I've tested this simple example those are the results (in my case):

The Memory adresse of c is : 2293556  
The Result of the multiplication is : 12  
The Memory adresse of x is : 2293620  
The Memory adresse of y is : 2293616  
The Memory adresse of z is : 2293612  

as you can see , the three variables x,y,z that are declared in the main function have closed memory adresses (22936xx) , the variable c that's declared in the multi function has a different adress (22935xx).

looking at the x,y and z variables, we can see that there's a difference of 4 bytes between each two variables (i.e : &x-&y=4, &y-&z=4).

my question is , why does the difference between every two variable equals 4?

Upvotes: 2

Views: 488

Answers (5)

Greg Hewgill
Greg Hewgill

Reputation: 994251

It appears that you are running on a 32-bit machine. The size of each int is 32 bits, and with 8 bits in a byte, the size of an int is 4 bytes. Each memory address corresponds to one byte, so there is a difference of 4 between the address of each local variable.

Upvotes: 2

Emil Sit
Emil Sit

Reputation: 23562

x, y, and z, are integer variables that will be created on the call stack (but see below). The sizeof int is 4 bytes, so that is how much space a compiler will allocate on the stack for those variables. These variables are adjacent to one another, so they are 4 bytes apart.

You can read about how memory is allocated for local variables by looking for information on calling conventions.

In some cases (where you do not use the address-of operator), the compiler may optimize local variables into registers.

Upvotes: 5

chrisaycock
chrisaycock

Reputation: 37928

Local variables are allocated on the "stack". Often the compiler will put them in sequential order since there's really no reason not to. An integer in C is 4 bytes. Therefore, it makes sense that y comes in 4 bytes after x, and z comes in 4 bytes after y.

Upvotes: 2

wkl
wkl

Reputation: 80031

In your situation the three variables were allocated in contiguous memory blocks. On x86 systems, int types are 32-bits wide, i.e. sizeof(int) == 4. So each variable is placed 4 bytes apart from the last.

Upvotes: 4

KevinDTimm
KevinDTimm

Reputation: 14376

The size of a machine word on your machine is 4 bytes so, for speed of access by your program they offset each variable on a 4 byte boundary.

Upvotes: 2

Related Questions