How to find the address location of variables in C? - c

I have written a program that defines 3 int variables, and printed them out using %p format specifier.
The output is as follows:
0000000000000001
0000000000000002
0000000000000003
My question is now how do I figure out where in memory these variables are stored. Since I'm using the %p format specifier, are the outputs already the addresses in memory where variables are stored?
Here is my code:
int main(void)
{
int iX1 = 1;
int iX2 = 2;
int iX3 = 3;
printf("%p\n", iX1);
printf("%p\n", iX2);
printf("%p\n", iX3);
return EXIT_SUCCESS;
}

%p is only the format specifier for the data you provide.
To get the address of a variable use the & operator like this:
#include <stdio.h> // printf
#include <stdlib.h> // EXIT_SUCCESS
int main(void)
{
int iX1 = 1;
int iX2 = 2;
int iX3 = 3;
printf("%p\n", (void*){&iX1});
printf("%p\n", (void*){&iX2});
printf("%p\n", (void*){&iX3});
return EXIT_SUCCESS;
}
Output:
0x7ffee4f7e8e8
0x7ffee4f7e8e4
0x7ffee4f7e8e0

Related

How is typecasting and dereferencing works here

I am typecasting the address of a character variable whose address 0x7ffc684486ef to an integer pointer. The value at the address 0x7ffc684486ef is 16, when I dereference using the integer pointer it would read 4 bytes starting from address 0x7ffc684486ef, but it return value 0, how is it. Is my understanding wrong ?.
int main()
{
char var;
int *ptr;
ptr = (int *)&var;
printf("%p %p\n", &var, ptr);
*ptr = 16;
printf("%d %d\n", var, *ptr);
return 0;
}
O/P
0x7ffc684486ef 0x7ffc684486ef
16 0
Why the below code does not print anything.
#include <stdio.h>
#define ADDRESS 0x7ffc684486ef
typedef struct Point {
int x;
int y;
}Point;
int main()
{
Point *var = (Point *)(ADDRESS);
var->x = 2;
var->y = 5;
printf("Location: %p\n", var);
printf("Values: %d %d\n", var->x, var->y);
}
O/P:
NIL
C allows wildly unsafe pointer conversion such as ptr = (int *)&var; without protesting. So it's likely that you get the same address even after changing the pointer type.
However, when you de-reference the *ptr = 16; you invoke an impressive number of undefined behavior bugs: out of bounds access, possible misaligned access, strict aliasing violation, accessing memory you don't have access to, and so on. Don't do this, this code is incredibly broken and there's no telling what the result might be. What is undefined behavior and how does it work?
The second example is even worse:
How do you know there is memory you have access to at that address?
It's a misaligned address so you can't access it as a struct on most computers
The access of memory areas unknown by the compiler, such as hardware registers, has to be volatile qualified.
So it would seem that you make a misaligned address somewhere out in la-la-land and this too is wildly undefined behavior. There's no point in reasoning about any behavior you might encounter. Any form of behavior can happen.
Just to clarify the concept related to *p = 16 ...
Look at this piece of code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int numa = -1, num, numb = -1;
int *p = (int *)&num;
*p = 16;
printf("\n%d %d %d %d", *p, numa, num, numb);
}
When it runs it will output:
16 -1 16 -1
Now try changing the data type to char on the line that declares 'num':
#include <stdio.h>
#include <stdlib.h>
int main()
{
char numa = -1, num, numb = -1;
int *p = (int *)&num;
*p = 16;
printf("\n%d %d %d %d", *p, numa, num, numb);
}
you will receive "Segmentation fault".

Print the addresses of all the bytes occupied by an integer variable

Here's the code -
#include <stdio.h>
int main()
{
char character_1 = '0';
int integer_1 = 12321;
char character_2 = '1';
char character_3 = '2';
printf("Integer occupies %zu byte(s) of space.\n",sizeof(int));
printf("Address of Integer 1: %p\n",(void*)&integer_1);
printf("\n");
printf("Character occupies %zu byte(s) of space.\n",sizeof(char));
printf("Address of Character 1: %p\n",(void*)&character_1);
printf("Address of Character 2: %p\n",(void*)&character_2);
printf("Address of Character 3: %p\n",(void*)&character_3);
printf("\n");
return 0;
}
and, the generated output -
Integer occupies 4 byte(s) of space.
Address of Integer 1: 000000000061FE18
Character occupies 1 byte(s) of space.
Address of Character 1: 000000000061FE1F
Address of Character 2: 000000000061FE17
Address of Character 3: 000000000061FE16
I want to print the addresses of all the four bytes of space occupied by the integer variable integer_1, which means print all four of these - 000000000061FE18, 000000000061FE19, 000000000061FE1A and 000000000061FE1B. How do I do it?
Is this what you are trying to do?
#include <stdio.h>
int main()
{
int integer_1 = 12321;
unsigned char* p = (unsigned char*)&integer_1;
for (int i=0; i<sizeof(int); i++){
printf("Address: %p -> Value: %02hhx\n", p+i, *(p+i));
}
return 0;
}
EDIT: As pointed out by KPCT, working with void* is indeed possible, just a bit more tedious if you are also interested in the value pointed, and not the address only.
For example, adapting my above solution to use void*, would result in something like this
#include <stdio.h>
int main()
{
int integer_1 = 12321;
void* p = (void*)&integer_1;
for (int i=0; i<sizeof(int); i++){
printf("Address: %p -> Value: %02hhx\n", p+i, *((char*) p+i));
}
return 0;
}
where you would have to go through the cast to char* anyway
You would need to cast the int pointer to a char pointer (or an int8_t pointer), then step through each of the bytes, something like this:
char *cp = (char*)&integer_1;
for (int i = 0; i < sizeof(integer_1); ++i)
printf("Address of integer_1, byte %d: %p\n",i,cp++);

