The issue I'm having is for a project that is simulating RAM memory using a void* that is allocated N number of bytes. Always multiples of 4. One of the requirements is that is has to use a void*, so I'm not able to use uint_32 or char to use it.
Here is a current
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdint.h>
int main(void) {
void* mem = malloc(4096); //allocate 4096 bytes
uint32_t* test1 = malloc(sizeof(uint32_t));
*test1=12344444;
uint32_t* test2 = malloc(sizeof(uint32_t));
*test2=56788888; //declaring some test values
memcpy(&mem, &test1, sizeof(uint32_t)); //I try to copy test1 to the start of void*
memcpy(&mem sizeof(uint32_t), &test2, sizeof(uint32_t)); //I try to copy test2 next to test1
print(mem);
return 0;
}
And I made a simple print to see what is being written to the buffer.
void print(void* buffer){
for(int i = 0; i<4096; i =sizeof(uint32_t)){
printf ("%d ", *((uint32_t*)(buffer i)));
}
}
The issue is that I'm getting some weird values in some of these indexes and my two tests are separated by some weird data after I do the first memcpy.
12344444 0 0 0 0 0 33 0 56788888
What am I doing wrong here? Is there another way while still using void* for the mem? What would I need to do to access the variable stored at the nth byte?
Thank you
CodePudding user response:
mem and test1 are already the pointers the destination and source buffer, so do this
memcpy(mem, test1, sizeof(uint32_t));