How to analyze this FIFO program? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here are some pieces of the program that completes FIFO buffer pool. I don't know how does it complete. Does anyone help me to analyze it?
This is a struct of FlameHandle, this struct stores total flames, the first frame, and the last frame.
typedef struct FlameHandle{
PageFlame *first;
PageFlame *last;
PageFlame *totalFlames;
int numWriteIO;
int numReadIO;
} FlameHandle;
This is a struct of PageFlame, this struct stores info of each flame.
typedef struct PageFlame{
char *data;
PageNumber pageNum;
bool isDirty;
int fixCount;
struct PageFlame *next;
struct PageFlame *prev;
} PageFlame;
This is a struct of BM_BufferPool
typedef struct BM_BufferPool {
char *pageFile;
int numPages;
ReplacementStrategy strategy;
void *mgmtData; // use this one to store the bookkeeping info your buffer
// manager needs for a buffer pool
} BM_BufferPool;
This is a struct of BM_PageHandle
typedef struct BM_PageHandle {
PageNumber pageNum;
char *data;
} BM_PageHandle;
This is a function pinPage.
RC pinPage(BM_BufferPool *const bm, BM_PageHandle *const page, const PageNumber pageNum)
{
FlameHandle *FH;
FH = bm->mgmtData;
page->data = (char*)malloc(sizeof(char)*PAGE_SIZE);
SM_FileHandle fh;
PageFlame *pf;
openPageFile(bm->pageFile,&fh);
page->pageNum = pageNum;
for(int i=0; i<bm->numPages;i++)
{
pf = &(FH->totalFlames)[i];
if((FH->totalFlames[i]).pageNum == NO_PAGE)
{
FH->totalFlames[i].pageNum = pageNum;
pf->fixCount++;
ensureCapacity(pageNum+1,&fh);
//Reads new flame from fh and stores it to (FH->first)->data).
readBlock(pageNum,&fh,(FH->first)->data);
FH->numReadIO++;
strcpy(page->data,(FH->first)->data);
FH->last = FH->first;
if(FH->last->next == NULL)
{
FH->first = FH->totalFlames;
}
else
{
(FH->first) = (FH->last)->next;
}
return RC_OK;
}
}
}
In this function, It needs to store a new flame in an empty flame (pageNum == NO_PAGE), and at the same time, it needs to keep the FIFO sequence. I really cannot understand the semantics of following code:
if(FH->last->next == NULL){
FH->first = FH->totalFlames;
}else
{
(FH->first) = (FH->last)->next;
}
Does anyone can help me?
Here is the link of the whole project on GitHub:
https://github.com/randywhisper/assign_cs525/blob/master/assign2/buffer_mgr.c

Remember that the code you pasted there is RS_FIFO.
You can run the code using your pen.
In initBufferPool function, you create a Doubly linked list named PageFlame.
PageFlame have N totalPageFlames.
PageFlame's fist points to totalPageFlames[0], last points to end.
FIFO is first in first out.
Suppose you have a PageFlame(length 4) like a,b,c,d.
Here comes your snippet code.
FH->last = FH->first, last->next is b, not NULL, so goes to (FH->first) = (FH->last)->next;.
That means , last points to a while first points to b.
Next run, last points to b while first points to c.
a is read first and then first moves to next one b.
I think this is clear.
When there is only one element d and d->next is NULL.
So first will move from last out element c to d. When next element e comes in the queue, first will move to the e and last stay in d.
RS_FIFO, first in first out. That is it.

Related

How can I access the changed version of my linked list?

