c++.dos - access violation
- tomohawk007 htomail.com (245/245) Mar 27 2002 Hi there
- Walter (6/251) Mar 28 2002 I'd try building it and debugging it under Win32 first, and then port it...
Hi there I just downloaded Digital Mars & am having problems testing it on a small C program using DOS. Other small programs seem to work but this one doesn't. The only difference is that this program uses the malloc() function. I am getting an access violation: ****************************************************** Application exception occurred: App: (pid=468) When: 28/03/2002 16:19:37.957 Exception number: c0000005 (access violation) ****************************************************** Can you please tell me why this might be happening? Here is my program: *********************************************************************** /* A program that initializes a Linked Deque & presents the user with a menu to manipulate the Linked Deque. Functions are included to: Push, Append, Pop, Eject, Print, Size, Show menu/Clear screen & Quit */ #include <stdio.h> #include <stdlib.h> #include <alloc.h> #include <conio.h> #include <disp.h> struct NodeRec { int entry; struct NodeRec* next; }; typedef struct NodeRec Node; struct LinkedDequeRec { int count; Node* front; Node* rear; }; typedef struct LinkedDequeRec Deque; /* Make a new node which contains a value */ Node* makeNode (int value) { Node* newNodePtr = (Node*)malloc(sizeof(Node)); if (newNodePtr == NULL) { fprintf(stderr, "Out of memory"); exit(1); } else { newNodePtr -> entry = value; newNodePtr -> next = NULL; } return newNodePtr; } /* Initialize a Linked Deque to be empty */ void initializeDeque (Deque* dequePtr) { dequePtr -> front = NULL; dequePtr -> rear = NULL; dequePtr -> count = 0; } /* Push an item at the front of a Linked Deque */ void push (Deque* dequePtr, int item) { Node* newNodePtr = makeNode(item); if (newNodePtr != NULL) { if (dequePtr -> count == 0) { dequePtr -> front = newNodePtr; dequePtr -> rear = newNodePtr; dequePtr -> count = 1; } else { newNodePtr -> next = dequePtr -> front; dequePtr -> front = newNodePtr; dequePtr -> count++; } } } /* Append an item at the rear of a Linked Deque */ void append (Deque* dequePtr, int item) { Node* newNodePtr = makeNode(item); if (newNodePtr != NULL) { if (dequePtr -> count == 0) { dequePtr -> front = newNodePtr; dequePtr -> rear = newNodePtr; dequePtr -> count = 1; } else { dequePtr -> rear -> next = newNodePtr; dequePtr -> rear = newNodePtr; dequePtr -> count++; } } } /* Pops an item off the front of the Deque & returns it */ int pop (Deque* dequePtr) { int item; Node* oldNodePtr = dequePtr -> front; if (dequePtr -> count == 0) { fprintf(stderr, "Deque is empty!\n"); exit(1); } else { item = oldNodePtr -> entry; dequePtr -> front = oldNodePtr -> next; free(oldNodePtr); dequePtr -> count--; } return item; } /* Ejects an item from the rear of the Deque & returns it */ int eject (Deque* dequePtr) { int item, rearCount; Node* newRearPtr; if (dequePtr -> count == 0) { fprintf(stderr, "Deque is empty!\n"); exit(1); } else { item = dequePtr -> rear -> entry; newRearPtr = dequePtr -> front; for (rearCount=dequePtr->count; rearCount>2; rearCount--) { newRearPtr = newRearPtr -> next; } dequePtr -> rear = newRearPtr; free(newRearPtr -> next); dequePtr -> count--; } return item; } /* Prints out the items in a Linked Deque */ void printDeque(Deque* dequePtr) { Node* currentNodePtr; int itemCount; printf("Deque: "); if (dequePtr -> count == 0) { printf("Deque empty...\n"); } else { currentNodePtr = dequePtr -> front; for (itemCount=dequePtr->count; itemCount>0; itemCount--) { printf("%d ", currentNodePtr -> entry); currentNodePtr = currentNodePtr -> next; } printf("\n"); } } void showMenu (void) { disp_open(); disp_move(0,0); disp_eeop(); disp_close(); /* clrscr(); */ printf("Welcome to my little Deque manipulation thingy...\n"); printf("=================================================\n\n"); printf("1 Push\n2 Append\n3 Pop\n4 Eject\n5 Print\n"); printf("6 Size\n7 Clear screen/Show menu\n8 Quit\n\n"); printf("Enter option: "); } main() { int item, option; Deque* theDeque; initializeDeque(theDeque); showMenu(); scanf("%d", &option); while (option<1 && option>8) { printf("Not a vaild option... enter again: "); scanf("%d", &option); } while (option!=8) { if (option==1) { printf("Please enter an integer to push...\n"); while (scanf("%d", &item) != EOF) { push(theDeque, item); printDeque(theDeque); printf("Keep pushing...(crtl-Z to exit): "); } } else if (option==2) { printf("Please enter an integer to append...\n"); while (scanf("%d", &item) != EOF) { append(theDeque, item); printDeque(theDeque); printf("Keep appending...(crtl-Z to exit): "); } } else if (option==3) { item = pop(theDeque); printf("Popped: %d\n", item); } else if (option==4) { item = eject(theDeque); printf("Ejected: %d\n", item); } else if (option==5) { printDeque(theDeque); } else if (option==6) { printf("Size: %d\n", theDeque -> count); } else if (option==7) { showMenu(); } printf("Now what... "); scanf("%d", &option); while (option<1 && option>8) { printf("Not a vaild option... enter again: "); scanf("%d", &option); } } return 0; } ************************************************************************** Thanks
Mar 27 2002
I'd try building it and debugging it under Win32 first, and then port it to DOS. It's much easier to chase down pointer bugs that way. -Walter <tomohawk007 htomail.com> wrote in message news:a7uap2$55q$1 digitaldaemon.com...Hi there I just downloaded Digital Mars & am having problems testing it on a smallCprogram using DOS. Other small programs seem to work but this onedoesn't. Theonly difference is that this program uses the malloc() function. I am getting an access violation: ****************************************************** Application exception occurred: App: (pid=468) When: 28/03/2002 16:19:37.957 Exception number: c0000005 (access violation) ****************************************************** Can you please tell me why this might be happening? Here is my program: *********************************************************************** /* A program that initializes a Linked Deque & presents the user with a menu to manipulate the Linked Deque. Functions are included to: Push, Append, Pop, Eject, Print, Size, Show menu/Clear screen & Quit */ #include <stdio.h> #include <stdlib.h> #include <alloc.h> #include <conio.h> #include <disp.h> struct NodeRec { int entry; struct NodeRec* next; }; typedef struct NodeRec Node; struct LinkedDequeRec { int count; Node* front; Node* rear; }; typedef struct LinkedDequeRec Deque; /* Make a new node which contains a value */ Node* makeNode (int value) { Node* newNodePtr = (Node*)malloc(sizeof(Node)); if (newNodePtr == NULL) { fprintf(stderr, "Out of memory"); exit(1); } else { newNodePtr -> entry = value; newNodePtr -> next = NULL; } return newNodePtr; } /* Initialize a Linked Deque to be empty */ void initializeDeque (Deque* dequePtr) { dequePtr -> front = NULL; dequePtr -> rear = NULL; dequePtr -> count = 0; } /* Push an item at the front of a Linked Deque */ void push (Deque* dequePtr, int item) { Node* newNodePtr = makeNode(item); if (newNodePtr != NULL) { if (dequePtr -> count == 0) { dequePtr -> front = newNodePtr; dequePtr -> rear = newNodePtr; dequePtr -> count = 1; } else { newNodePtr -> next = dequePtr -> front; dequePtr -> front = newNodePtr; dequePtr -> count++; } } } /* Append an item at the rear of a Linked Deque */ void append (Deque* dequePtr, int item) { Node* newNodePtr = makeNode(item); if (newNodePtr != NULL) { if (dequePtr -> count == 0) { dequePtr -> front = newNodePtr; dequePtr -> rear = newNodePtr; dequePtr -> count = 1; } else { dequePtr -> rear -> next = newNodePtr; dequePtr -> rear = newNodePtr; dequePtr -> count++; } } } /* Pops an item off the front of the Deque & returns it */ int pop (Deque* dequePtr) { int item; Node* oldNodePtr = dequePtr -> front; if (dequePtr -> count == 0) { fprintf(stderr, "Deque is empty!\n"); exit(1); } else { item = oldNodePtr -> entry; dequePtr -> front = oldNodePtr -> next; free(oldNodePtr); dequePtr -> count--; } return item; } /* Ejects an item from the rear of the Deque & returns it */ int eject (Deque* dequePtr) { int item, rearCount; Node* newRearPtr; if (dequePtr -> count == 0) { fprintf(stderr, "Deque is empty!\n"); exit(1); } else { item = dequePtr -> rear -> entry; newRearPtr = dequePtr -> front; for (rearCount=dequePtr->count; rearCount>2; rearCount--) { newRearPtr = newRearPtr -> next; } dequePtr -> rear = newRearPtr; free(newRearPtr -> next); dequePtr -> count--; } return item; } /* Prints out the items in a Linked Deque */ void printDeque(Deque* dequePtr) { Node* currentNodePtr; int itemCount; printf("Deque: "); if (dequePtr -> count == 0) { printf("Deque empty...\n"); } else { currentNodePtr = dequePtr -> front; for (itemCount=dequePtr->count; itemCount>0; itemCount--) { printf("%d ", currentNodePtr -> entry); currentNodePtr = currentNodePtr -> next; } printf("\n"); } } void showMenu (void) { disp_open(); disp_move(0,0); disp_eeop(); disp_close(); /* clrscr(); */ printf("Welcome to my little Deque manipulation thingy...\n"); printf("=================================================\n\n"); printf("1 Push\n2 Append\n3 Pop\n4 Eject\n5 Print\n"); printf("6 Size\n7 Clear screen/Show menu\n8 Quit\n\n"); printf("Enter option: "); } main() { int item, option; Deque* theDeque; initializeDeque(theDeque); showMenu(); scanf("%d", &option); while (option<1 && option>8) { printf("Not a vaild option... enter again: "); scanf("%d", &option); } while (option!=8) { if (option==1) { printf("Please enter an integer to push...\n"); while (scanf("%d", &item) != EOF) { push(theDeque, item); printDeque(theDeque); printf("Keep pushing...(crtl-Z to exit): "); } } else if (option==2) { printf("Please enter an integer to append...\n"); while (scanf("%d", &item) != EOF) { append(theDeque, item); printDeque(theDeque); printf("Keep appending...(crtl-Z to exit): "); } } else if (option==3) { item = pop(theDeque); printf("Popped: %d\n", item); } else if (option==4) { item = eject(theDeque); printf("Ejected: %d\n", item); } else if (option==5) { printDeque(theDeque); } else if (option==6) { printf("Size: %d\n", theDeque -> count); } else if (option==7) { showMenu(); } printf("Now what... "); scanf("%d", &option); while (option<1 && option>8) { printf("Not a vaild option... enter again: "); scanf("%d", &option); } } return 0; } ************************************************************************** Thanks
Mar 28 2002