Stack in x86_64 in C program on linux [duplicate] - c

This question already has answers here:
Order of local variable allocation on the stack
(10 answers)
Where in memory are my variables stored in C?
(9 answers)
Address of a string literal and array
(5 answers)
What is the difference between returning a char* and a char[] from a function? [duplicate]
(6 answers)
Closed 1 year ago.
I thought the stack in x86_64 would grow down hence from the max. address of the processes virtuell memory till the end of the stack size (https://www.google.com/url?sa=i&url=https%3A%2F%2Fen.wikibooks.org%2Fwiki%2FX86_Disassembly%2FThe_Stack&psig=AOvVaw3PNsHC2wRGe3XfCCsCi9Kd&ust=1631093147793000&source=images&cd=vfe&ved=0CAkQjRxqFwoTCNiH6c_F7PICFQAAAAAdAAAAABBY).
However in the following C programm the the first char of b, which I thought should be pushed on the stack after a and thereby should have a lower address, can be accessed by adding a positive offset instead of a negative one. Why is that?
I also don't understand why the addresses of the pointers to the stings a and b as well as the long long int c are stored ad much higher addresses. Whats in between the 0x7ff... and the 0x55f... addresses? A gap in the Stack?
#include <stdio.h>
int main() {
char *a = "abcdefg";
char *b = "mnopqrs";
printf("address a: %p\n", a);
printf("address &a: %p\n", &a);
printf("address b: %p\n", b);
printf("address &b: %p\n", &b);
printf("Char at addr %p: %c\n", a+8, *(a+8));
long long c = 0;
printf("address c: %p\n", &c);
return 0;
}
Output:
address a: 0x55fa2e1c8004
address &a: 0x7ffc148d29b0
address b: 0x55fa2e1c800c
address &b: 0x7ffc148d29b8
Char at addr 0x55fa2e1c800c: m
address c: 0x7ffc148d29c0

Related

How to use memory address to dereference? [duplicate]

This question already has answers here:
Pointers in C: when to use the ampersand and the asterisk?
(10 answers)
Closed 14 days ago.
I create a pointer and get its memory address. How can i dereference the pointer?
#include <stdlib.h>
int main()
{
int b[] = {11,22,33,44,55};
int *a = b;
printf("memory:%p\n",a);
return(0);
}
suppose the memory address of b is 0x7fff24e69e60. How can i get the b[0] by the memory address?
How to use memory address to dereference in C language?
In C programming, to dereference a memory address, you use the * operator.
int x = 5;
int *ptr = &x;
printf("Value of x: %d\n", x);
printf("Value stored at address %p is: %d\n", ptr, *ptr);
Here, ptr is a pointer to an integer and is assigned the address of x using the & operator. The *ptr expression dereferences the pointer and accesses the value stored at the memory address ptr.

What does int* ip = (int *)p mean? [duplicate]

This question already has an answer here:
Converting unsigned char array to an integer
(1 answer)
Closed 8 months ago.
#include <stdio.h>
int main(){
char p[5] = "ABCD";
int* ip = (int *)p;
printf("%d \n", *(ip + 0)); // 1145258561
printf("%d \n", ip); // 6422016
}
Could anybody please explain to me the output of this program?
Here you cast the char[4], p, into an int* and initialize ip with the result:
int* ip = (int *)p;
Here you dereference ip, which, since it's an int* means that it will read sizeof(int) bytes to form an int from the address ip points at:
printf("%d \n", *(ip + 0)); // 1145258561
Since ints are often 4 bytes, it will often seem to work, but it violates the strict aliasing rule and results in undefined behavior. Also, if an int is 8 bytes, the program would have undefined behavior since it would then read outside the char[4].
Here you print the value of ip as an int, but it is a pointer, so again, the program will have undefined behavior. Also, a pointer is often 8 bytes so it will likely cause undefined behavior for that reason too.
printf("%d \n", ip); // 6422016
To properly print pointers, use %p and cast the pointer to void*:
printf("%p\n", (void*) ip);

why looks different between array allocated pointer and variable allocated pointer in C LANGUAGE? [duplicate]

This question already has answers here:
Pointers - Difference between Array and Pointer
(2 answers)
Closed 1 year ago.
hello i am first question in stackoverflow!
i am not good at english :)but i tried it!
int numarr[1] = { 11 }; int* numptr1 = numarr; printf("%d", *numptr1);
result: 11
int num = 11;
int* numptr2 = &num;
printf("%d", *numptr2);
result: 11
i found difference!
int* numptr1 = numarr;
int* numptr2 = &num;
why variable uses "&" and why array not use "&" when i allocated?
I dont know difference... please tell me! Thanks you!
Pointer is a fundamental concept of C programming.
In computer science, a pointer is a programming language object that stores a memory address.
You can think of your computer's memory as a contiguous array of bytes. Each time that you make an innocent declaration and assignation such as int a = 5, this value is written into your computer's memory on 4 bytes (integer size). This value will be written at a specific memory address, the stack (fast access to memory) if no memory allocation, else it will be stored deeper in the heap. This address also has a value!
#include <stdio.h>
int main(void) {
int a = 5; // declaring an integer variable and assigning the value of 5
int *ptr; // declaring a pointer to integer
int b; // declaring an integer variable
printf("ptr's value: %2d, ptr's address: %p\n\n", *ptr, ptr);
ptr = &a; // pointer ptr points to what is stored at the memory address of variable a
b = a; // b will take the value and not the address
a = 42; // b is still equal to 5, but ptr will return 42, which is the value now stored at a's location;
printf(" a's value: %2d, a's address: %p\n", a, &a);
printf("ptr's value: %2d, ptr's address: %p\n", *ptr, ptr); // you will get the same as above, notice that you have to dereference the pointer with * to get the value, and using the pointer alone (ptr) will give you the memory address.
printf(" b's value: %2d, b's address: %p\n", b, &b);
//printf("Size of ptr: %zu\n", sizeof(ptr)); // size of ptr in bytes, 8 on my system.
return 0;
}
You will get this kind of outpu:
ptr's value: 1, ptr's address: 0x7ffd99493000
a's value: 42, a's address: 0x7ffd99492f08
ptr's value: 42, ptr's address: 0x7ffd99492f08 <-- they now match thanks to ptr = &a
b's value: 5, b's address: 0x7ffd99492f0c
NB: On the second printf you will get the value that you got for a, notice that you have to dereference the pointer with * to get the value, and using the pointer alone (ptr) will give you the memory address.
For your specific question about & What is an R value:
In many languages, notably the C family, l-values have storage addresses that are programmatically accessible to the running program (e.g., via some address-of operator like "&" in C/C++), meaning that they are variables or de-referenced references to a certain memory location