I'm currently working on sorting algo. project. We have a limited amount of operations we can use and we are allowed to only use 2 stacks. One of the operations is called pushA and pushB.
The push operation is supposed to push the top value of one stack to the top of the other stack.
This is my current implementation:
typedef struct stack
{
struct stack *prev;
int data;
int size;
struct stack *next;
} t_list_a, t_list_b;
int main(int argc, char *argv[])
{
t_list_a *tail_a;
if (!(ft_isdigit(argv, argc)) || !(checkDups(argv, argc)))
return (1);
tail_a = createList(tail_a, argv, argc);
}
t_list_a *createList(t_list_a *tail_a, char **argv, int argc)
{
int i;
tail_a = firstNode_A(ft_atoi(argv[argc - 1]));
i = argc - 2;
while (i > 0)
{
tail_a = addEnd_A(tail_a, ft_atoi(argv[i]));
i--;
}
tail_a->size = argc;
return (tail_a);
}
firstNode_A and addEnd just either creates the first node or adds a node to the other
Now to the issue
void pushB(t_list_a *tail_a, t_list_b *tail_b)
{
if (!tail_b)
tail_b = firstNode_B(tail_a->data);
else
tail_b = addEnd_B(tail_b, tail_a->data);
tail_a = delLast_A(tail_a);
}
ISSUE:
If I access tail_b.data it's now accessible and tail_a is updated accordingly. However, If I try to declare a t_list_b *tail_b in my main it's not accessible.
I do understand the issue that the struct ptr to the struct is not accessible outside the local scope. However, I don't know how to remedy it.
I've tried creating a double ptr inside the struct and outside and giving the address of tail_a and tail_b to the ptr.
I was thinking to create an array of structs.
P.S:
I'm a bit lost, also this is my second time asking a question so I expect there are some flaws in "how I ask". I would appreciate it a lot if you could indicate how I could ask my questions better so I can learn. Thank you.

C Read from File into LinkedList

I have a text file that I need to read a populate a linked list. The file structure is like this.
Ant,Adam 10 5
Mander,Sally 4 3
King,May 6 6
King,Joe 9 6
Graph,Otto 2 5
Carr,Redd 1 3
The name is szName. The second int is iDepartTmUnits, and the last int is iTime;
I'm trying to read the input from stdin
It should insert EVT_ARRIVE and EVT_DEPART events into the simulation's eventList. Assuming you are using fgets and sscanf, please make certain you check the count returned from your sscanf.
// Event Constants
#define EVT_ARRIVE 1 // when a person arrives
#define EVT_DEPART 2 // when a person departs the simulation
We have these structures
typedef struct
{
char szName[16]; // Name
int iDepartTmUnits; // time units representing how long he/she stays around
} Person;
// Event typedef (aka Element)
typedef struct
{
int iEventType; // The type of event as an integer:
// EVT_ARRIVE - arrival event
// EVT_DEPART - departure event
int iTime; // The time the event will occur
Person person; // The person invokved in the event.
} Event;
// NodeLL typedef - these are the nodes in the linked list
typedef struct NodeLL
{
Event event;
struct NodeLL *pNext; // points to next node in the list
} NodeLL;
// typedefs for the ordered link list
typedef struct
{
NodeLL *pHead; // Points to the first node in the ordered list
} LinkedListImp;
typedef LinkedListImp *LinkedList;
// typedefs for the Simulation
typedef struct
{
int iClock; // clock time
LinkedList eventList; // A linked list of timed events
} SimulationImp;
typedef SimulationImp *Simulation;
Now where I am struggling is how to populate a linked list with this information.
Actually I'm struggling with a lot to grasp my head around this, so I'm sorry at the question being overly complex or overly simple.
First Thing I am struggling with
I'm declaring it as
void generateArival(Event eventM[])
I believe that is incorrect, because in my main, I wouldn't pass it the event, I believe I would pass it a Simulation implementation.
Second Thing I Am struggling with
Here is the code I have so far where I am too copy from the file into a linked list.
while(!feof(pFilePerson))
{
iLineCount++;
i++;
fgets(szInputBuffer, MAX_LINE_SIZE, pFilePerson);
iScanfCnt = sscanf(szInputBuffer,"%s %d %d\n",
event.person.szName,
event.iTime,
event.person.iDepartTmUnits,
);
}
Lastly
I am to input the EVT_ARRIVE and EVT_DEPART into the eventList.
I believe that to be something like this,
they are int 1 and 2 respectfully, so I would need something like iEvent = event.iEventType;
and input that into the sim->eventList
Any help is appreciated, I need a lot more time with this concept of linked lists, but this is breaking my head.
EDIT
I can print out the name but not the numbers
while(fgets(szInputBuffer, sizeof szInputBuffer, pFilePerson) != NULL)
{
// print the input buffer as is (it also has a linefeed)
//printf("Person # %d: %s\n", iLineCount, szInputBuffer);
sscanf(szInputBuffer,"%s",
event.person.szName);
sscanf(szInputBuffer, "%I64d",
&event.person.iDepartTmUnits);
//linkList.event.iTime);
printf("%-7s\n", event.person.szName);
printf("%d\n", event.person.iDepartTmUnits);
}

