| | |
Cursor Base implementation of list problem
Thread Solved |
Hello. I've been trying to solve this problem but I can't and my head hurts already. Please help me.
I think the problem is in the myMalloc function. Notice that there are two printf in there. One is for the temp value and one is for the VH-> first value. When I ran the program and input different names, sometimes it goes the way I wanted it to. Sometimes, the value of first suddenly becomes -16384 or any number that is not what I expected. This messes up the arrays causing some undesirable effect. The unexpected numbers comes out randomly depending on the names I inputted or the order of names (e.g.: I inputted the names "Kris" and "Allison" in this order and it goes smoothly. I input "Allison and "Kris" in this order, the value of first after "Kris" becomes -1. It shouldn't be -1 because the program only got 2 virtualheaps).
I don't get this. Please help.
c Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #define max 12 #define num 30 #define nam 24 typedef struct { char name[nam]; int score; int rank; }CONTESTANT; typedef struct { CONTESTANT CA; int next; }HeapSpace; typedef struct { HeapSpace *H; int first; }VirtualHeap, *VHeap; typedef struct { int storage[max]; int ladder[max]; int nextrung; }DualDataStruc, *DDS; void initialize(DDS *D, VHeap *VH); int myMalloc(VHeap *VH); void myFree(VHeap *VH, int x); int hash(char n[]); void insert(DDS *D, VHeap *VH, int NOP); void display(DDS D, VHeap VH); void initialize(DDS *D, VHeap *VH) { int ctr; *D = (DDS) malloc(sizeof(DualDataStruc)); *VH = (VHeap) malloc(sizeof(VirtualHeap)); (*VH)->H = (HeapSpace *) malloc(sizeof(HeapSpace) * num); (*VH)->first = 0; for(ctr = 0; ctr < num - 1; ctr++) { (*VH)->H[ctr].next = ctr + 1; } (*VH)->H[ctr].next = -1; for(ctr = 0; ctr < max; ctr++) (*D)->storage[ctr] = -1; (*D)->nextrung = 0; } int myMalloc(VHeap *VH) { int temp; temp = (*VH)->first; printf("%d\n", temp); if(temp != -1) { (*VH)->first = (*VH)->H[temp].next; printf("%d ", (*VH)->first); } return temp; } void myFree(VHeap *VH, int x) { int temp; if(x != -1) { (*VH)->H[x].next = (*VH)->first; (*VH)->first = x; } } int hash(char n[]) { int ctr, sum = 0; for(ctr = 0; ctr < strlen(n); ctr++) sum += (int)n; return sum % max; } void insert(DDS *D, VHeap *VH, int NOP) { int ctr; int x, h; char n[nam]; printf("Enter the name of the contestant:\n"); for(ctr = 0; ctr < NOP; ctr++) { flushall(); gets(n); x = myMalloc(&(*VH)); h = hash(n); strcpy((*VH)->H[x].CA.name, n); (*VH)->H[x].next = (*D)->storage[h]; (*D)->storage[h] = x; (*D)->ladder[++((*D)->nextrung)] = x; } } void display(DDS D, VHeap VH) { int ctr; for(ctr = 1; ctr <= D->nextrung; ctr++) puts(VH->H[D->ladder[ctr]].CA.name); getch(); clrscr(); } void main(void) { DDS D; VHeap VH; int NOP; initialize(&D, &VH); do { printf("How many players are there? "); scanf("%d", &NOP); if(NOP < 1 || NOP > 12) { printf("You can only input 1 to 12 players"); getch(); } clrscr(); }while(NOP < 1 || NOP > 12); insert(&D, &VH, NOP); display(D, VH); }
I think the problem is in the myMalloc function. Notice that there are two printf in there. One is for the temp value and one is for the VH-> first value. When I ran the program and input different names, sometimes it goes the way I wanted it to. Sometimes, the value of first suddenly becomes -16384 or any number that is not what I expected. This messes up the arrays causing some undesirable effect. The unexpected numbers comes out randomly depending on the names I inputted or the order of names (e.g.: I inputted the names "Kris" and "Allison" in this order and it goes smoothly. I input "Allison and "Kris" in this order, the value of first after "Kris" becomes -1. It shouldn't be -1 because the program only got 2 virtualheaps).
I don't get this. Please help.
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
0
#2 27 Days Ago
It works for both cases here.
I didn't see any glaring problems that relate to your issue.
At line 97 I think you intended something else ...
I'll look some more tomorrow.
One suggestion; name variables/types to give more info.
DDS should be PDDS and D should be pPDds etc. Something to let you know it's a ptr to a ptr NOT a ptr to a struct. As it is it's really hard to follow.
I didn't see any glaring problems that relate to your issue.
At line 97 I think you intended something else ...
C Syntax (Toggle Plain Text)
for(ctr = 0; ctr < strlen(n); ctr++) sum += (int)n[ctr]; // [ctr]<--- you want to sum the chars ?
I'll look some more tomorrow.
One suggestion; name variables/types to give more info.
DDS should be PDDS and D should be pPDds etc. Something to let you know it's a ptr to a ptr NOT a ptr to a struct. As it is it's really hard to follow.
Last edited by SVR; 27 Days Ago at 1:00 am. Reason: spelling
0
#3 27 Days Ago
•
•
•
•
It works for both cases here.
I didn't see any glaring problems that relate to your issue.
At line 97 I think you intended something else ...
C Syntax (Toggle Plain Text)
for(ctr = 0; ctr < strlen(n); ctr++) sum += (int)n[ctr]; // [ctr]<--- you want to sum the chars ?
I'll look some more tomorrow.
One suggestion; name variables/types to give more info.
DDS should be PDDS and D should be pPDds etc. Something to let you know it's a ptr to a ptr NOT a ptr to a struct. As it is it's really hard to follow.

Thanks for the advice, I'll take it.
0
#4 27 Days Ago
Solved it already. The problem was just in the hash function. I think it was because, if the name inputted is too big, the integer the letters will keep incrementing until it exceeds the maximum for integer giving some undesirable number. I just change the hashing function.
Thank you SVR. You made me look at the statement you quoted.
Thank you SVR. You made me look at the statement you quoted.
![]() |
Other Threads in the C Forum
- Previous Thread: File modification time as an int
- Next Thread: Need help
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





