memory-address

print the memory location of a variable (or pointer)

旧巷老猫 提交于 2019-12-06 11:01:24
I want to print where a variable is stored. I Google it and I found this: int *p; printf("memory location of ptr: %p\n", (void *)p); If I write this, is it right? printf("memory location of ptr: %p\n", &p); I compiled it and I didn't get any errors or warnings. However, the above two commands didn’t return the same value! Lets say you have these declarations: int i; int *p = &i; It would look something like this in memory: +---+ +---+ | p | --> | i | +---+ +---+ If you then use &p you get a pointer to p , so you have this: +----+ +---+ +---+ | &p | --> | p | --> | i | +----+ +---+ +---+ So the

Calculating JMP instruction's address

久未见 提交于 2019-12-06 10:52:54
I am trying to hook a function by replacing its beginning with a JMP instruction which should lead to my function. But the problem is that I don't know how to calculate the JMP offset to target the address of my function. Well, I know how to do it if you jump forward in memory (Destination addr - Current addr), but I haven't got any ideas how to determine it when you jump back in memory. Could somebody help? Just use negative offset to jump backwards. And remember to account for the size of the JMP instruction. The offset is relative to the end of the JMP instruction and not the beginning. If

MemorySharp setting offset to an address not working

我与影子孤独终老i 提交于 2019-12-06 10:08:52
问题 Ok so I am using the MemorySharp library to read/write the memory of a game. My problem is when I try to add the offsets to the base pointer address Visual Studio throws an error during runtime. Here is the base code using (var m = new MemorySharp(ApplicationFinder.FromProcessName("Cube").First())) { IntPtr healthPtr = GetBaseAddress("Cube") + 0x0036B1C8; int[] offsets = {0x39c, 0x16c}; foreach(var offset in offsets) { healthPtr = m[healthPtr + offset].Read<IntPtr>(); //I'm getting the error

C: Does the address operator (&) produce a pointer (address + type) or just an address?

天大地大妈咪最大 提交于 2019-12-06 05:08:59
Most of what I've read about the address operator, & , says it's used to get just that - an address. I recently heard it described differently, though, as producing a full-fledged pointer. Given the following C code, int i1 = 5; int *p1; p = &i1; my understanding is that p1 references the int 5 by storing the address where i1 's data is stored and remembering that the data in that location is to be interpreted as an int (which dictates how many bytes to read and how to interpret the read data). Does the address operator yield both the address and the "type-awareness", or is the type-awareness

ASLR and addresses

谁说胖子不能爱 提交于 2019-12-05 23:05:41
Have a look at this main: int main() { int asd = 10; printf("%p\n", &asd); return 0; } Address of asd at at a given moment: 0x7ffff5f7c16c Address of main (always the same): (gdb) disass main Dump of assembler code for function main: 0x00000000004005b4 <+0>: push %rbp Why the addresses of the variables, of a regular c program, change at every execution, whereas the starting address of the program itself it is always the same (assuming that it is not position independent)? I see that the address variability is due to the ASLR mode, but why it does affect only the program variables, and does not

How is it that main function is always loaded at the same address whereas variables have different address most of the time?

假装没事ソ 提交于 2019-12-05 17:39:59
I wrote this small program today and I was blown away by the results. Here is the program int main(int argc, char **argv) { int a; printf("\n\tMain is located at: %p and the variable a is located at address: %p",main,&a); return 0; } on my machine the main function is always loaded at address "0x80483d4" and the address of the variable keeps on varying How does this happen? I read in operating systems that as a part of virtualization scheme the OS keeps relocating the address of instructions. So why is it that everytime I run this program that main is loaded at the same address? thanks in

Can pointer point to itself memory address in C?

孤街醉人 提交于 2019-12-05 15:23:45
In the following code, a pointer points to its own memory address. #include <stdio.h> int main() { void * ptr; ptr = &ptr; return 0; } Would it make sense, if the pointer was able to point to its own memory address? No, it doesn't make sense. If you can find variable ptr, you can just do &ptr. It will give you the same results as the contents of ptr. Moreover since ptr only tells something about itself, it's useless anyhow. It doesn't provide any info meaningful to the rest of your program. Come to think of it, there's one exception. You could use the case where ptr == &ptr as a kind of

how to get struct's start address from its member's address

ⅰ亾dé卋堺 提交于 2019-12-05 08:13:44
In C language, how to get struct's start address from its member's address? struct type1 { //... int member1; //... }; struct type1 obj1; And ptr1 is the address of member member1 in obj1 , how to define macro #define start_add(ptr1, type1, member1) to get obj1's start address? You can do this using offsetof : #define start_add(ptr1, type1, member1) ((type1 *)((char *)(ptr1) - offsetof(type1, member1))) azure this from windows wdk /* #define CONTAINING_RECORD(address, type, field) ((type *)( \ (PCHAR)(address) - \ (ULONG_PTR)(&((type *)0)->field))) */ 来源: https://stackoverflow.com/questions

Address of each character of std::string

╄→гoц情女王★ 提交于 2019-12-04 23:09:37
问题 I tried to print the address of each character of std::string . But I amn't understanding what is happening internally with std::string that is resulting this output while for the array it is giving the address as I expected. Could someone please explain what is going on? #include <iostream> #include <string> using namespace std; int main(){ string str = "Hello"; int a[] = {1,2,3,4,5}; for( int i=0; i<str.length(); ++i ) cout << &str[i] << endl; cout << "**************" << endl; for( int i=0;

When will memory used in a function become free ??(C programming)

拈花ヽ惹草 提交于 2019-12-04 21:06:21
Below is the code The Code: #include <stdio.h> int * num(void); int main(void) { int * num2; num2 =num(); printf("%d\n" , *num2); return 0; } int * num(void) { int num = 20; return &num; } The Question : As we know , the function num is local to its function num() , so in this code I try to return the address of the variable num in the function to the function that calls it , which is main() . After that I just use the dereferencing operator to extract the value of the specific num variable and print it out in main() function. There's one thing i'm confused . I remember i read a book about