corruption of the heap, error in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
well I dont need to explain too much, this function get this error at the line I marked all the time, only in this function there are allocations of memoery, please help me find out where is the issue...(read some other topics with that here, but nothing helped me solve it)
ERROR:"Windows has triggered a breakpoint in sapProject.exe.
This may be due to a corruption of the heap, which indicates a bug in sapProject.exe or any of the DLLs it has loaded.
"
void storeTok(char * lexem,int line,enum keywords typeof)
{
int ind;
if(tokens.size%100==0)
{
if(tokens.size==0)
{
tokens.ptrInd=-1;
tokens.first=(node*)malloc(sizeof(node));
tokens.first->back = NULL;
tokens.first->next = NULL;
tokens.last=tokens.first;
}
else
{
node * nodenz=(node*)malloc(sizeof(node)); /error is here
nodenz->back = tokens.last;
nodenz->next = NULL;
tokens.last->next=nodenz;
tokens.last=tokens.last->next;
tokens.last=nodenz;
}
}
// general
ind=tokens.size-(tokens.size/100)*100;
tokens.last->tokens[ind].type=typeof;
tokens.last->tokens[ind].linen=line;
tokens.last->tokens[ind].lexema=lexem;
tokens.size++;
}
Thanks!
edit(except this, there is also a header(and thats it..):
typedef struct token
{
char * lexema ;
int linen;
enum keywords type;
}token;
typedef struct node
{
struct node * next,*back;
token tokens [50];
}node;
typedef struct LL
{
struct node * last, *first, * ptr;
int size,ptrInd;
}LL;
LL tokens;
void storeTok(char * lexem,int line,enum keywords typeof);
main func:
void main()
{
int i;
for(i=0;i<26;++i)
{
storeTok("blabla",1,END);
storeTok("sdfasd",1,START);
storeTok("sfadds",1,IF);
storeTok("gvdfd",1,THEN);
storeTok("dfsfd",1,ELSE);
}
storeTok("dfsfd",1,EOF_);
}
That line is probably the victim. The code that corrupted the heap is elsewhere. You can use a tool like valgrind or a similar tool to find it. (Or give us enough code to replicate the error and we can track it down for you.)

adding of alternative node which contain interger value in give linked list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i wrote a code to add alternative node which contain integer value in single linked list.My code get crash please help me to fix it.
example lets take there are 6 node in singly linked list 3 5 8 6 4 9
then o/p should be 3+8+4 and 5+6+9 and my approach is wrong i guess please help me to fix it .In below code i am returning only one alternative value i.e 3+8+4 ?
void add(struct st **ptr)
{
struct st *curr,*prev;
curr=*ptr;
while(curr->next!=NULL)
{
if(curr->next->next->data!=NULL) //checking alternative node is present or
//or not and to avoid crash
{
sum= curr->data + curr->next->next->data;
}
else
{
sum= curr->data;
}
curr= curr->next;
}
prev=*ptr;
while(prev->next !=null)
{
prev=prev->next;
if(prev->next->next->data!=NULL)
{
sum=prev->data+prev->next->next->data;
}
else
{
sum=prev->data;
}
}
return sum;
}
First of all, It is not possible to return twice from a function. You can use call by reference as an alternative to it. Before the function call you can create two variables for two sums, initialize them to zero and send them as reference.
int sum_even = 0; //sum of elements at even position
int sum_odd = 0; //sum of elements at odd position
add(&start, &sum_even, &sum_odd); // call by reference
//sum_even and sum_odd will have the respective sums
Now for problem on linked lists it is advisable that you sit with pen and paper and try to trace each line of code you write. Testing boundary conditions is essential.
I have written a possible solution for your problem.
void add(struct st **ptr, int *sum_even, int *sum_odd)
{
struct st *even, *odd;
even = *ptr;
if(even->next) odd = even->next;
else odd = NULL;
while(even != NULL)
{
*sum_even += even->data;
if(even->next == NULL) break;
even = even->next->next;
}
while(odd != NULL)
{
*sum_odd += odd->data;
if(odd->next == NULL) break;
odd = odd->next->next;
}
}
if(curr->next->next->data!=NULL)
Here you should also check curr->next->next is not NULL.
Also, your if condition is also not correct. You should check for next!=NULL than data.
So update your ifs to
if(curr->next->!=NULL && curr->next->next->data!=NULL)
Also, try to use gdb or some other debugger to debug and see why your program is crashing and what are the problems.

i need to remove same elements from list in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
someone can end this algoritm?
void trinti_pasikartojancius(struct el * *prad, struct el * *pab, struct el * elem){
struct el *g, *elemk;
int i =1;
g = *prad;
elem= elem->kitas;
if(g->duom == elem->duom){
elem->kitas->pries = elem->pries;
elem->pries->kitas = elem->kitas;
free(elem);
if( g->kitas != NULL){
g = g->kitas;
g->pries = NULL;
free( *prad );
*prad = g;
}
else{
free ( *prad );
*prad = NULL;
*pab = NULL;
}
}
}
(Translated variable names to English, via Google Translate. I believe the question was meant to be "Can someone complete this algorithm?")
void delete_duplicate (struct e ** start, struct ** e end, struct elem * e) {
struct e * g * elemk;
int i = 1;
g = * start;
elem = elem-> next;
if (g-> conn == elem-> data) {
elem-> next-> v = elem-> before;
elem-> v-> next = elem-> next;
free (items);
if (g-> next! = NULL) {
g = g-> next;
g-> before = NULL;
free (* start);
* start = g;
}
else {
free (* start);
* start = NULL;
* end = NULL;
}
}
}
Assuming (as Google tells me) that pries and kitas are Lithuanian for "previous" and "next," then we have a doubly-linked list, and I guess we're removing an element.
The problem appears to be that you don't have a loop around your if/else. As your program is written, without the loop, g->duom == elem->duom can't be true unless prad (start) is elem.
But since you already know the node to remove (elem), why look for it? Start with this:
elem->kitas->pries = elem->pries;
elem->pries->kitas = elem->kitas;
free(elem);
This removes elem from whatever list it's in.
But, you might also have two conditions to watch for. elem could be the beginning of the list (prad) or the end of the list (pab) or both. So, if those are important (sometimes they are not), check for them separately.
If you want/need to be paranoid, you should also loop through the list (exactly like a search routine, which I assume you already have written) to make sure that prad, pab, and elem are all part of the same list.
If you need a more complete guide through the process, there is a full sample program available with every list operation implemented. It may not work for your application, but will at least point the way forward if you get lost.
Edit: Having gone deeper in depth on the translation attempt, I now see what's going on. Deleting duplicates of the elem parameter makes much more sense.
My second paragraph is the important one. You need a loop. The code you produced even has space for a loop, where the indentation seems strange. It should look something like:
while (g != NULL) {
/* Your existing, indented code here */
g = g->kitas;
}

Resources