CS Problem 321_HW3_1 output looks OK! |
/* Call this routine when a signal happens */
void mySignalHandler(int signal_number)
{
printf("mySignalHandler> hit signal %d! OK!\n",signal_number);
exit(1);
}
int foo(void) {
int *badPointer=(int *)0;
/* add code HERE to make mySignalHandler get called on a segfault */
printf("Now segfaulting (should call mySignalHandler)\n");
(*badPointer)=0;
printf("Back from segfault?!\n");
return 0;
}
/* Puts some memory at this pointer */
void notSoBad(void *ptr)
{
/* Your code here */
}
/* You may NOT change the foo routine! */
int foo(void) {
int *badPointer=(int *)0;
notSoBad(badPointer); /* Put memory at the bad pointer */
printf("Now accessing the pointer %p\n",badPointer);
(*badPointer)=0;
printf("I guess that's an OK pointer!\n");
return 0;
}
/* One linked-list node. Points to an allocated buffer */
struct llNode {
llNode *next; /* Points to next entry in the linked list */
char *buf; /* allocated buffer */
/* Constructor: create a linked-list node */
llNode(llNode *next_,char *buf_)
:next(next_), buf(buf_) {}
/* Return number of nodes in linked list */
int count(void) {if (next) return 1+next->count(); else return 1;}
/* Free all the allocated buffers in this list */
void freeBuf(void) {
delete[] buf; buf=0;
if (next) next->freeBuf();
}
};
/* Allocate number different buffers of this size.
Return a linked list of the allocated buffers. */
llNode *allocateBufs(int size,int number) {
llNode *head=NULL;
for (int i=0;i<number;i++) {
char *buf=new char[size];
llNode *n=new llNode(head,buf);
head=n;
if ((i%100)==0)
printf("Allocated %dth %d bytes at %p, link at %p\n",
i,size,buf,n);
}
return head;
}
/* Use the buffers, then throw them away. */
void processBufs(llNode *l,int size) {
llNode *cur=l;
while (cur!=0) {
cur->buf[size-1]='K';
cur=cur->next;
}
l->freeBuf();
}
/* You MAY NOT modify the foo routine... */
int foo(void) {
int nLinks=0;
try {
llNode *lA=allocateBufs(60000,1000);
processBufs(lA,60000);
llNode *lB=allocateBufs(61000,1000);
processBufs(lB,61000);
nLinks=lA->count()+lB->count();
/* don't worry about throwing away the lA and lB links... */
} catch (...) {
printf("Error allocating memory!\n");
}
return nLinks;
}