Error message in Visual C++ Compiler

Simulation of queue using linked list

Page 1 of 1

2 Replies - 614 Views - Last Post: 03 November 2009 - 03:02 PM Rate Topic: -----

#1 j7x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 22-September 09

Error message in Visual C++ Compiler

Posted 03 November 2009 - 12:42 PM

I was using MS Visual C++ 6.0 for the first time for simulating queue using a singly linked list. The code runs fine in Turbo C++ IDE 3.0. But it's showing me the following message whenever I try to delete an element.

Posted Image

Here's my code.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#define MAX 10

struct linkedqueue
{
	int data;
	struct linkedqueue *next;
};

typedef struct linkedqueue *q;
int stat=0;

q insq(q hNode)
{
	q temp,cur;
	temp=(q)malloc(sizeof(q));

	if(stat==MAX)
	{
		printf("\nQueue_Overflow_Error. Re-run the program.\n");
		getch();
		return hNode;
	}
	stat++;
	cur=hNode;

	printf("\nEnter the value:\n");
	scanf("%d",&temp->data);
	temp->next=NULL;

	if(hNode==NULL)
	{
		return temp;
	}

	if(stat==1)
	{
		cur->next=temp;
		return hNode;
	}

	while(cur->next!=NULL)
		cur=cur->next;

	cur->next=temp;
	return hNode;
}

q delq(q hNode)
{
	q cur;
	
	if(stat==0)
	{
		printf("\nQueue_Underflow_Error.\n");
		return hNode;
	}

	if(stat==1)
	{
		printf("\nDeleted %d.\n",hNode->data);
		free(hNode);
		return NULL;
	}

	printf("\nDeleted %d.\n",hNode->data);
	cur=hNode;
	hNode=hNode->next;
	free(cur);
	return hNode;
}


void dispq(q hNode)
{
	q cur;

	if(stat==0)
	{
		printf("\nQueue_Empty_Error.\n");
		return;
	}
	cur=hNode;
	printf("\nThe queue elements are:\n");

	while(cur!=NULL)
	{

		printf("\n\t%d",cur->data);
		cur=cur->next;
	}
}
		

void main()
{

	q fNode;
	int opt;
	fNode=NULL;

	system("cls");
	while(1)
	{
		system("cls");
		printf("\nQueue counter: %d.\n\n",stat);
		printf("\nMenu:\n");
		printf("\n\t1. Insert\n\t2. Delete\n\t3. Display\n\t0. Exit\n\n");
		printf("\nInput please: ");
		scanf("%d",&opt);

		switch(opt)
		{
			case 1:		fNode=insq(fNode);
						break;
			case 2:		fNode=delq(fNode);
						getch();
						break;
			case 3:		dispq(fNode);
						getch();
						break;
			case 0:		exit(100);

			default:	printf("\nInvalid Input.\n");
						getch();
						break;
		}
	}
}
		





What could be the possible error?

Is This A Good Question/Topic? 0
  • +

Replies To: Error message in Visual C++ Compiler

#2 hackterr  Icon User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 293
  • Joined: 13-August 09

Re: Error message in Visual C++ Compiler

Posted 03 November 2009 - 12:52 PM

I think what this error is trying to say is that you have over written a block of allocated memory.
Could be because of a size mismatch in data and deletion
or could be an error due to some missing declaration
If any of the members is moved outside of the class, the message might disappear.
Is it occurring for a particular data or all data?

This post has been edited by hackterr: 03 November 2009 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5669
  • View blog
  • Posts: 22,517
  • Joined: 23-August 08

Re: Error message in Visual C++ Compiler

Posted 03 November 2009 - 03:02 PM

Bet all of this will boil down to this:
typedef struct linkedqueue *q;

confusing the issue, because this:
temp=(q)malloc(sizeof(q));
is not allocating the amount of memory you think.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1