Why is this reference not working/ valid? [duplicate]

This question already has answers here:
Does C have references?
(2 answers)
Closed 4 years ago.
I'm learning referencing in C and here is my code:
#include <stdio.h>
int main(void)
{
int x = 5;
int &ref = x;
printf("%d\n", x);
printf("%p\n", &x);
printf("%p\n", &ref);
return 0;
}
When i compile, it shows these errors:
reference.c:7:6: error: expected identifier or '('
int &ref = x;
reference.c:11:18: error: use of undeclared identifier 'ref'
printf("%p\n", &ref);
2 errors generated.
What should i do to fix this and thanks in advance.
c does not have a reference type, you can only use point instead of ref.
#include <stdio.h>
int main(void) {
int x = 5;
int *ref = &x;
printf("%d\n", x);
printf("%p\n", &x);
printf("%p\n", &ref);
return 0;
}
A "reference" in C is called a pointer. Through the pointer you reference something. In your code you need to write:
#include <stdio.h>
int main(void)
{
int x = 5;
int *ref = &x; // now ref points to x
printf("%d\n", x); // print the value of x
printf("%p\n", &x); // print the address of x
printf("%p\n", &ref); // print the address of the pointer variable
printf("%d\n", *ref); // print the value of the int that ref is pointing to
return 0;
}

Printing what is stored in pointer prints what is stored in that memory adress

When I try to print this
#include <stdio.h>
int main(){
int x = 3;
int *ptr = &x;
//printf("Address is : %d\n",&ptr);
ptr++;
*ptr = 1;
printf("%d %d",x,ptr);
return 0;
}
The code outputs 3 1, shouldn't it be 3 (then address of ptr?). Then, when I uncomment first printf it prints out:
Address is : 6356744
3 6356752
Does anyone know what is going on?
You have several serious problems in your code.
1) You print a pointer value or an address of a variable using %d but should not. That is undefined behavior so we can't know what will happen. To print a pointer value or an address of a variable use %p and cast to a void pointer like:
printf("Address is : %p\n",(void*)&ptr);
2) You write to memory that is not allocated to your program. These lines:
ptr++;
*ptr = 1;
make you write the value "1" one step past x. So this is also undefined behavior.
Correction the above could give you this program:
#include <stdio.h>
int main(){
int x = 3;
int *ptr = &x;
printf("Address is : %p\n",(void*)&ptr);
ptr++;
// *ptr = 1;
printf("%d %p\n",x,(void*)ptr);
return 0;
}
With the possible output:
Address is : 0x7ffc5b0923c8
3 0x7ffc5b0923c8
but the output may change from run-to-run and system-to-system

Memory map while developing C application with Eclipse

Just for educational purpose I have written C code that gets out of array bound:
int main( int argc, char ** argv ) {
char *cp = "dabsf";
cp=cp+10;
printf("%c",*cp);
return 0;
}
I have letter n in output.
Is it possible somehow to see whole memory map and see what bytes are near cp array and find where is n?
I'm using MinGW compiler.
Here's some code to print memory out, you can use it to print memory around after any pointer you want (trying to print too much might give you an access violation, especially trying addresses before your first variable):
#include <stdio.h>
void printMemory(void *address, int rows, int cols) {
for(int r = 0, c; r < rows; ++r) {
c = 0;
printf("%8p ", (void *)(((unsigned int*)address) + (r*cols + c)));
for(; c < cols; ++c) {
printf("0x%08x ", *(((unsigned int *)address) + (r*cols + c)));
}
printf("\n");
}
}
int main(void) {
char *test = "Hello World!";
unsigned int value1 = 0xABABABAB;
unsigned int value2 = 0xDEADBEEF;
unsigned int value3 = 0xCAFEBABE;
printf("%s, %d, %d, %d\n", test, value1, value2, value3);
printMemory(test, 4, 2);
printf("\n");
printMemory(&value1, 1, 3);
return 0;
}
The output is (in my case the string was stored in a different place than the integers):
Hello World!, -1414812757, -559038737, -889275714
0x80486ad 0x6c6c6548 0x6f57206f
0x80486b5 0x21646c72 0x2c732500
0x80486bd 0x2c642520 0x2c642520
0x80486c5 0x0a642520 0x01000000
0xbf8aab50 0xabababab 0xcafebabe 0xdeadbeef
Also this works for debugging, but do not do this in real code, accessing memory addresses that aren't for your variables is undefined behavior.
To print the memory map of 10 positions from cp:
#include <stdio.h>
int main(void) {
int i;
char *cp = "dabsf";
printf("Address of 1st element of cp %p\n", cp);
for(i=1;i<=10;i++)
{
printf("Address of %c is %p\n",*(cp+i), cp+i); // cp+i is the same as &(*(cp+i))
}
return 0;
}
To get the address of any element after the array:
cp = cp + 8;
printf("Element at cp+8 is %c\n", *cp);
printf("Address of cp+8 is %p\n", cp);
Note: the output of the code just above may change in successive runs of the code.

Resources