malloc:从内存获取信息

malloc:从内存获取信息,c,caching,malloc,C,Caching,Malloc,我目前正在做一个项目来模拟加载和存储信息,就像计算机缓存一样 我使用malloc将结构加载到内存中,但我不知道如何再次检索该信息。 对于任何行,我都需要能够调用缓存中的特定行,并查看它是否会像缓存那样导致“命中或未命中” struct cache {int s; int E; int b;}; //my struct //creating a similated cache in memory. struct cache** c1 = (struct cache**) malloc( pow(

我目前正在做一个项目来模拟加载和存储信息,就像计算机缓存一样

我使用malloc将结构加载到内存中,但我不知道如何再次检索该信息。 对于任何行,我都需要能够调用缓存中的特定行,并查看它是否会像缓存那样导致“命中或未命中”

struct cache {int s; int E; int b;}; //my struct

//creating a similated cache in memory.
struct cache** c1 = (struct cache**) malloc( pow(2,ssize) * Esize * sizeof(struct cache));`
基本上当你这样做的时候

struct cache** c1 = (struct cache**) malloc( pow(2,ssize) * Esize * sizeof(struct cache)). 
这是指向结构缓存指针的指针。它类似于指向结构缓存的指针数组,但它不在堆栈内存中,而是在堆内存中。尽管如此,你还是做错了。结构缓存的每个索引**假定等于结构缓存指针的大小。下面的代码可能解释了一切

为struct cache**获取空间后,每个索引都应该为其分配一个指向struct*cache的指针,或者为其分配一个指向struct cache的指针,其大小等于struct cache的大小。在下面的代码中,我使用malloc创建了10个索引。所以0-9

#include <stdlib.h>
#include <stdio.h>


struct cache {int s; int E; int b;}; //my struct

//creating a similated cache in memory.
int main(){
    // Each index equal the size of a pointer to struct cache and there is 10 index
    struct cache** c1 = (struct cache**) malloc( sizeof( struct cache* ) * 10 );

    // Each index is a pointer to struct cache with the malloc size it pointing
    // to equal to struct cache
    c1[0] = (struct cache*) malloc( sizeof(struct cache) );

    // Any later index would have to use malloc or assign a pointer to c1

    c1[0]->s = 1;
    c1[0]->E = 2;
    c1[0]->b = 3;

    printf("%d %d %d", c1[0]->s,
                       c1[0]->E,
                       c1[0]->b );
    return 0;

}
#包括
#包括
结构缓存{ints;inte;intb;}//我的结构
//在内存中创建相似的缓存。
int main(){
//每个索引等于指向结构缓存的指针的大小,有10个索引
结构缓存**c1=(结构缓存**)malloc(sizeof(结构缓存*)*10);
//每个索引都是指向结构缓存的指针,它指向的是malloc大小
//等于结构缓存
c1[0]=(结构缓存*)malloc(sizeof(结构缓存));
//任何以后的索引都必须使用malloc或将指针分配给c1
c1[0]>s=1;
c1[0]>E=2;
c1[0]>b=3;
printf(“%d%d%d”,c1[0]>s,
c1[0]>E,
c1[0]>b);
返回0;
}

这完全不清楚您想做什么以及您想问什么。关于
2的整数幂的标准注释-不要使用
pow
!使用位移位。我是否遗漏了什么或这里有实际问题?