Reputation: 628
I have been looking into some code for measuring latencies and one of the methods I came across deals with iterating through a linked list that is confined to a dynamically allocated memory space. I understand traditional linked lists pretty well and I understand dynamically allocated arrays, but putting the two together is throwing me through a loop. This may seem like a relatively basic question but I am not great with c and for whatever reason I am having a hard time constructing it.
I wanted to make strides of 128 bytes so I tried doing the following (I know I am probably way off base but I needed to start somewhere)
char** randomArray = malloc(accessSize); //allocate space for linked list
char** start=randomArray; //header for base adress
char** iterate; //iterator
char** end =randomArray+accessSize; //end address
for(iterate=start; iterate < end; iterate+=128){
*iterate = iterate+128; //step through in strides of 128 bytes, assign next addr to current value
}
*iterate=start; //linked list is circular, assign last element to first
I had no idea what data type to point to, normally you allocate space for a struct but that space already exists.
I then thought I would iterate through it as follows
for(counter = 0; counter < LotsOfAccess; counter++){
start = (char**) *start;
}
Well this didn't even work slightly for me and I can't seem to find out why. Does any have any suggestions for the proper way of implementing this or what I might be doing wrong?
Upvotes: 0
Views: 1210
Reputation: 628
Well did't get many bites on this one but I still think its a useful question so I will post what I did!
To create the linked list with strides of 128 bytes in a dynamically allocated space I did:
char * randomArray = malloc(accessSize*sizeof(char));
int counter;
char ** head = (char **) randomArray;
char ** iterate = head;
for(counter=0; counter < accessSize; counter+=128){
(*iterate) = &randomArray[counter+128];
iterate+=(128/sizeof iterate);
}
*iterate = head;
To traverse I did:
iterate = head;
for(counter=0; counter < NUM_ACCESSES; counter++){
iterate = *iterate;
}
Upvotes: 1