简体   繁体   中英

How to read & write 1 byte into a particular memory address?

I'm trying to read a single byte from a particular memory address of a structure followed by writing that same byte into that same address. My goal is to load 64-byte memory associated with that memory address into the cache line.

I have a structure variable testStructure of size 12584 . I tried the following code to read and write back 1 byte into the memory address,

unsigned char *p;
int forEachCacheLine = sizeof(testStructure);
printf("size of forEachCacheLine is %d\n", forEachCacheLine);

for (int i = 0; i<forEachCacheLine ; i+=64) {

    printf("i is %d\n",i);

    // read 1 byte 
    p=(unsigned char *)&testStructure+i;
    printf("Read from %p byte is %hhx\n", &testStructure+i, p);


    // write 1 byte
    *(unsigned char *)(&testStructure+i)=p;
    printf("Write into %p byte is %hhx\n\n", &testStructure+i, p);
}

upon running the code I get the following output:

size of forEachCacheLine is 12584
i is 0  
Read from 0x7f5d42e71f80 byte is 80
Write into 0x7f5d42e71f80 byte is 80

i is 64  
Read from 0x7f5d42f36980 byte is c0
Segmentation fault (core dumped)

As the output shows, the writing attempt in the first iteration was successful. However, the second iteration causes a Segfault . Any comments on why I'm getting this Segmentation fault ?

I'm not quite sure if this is the correct approach to achieve my goal. But as of now, this is the only way I can think off. If this is an incorrect approach can someone please suggest an alternate approach?

While your for loop only seems to increment i by 64 bytes each iteration, your debug output shows that the second read is 805376 bytes beyond the first - which is out of bounds and likely causing the segfault. 805376 is 64 times 12584. What this means is that each iteration is incrementing p by 64 teststructure 's instead of 64 chars .

Try replacing

p=(unsigned char *)&testStructure+i; 

with

p=((unsigned char *)&testStructure)+i;

This ensures that &teststructure is a char* and not a teststructure* when we add i to it.

On most modern systems, there is little to no performance gain from explicitly caching memory like this. If you really want the memory in cache though - try using __builtin_prefetch() on each 64 bytes of teststructure . This is a GNU extension that populates a cache line. You should also pay attention to it's optional arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM