Trying to implement a stack using dynamic array.
The Problem which i am facing is that if i a pushing 60 or 90 elements then the program is running fine but on inserting say 600 elements while printing the first two elements are showing junk value.
Compiled the code using DEVc++ and Codelite (both using GCC compiler)
In Codelite if i am trying to debug the number of junk values in the output increase.
#include <stdio.h>
#include <malloc.h>
#define INCREMENT 30
//int *array;
int capacity = 0;
int size = 0;//this will also be used as a index
int push(int *array,int val)
{
array[size++] = val;
if(size == capacity)
capacityUP(array,capacity+INCREMENT);
return 1;
}
int pop(int *array)
{
size-=1;//index is one less than the size
if(size < 0)
return 0;
else
return array[size];
}
int capacityUP(int *array,int ncapacity)
{
int i ;
realloc(array,sizeof(int)*ncapacity);
capacity = ncapacity;
printf("increasing capacity to %d \n",capacity);
return 1;
}
int capacityDown();
main()
{
int i;
int *array = (int*)malloc(sizeof(int) * INCREMENT);
capacity = INCREMENT;
for(i = 0 ; i < 600 ; i++)
push(array,i);
printf("size is %d \n",size);
do
{
printf("poping the top %d \n", pop(array));
}while(size);
printf("size is %d \n",size);
getchar();
}