Why does a pointer "forget" its value despite pointing to malloc'ed memory [duplicate]

This question already has answers here:
How do I modify a pointer that has been passed into a function in C?
(7 answers)
Closed 3 years ago.
void init(int *a) {
int *b = malloc(sizeof(int));
*b = 4;
a = b;
printf("b address %d\n", b);
printf("a address %d\n", a);
printf("%d\n",*a);
}
int main()
{
int *a = malloc(sizeof(int));
printf("a address %d\n", a);
init(a);
printf("a address %d", a);
return 0;
}
will print the output
a address 32206864
b address 32211008
a address 32211008
4
a address 32206864
Here, the init function is initializing the value for a. However, this is done is incorrect and I am trying to determine why. Observe that once the init function ends the pointer a forgets the address it's supposed to point to. I assume this has something to do with the fact that a is set to b, a pointer that gets popped off the stack once the function ends.
But why does this make sense? Shouldn't a remember what memory address it's been set to? After all, the scope of a is the main function, not the init function.
The a in the function init and the a in main are different objects. The call init(a) only passes the value of a to the function. That value is only a copy of the value in the a in main. The init function does not receive any reference to the a in main.

What is the difference in C between &i and i if i is an array of integers? [duplicate]

This question already has answers here:
How come an array's address is equal to its value in C?
(6 answers)
Closed 9 years ago.
I tried a code to see what is the difference between &i and i if i is an array. My assumption was that i represents the starting address of the array, and I had no idea what will happen if I print &i as well.
The result was surprising (to me), as both i and &i was the starting address of the array.
#include<stdio.h>
int main()
{
char str[25] = "IndiaBIX";
int i[] = {1,2,3,4};
printf("%i\n", &i);
printf("%i\n", i);
return 0;
}
The result will be:
2686692
2686692
This is the same address.
But if I use a pointer to int:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int *j = (int *) malloc(sizeof(int));
*j = 12;
printf("%i %i %i\n", *j,j,&j);
return 0;
}
The result will be:
12
5582744
2686748
Here I assume the first is the value of the memory area pointed to by j,the second is the address of the memory area pointed to by j, the third is the memory address of the pointer itself. If I print i and &i, why does &i not mean the memory address of the pointer i?
int i[] = {1,2,3,4};
The difference is their type, i has a type of integer array, &i has a type of a pointer of an integer array.
Yeah, both i and &i leads to print the same answer but they are not exactly same,
-> i represents the address of the first element in an array named i.
-> &i represents the address of the whole array(though values of both are same, their types are different)
For more info, please refer this [link]http://publications.gbdirect.co.uk/c_book/chapter5/arrays_and_address_of.html
int ar[10];
ip = ar; /* address of first element */
ip = &ar[0]; /* address of first element */
ar10i = &ar; /* address of whole array */

Resources