Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 636 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read/write type values from "raw" memory in C?

#1
How do I make something like this work?

void *memory = malloc(1000); //allocate a pool of memory
*(memory+10) = 1; //set an integer value at byte 10
int i = *(memory+10); //read an integer value from the 10th byte
Reply

#2
So, by "work" I assume you mean "how do I dereference/perform pointer arithmetic on a `void*`"? You can't; you have to cast it, typically to a `char*` if you're just concerned with reading chunks of memory. Of course, if that's the case, simply declare it as a `char*` to begin with.
Reply

#3
Easy example: treat the memory as an array of unsigned char

void *memory = malloc(1000); //allocate a pool of memory
uint8_t *ptr = memory+10;
*ptr = 1 //set an integer value at byte 10
uint8_t i = *ptr; //read an integer value from the 10th byte

You can use integers too, but then you must pay attention about the amount of bytes you are setting at once.
Reply

#4
The rules are simple:

- every pointer type (except function pointers) can be cast to and from void*, without loss.
- you cannot perform pointer arithmetic on void* pointers, and cannot dereference them
- sizeof(char) equals 1, by definition; so incrementing a char pointer means "adding 1" to the "raw" pointer value

From this you can conclude that if you want to perform "raw" pointer arithmetic you have to cast to and from char*.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through