Newbie to C here and following online book tutorials. (I have been attempting linked-lists but little complex for now, will return to these later.) I have the following algorithm:
Algorithm: PUSH (STACK, ITEM)
[STACK is an array of MAXSIZE and ITEM is an item to be pushed onto
stack]
1. [Check for stack overflow]
If TOP = MAXSIZE - 1 then
a) Print: Overflow
b) Return
2. [Increase top by 1]
Set TOP = TOP + 1
3. [Insert item in new top position]
Set STACK[TOP] = ITEM
4. Return
However, I cannot understand step 1, for Top, how would I go about initialising this, because Top should indicate some pre-existing value? For example:
(My attempt)
#include <stdlib.h>
#include <stdio.h>
int push(int item, int(*stack)[item]){
int top;
if(top == sizeof(*stack)/sizeof(*stack[0])-1){
printf("Overflow");
return -1;
}
top++;
*stack[top] = item;
return **stack;
}
int main(void){
int arr[4] = {0 , 1, 2, 3};
int itm = 2;
int result;
result = push(itm, &arr);
printf("\nResult: %i", result);
return 0;
}
Would always produce an overflow
You will simply need to store the current value of top and send it along with the stack everywhere. You should define a struct, like so:
typedef struct Stack {
int stack[16]; // Replace 16 with desired capacity.
int top;
} Stack;
Create a new stack like this:
Stack s = { .top = -1 };
Now all your functions should receive a pointer to this stack, and functions like push should update the value of top.
Related
Numbers are pushed onto the stack, you need to pop the full stack, after which, the stack in which elements less than 10 are removed. Example code when all elements of the stack are removed:
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
struct kkk
{
float elem[15];
int top; // index of the top rlrment
};
struct kkk* st, * element; // pointers
void Init(struct kkk* st) // initialization
{
st->top = NULL;
}
void Push(struct kkk* st, float f) // push an item onto the stack
{
if (st->top < 15)
{
st->elem[st->top] = f;
st->top++;
}
else
printf("Stack full\n");
}
float Pop(struct kkk* st) // pop an item from the stack
{
float el;
if ((st->top) > 0)
{
st->top--;
el = st->elem[st->top];
return el;
}
else
{
printf("Stack is empty \n");
return 0;
}
}
float Vulychtop(struct kkk* st) // deleting the top of the stack
{
if ((st->top) > 0) {
return(st->elem[st->top - 1]);
}
else {
printf("Stack is empty!\n");
return 0;
}
}
int Gettop(struct kkk* st) // top element of the stack without delting
{
return(st->top);
}
int Isempty(struct kkk* st) // check
{
if ((st->top) == 0)
return 1;
else return 0;
}
void Vuvid(struct kkk* st) // Output of all elements
{
int i;
i = st->top;
if (Isempty(st) == 1) return;
do {
i--;
printf("%f\n", st->elem[i]);
} while (i > 0);
}
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int i, n, k;
float znach;
element = (struct kkk*)malloc(sizeof(struct kkk));
Init(element);
printf("Enter the number of items in the stack \n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the number %d: ", i);
scanf("%f", &znach);
Push(element, znach);
}
printf("In stack %d elements \n", Gettop(element));
printf("\n");
Vuvid(element);
printf("Top element %f\n", Vulychtop(element));
do {
printf("The element to be removed %f, ", Pop(element));
printf("Items left in the stack %d \n", Gettop(element));
} while (Isempty(element) == 0);
return 0;
}
Result: https://i.stack.imgur.com/wLczr.png
I create a stack, after which I start entering numbers into it. With that, I'm fine. Next, I find the top element of the stack and pop it out. After that, I need to remove those numbers from the stack, the value of which is less than 10, and I manage to completely clear the stack one by one. Can't make a condition for this.
One approach would be to use another stack as temporary storage. In pseudo-code something like:
* create tmp-stack
* while org-stack isn't empty
* data = pop org-stack
* if (data >= 10) push data to tmp-stack
* while tmp_stack isn't empty
* data = pop tmp_stack
* push data to org-stack
* free tmp-stack
This can be implemented using the already existing function.
Better performance can be achieved by operating directly on the array holding the stacks data. Based on ideas from #SergeBallesta it may look like:
* write-index = 0;
* read-index = 0;
* while read-index < top
* if array[read-index] >= 10
* array[write-index] = array[read-index]
* write-index = write-index + 1
* read-index = read-index + 1
* top = write-index
It seems you are using an old MS compiler. For example the header <malloc.h> is not a standard C header. Instead you should use the header <stdlib.h>. Also neither declaration from the header <math.h> is used. So you may remove the inclusion of the header.
The function Gettop does not do what is written in the comment for the function
// top element of the stack without delting
Actually it returns the current value of the data member st->top that is how many elements are present in the stack.
On the other hand, the comment for the function Vulychtop
// deleting the top of the stack
is incorrect. The function does not delete an element from the stack because the value of the data member st->top is not decreased. Also such a function should not output any message. It is the function Pop that removes an element from the stack.
The function Vulychtop could be defined the following way
int Vulychtop( struct kkk *st, float *value )
{
if ( st->top != 0 ) *value = st->elem[st->top - 1];
return st->top != 0;
}
The function Pop also should not issue any message and return an element of the stack.
Also there is no great sense to declare the pointer element in the file scope. It could be declared in main.
The pointer st declared in the file scope
struct kkk* st, * element; // pointers
^^
is not used in the program.
Also there is no need to allocate an object of the type struct kkk dynamically.
You could just write in main
struct kkk element;
Init( &element );
To remove elements that are less than 10 from the original stack using the open interface of the stack you need an auxiliary stack.
For example
struct kkk st;
Init( &st );
while( !Isempty( element ) )
{
float value;
Vulychtop( element, &value ); // here I am using the function definition I showed above
if ( !( value < 10.0f ) ) Push( &st, value );
Pop( element );
}
while( !Isempty( &st ) )
{
float value;
Vulychtop( &st, &value ); // here I am using the function definition I showed above
Push( element, value );
Pop( &st );
}
You can yourself add messages in the while loops about what elements are popped, pushed or removed.
Pay attention that as you allocated an object of the type struct kkk dynamically you should free it before exiting the program
free( element );
I am trying to do a level order traversal of a binary search tree and return the final result in a multi-dimensional array. eg. if root node is 2 and nodes at level 1 are 1,4 then it should return [[2], [1,4]] as returnColumnSizes from the code. I am new in data structures and don't have command in using malloc function as well. Any help will be appreciated. Thanks :)
int height(struct TreeNode *h){
if (h == NULL) return 0;
int l = height(h->left);
int r = height(h->right);
if (l > r)
return l+1;
else
return r+1;
}
int *ordercalc(struct TreeNode *root, int level){
if (root == NULL) return NULL;
int i = 0, arr[100];
if (level == 1)
arr[i++] = root->val;
else if(level > 1){
ordercalc(root->left, level-1); //decrease the level one per call to print data when it
ordercalc(root->right, level-1); // reaches proper level
}
return arr;
}
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes){
if (*returnSize == 0) return NULL;
**returnColumnSizes = (int **)malloc(*returnSize * sizeof(int *));
for (int i=0;i<height(root)+1;i++){
returnColumnSizes[i] = (int *)malloc(sizeof(int) * 10);
returnColumnSizes[i] = ordercalc(root, i);
}
return returnColumnSizes;
}
height looks good.
levelOrder looks okay, although i<height(root)+1; computes the height of root again and again in the loop even though it doesn't change. Also, malloc(sizeof(int) * 10); doesn't seem sufficiently dynamic for large trees (we'll come back to this later).
ordercalc needs to be re-considered. The function has arr[100]; allocated on its stack frame, then
if (level == 1)
arr[i++] = root->val;
and
return arr;
I can see you're trying to fill the levels based on the height, which is the right idea. However:
Returning a stack-allocated array is undefined behavior. When the call returns, all data on its frame cannot be accessed. You'll need to malloc on the heap and return a pointer if you want to do this.
arr[i++] = root->val; puts the root at arr[0] but nothing else ever happens with this array, so the intent is unclear.
Hardcoding 100 for the array size seems a mistake. Surely there is some tree with a level large enough to overflow this buffer, assuming you intend to fill it up beyond the root. When you do switch to malloc, you'll probably need to plan to realloc.
Instead of returning results from this function, it seems that passing in a pointer to the pre-allocated result structure is simplest.
A way to simplify the reallocation and size management is to approach the problem with multiple passes. Here's the game plan:
Get the tree height.
Allocate the result and column size arrays to match tree height.
Perform a second traversal and set the result column lengths for each level.
Allocate each level to match the size determined in step 3.
Perform a final pass to populate the pre-allocated result levels.
Here's the code:
int height(struct TreeNode *root) {
if (root) {
int left = height(root->left);
int right = height(root->right);
return 1 + (left > right ? left : right);
}
return 0;
}
void set_col_lens(struct TreeNode *root, int *res_col_lens, int depth) {
if (root) {
res_col_lens[depth]++;
set_col_lens(root->left, res_col_lens, depth + 1);
set_col_lens(root->right, res_col_lens, depth + 1);
}
}
void fill_levels(struct TreeNode *root, int **res, int *last_items, int level) {
if (root) {
int last_item = last_items[level]++;
res[level][last_item] = root->val;
fill_levels(root->left, res, last_items, level + 1);
fill_levels(root->right, res, last_items, level + 1);
}
}
int **level_order(struct TreeNode *root, int *res_len, int **res_col_lens) {
if (!root) {
*res_len = 0;
return NULL;
}
*res_len = height(root);
int **res = malloc(sizeof(*res) * (*res_len));
*res_col_lens = calloc(*res_len, sizeof(**res_col_lens));
int *last_items = calloc(*res_len, sizeof(*last_items));
set_col_lens(root, *res_col_lens, 0);
for (int i = 0; i < *res_len; i++) {
res[i] = malloc(sizeof((*res)[i]) * (*res_col_lens)[i]);
}
fill_levels(root, res, last_items, 0);
free(last_items);
return res;
}
One benefit of this is that the problem is broken into simple, distinct steps.
Another approach which I think is more natural is to use a queue and perform a breadth-first traversal in one stack frame. The problem then becomes writing a queue abstraction in C, which is not difficult but does take a bit of fussing.
I'm wanting to pass a local variable within a function, back through it's pointer parameter (not returned).
My assignment uses a stack data structure, and one criteria that must be used is the Pop() function must have a pointer parameter that is used to return the top-most item on the stack. I have used this before. My program became more complex with a data struct, I started getting either segmentation faults, or the data not being saved after the function's frame popped.
// Definitions
typedef char * string;
typedef enum { SUCCESS, FAIL } result;
typedef enum { INTEGER, DOUBLE, STRING } item_tag;
// Result Check
static result RESULT;
// Item_Tag
typedef struct {
item_tag tag;
union {
int i;
double d;
string s;
} value;
} item;
// Declarations
int STACK_SIZE = 0;
const int MAX_STACK_SIZE = 1024; // Maximum stack size
item stack[1024];
// Pop
result Pop(item *ip){
item poppedItem;
item * pointerReturn = malloc(sizeof(item));
// Check stack size is not 0
if(STACK_SIZE == 0){
return FAIL;
}
// If stack size is only 1, creates a blank stack
else if(STACK_SIZE == 1){
item emptyItem;
// Initialize
emptyItem.tag = INTEGER;
emptyItem.value.i = 0;
// Check top item's tag
poppedItem = stack[0];
// Store top item data based on tag
switch(stack[0].tag){
case STRING:
poppedItem.value.s = stack[0].value.s;
case DOUBLE:
poppedItem.value.d = stack[0].value.d;
default:
poppedItem.value.i = stack[0].value.i;
}
poppedItem.tag = stack[0].tag;
// Allocate memory for parameter, and have it point to poppedItem
ip = malloc(sizeof(poppedItem));
*ip = poppedItem;
// Store empty stack to top of stack
stack[0] = emptyItem;
// Decrease stack size
STACK_SIZE--;
}
// Grab top Item from stack
else{
// Check top item's tag
poppedItem = stack[0];
// Store top item data based on tag
switch(stack[0].tag){
case STRING:
poppedItem.value.s = stack[0].value.s;
case DOUBLE:
poppedItem.value.d = stack[0].value.d;
default:
poppedItem.value.i = stack[0].value.i;
}
poppedItem.tag = stack[0].tag;
// Allocate memory for parameter, and have it point to poppedItem
ip = malloc(sizeof(poppedItem));
*ip = poppedItem;
// Reshuffle Items in Stack
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
My knowledge with pointers is alright, and memory location/management. But I can't claim to be an expert by any means. I don't exactly know what happens in the background when you're using the function's own pointer parameter as a means of passing data back.
What is the correct syntax to solve this problem?
How can a parameter pass something back?
Thanks in advance!
EDIT*
Since many people are confused. I'll post some snippets. This is an assignment, so I cannot simply post all of it online as that'd be inappropriate. But I think it's okay to post the function itself and have people analyze it. I'm aware it's a bit messy atm since I've edited it several dozen times to try and figure out the solution. Sorry for the confusion. Keep in mind that not all the code is there. just the function in question, and some of the structure.
The function should receive a pointer to a valid object:
item catcher;
myFunc(&catcher); // Pass a pointer to catcher
and the function should modify the object it received a pointer to:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
Update:
You're overcomplicating things immensely – there should be no mallocs when popping, and you're leaking memory all over the place.
(Your knowledge of pointers and memory management is far from "alright". It looks more like a novice's guesswork than knowledge.)
It should be something more like this:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[0];
for(int idx = 0; idx < STACK_SIZE; idx++){
stack[idx] = stack[idx + 1];
}
STACK_SIZE--;
}
return SUCCESS;
}
but it's better to push/pop at the far end of the array:
result Pop(item *ip){
if (STACK_SIZE == 0){
return FAIL;
}
else {
*ip = stack[STACK_SIZE-1];
STACK_SIZE--;
}
return SUCCESS;
}
Response to the originally posted code:
typedef struct{
variables
}item;
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
int main(){
item * catcher;
myFunc(catcher);
printf("%s\n", catcher.variable);
}
A few issues.
Your program will not compile. variable has to have a type.
void myFunc(item *itemPointer){
item newItem;
newItem.variable = stuff;
}
stuff is not defined; item *itemPointer is not used.
item * catcher pointer has to point to allocated memory. It is not initialized.
Pass arguments via pointers and modify member of the structure like this:
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
Solution like:
void myFunc(item *itemPointer)
{
itemPointer->variable = stuff;
// or
*itemPointer = someItem;
}
is possible, but it assumes that stuff or someItem is a global variable which is not the best programming practice IMO.
Retrieve value from pointer via -> not . operator.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char * variable;
}item;
void myFunc(item *itemPointer, const char *string){
itemPointer->variable = string ;
}
int main(){
item * catcher;
char *new_string = "new string";
catcher = malloc(sizeof(item));
myFunc(catcher, new_string);
printf("%s\n", catcher->variable);
free(catcher);
return 0;
}
OUTPUT:
new string
I want to pass a table from Lua to C and then access all the values in C without copying the values from Lua address space to C stack. Is there a way of doing this?? I want to minimize the copy of values.
I tried using gettable() but in that case, the value is copied onto top of stack. So a copy is getting generated. I don't want this. Is there any other way??
Here is my C code:-
#include <lua.h> /* Always include this */
#include <lauxlib.h> /* Always include this */
#include <lualib.h> /* Always include this */
#include <malloc.h>
#define EXCEPTION_IS_NUMBER (-2) //Passed a custom error no. to be returned in
//case of error
#define SUCCESS (0)
static int iquicksort(lua_State *L) {
int k,len=0;
len=lua_tointeger(L,-2); //-2 specifies second element from top of stack.
//So I have passed 2 elements from Lua to C, first
//is size of table and second table. So when they
//are pushed to stack, the size is second element
//from top.So here I am storing it in variable len.
int *q;
int *p=(int *)malloc(len*sizeof(int));
q=p;
for(k=1;k<=len;k++)
{
lua_pushinteger(L,k); //if I want to access a[2], where a is my table
//and 2 is the index, then '2' needs to be at top
//of the stack and I need to pass the location of
//'a' in stack as second argument to gettable().
//So here Address of table was at top, I pushed
//the index on top, now address is second element
//from top. So I passed it as '-2' in gettable
//below. What gettable() does is that it fetches
//and copies that value at stack top. So I can
//use it from there.
lua_gettable(L,-2);
if(lua_isnumber(L,-1)) //Checking top value replaced by fxn is number...
{
*p++=lua_tointeger(L,-1); //Storing the values in array
}
else
{
lua_pushinteger(L,EXCEPTION_IS_NUMBER);
return 1;
}
lua_pop(L,1);
}
p=q;
sort(p,0,len-1);
for(k=1;k<=len;k++) //This fxn changes the value at prescribed location of table.
//here I am changing the values at Table's location...
//i.e. storing the sorted values in table.....
{
lua_pushinteger(L,k);
lua_pushinteger(L,*p++);
lua_settable(L,-3);
}
lua_pushinteger(L,SUCCESS);
return 1;
}
//Simple quicksort of values.....
void sort(int *arr, int left,int right){
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
sort(arr, left, j);
if (i < right)
sort(arr, i, right);
}
int luaopen_power(lua_State *L){
lua_register(L,"quicksort",iquicksort);
return 0;
}
I generated a shared library by compiling this program using the following command:-
gcc -Wall -shared -fPIC -o power.so -I/usr/local/include/lua5.1 -llua5.1 quicksort.c
Here is the Lua code for calling this:-
require("power")
x={5,4,6,5,3,2,3,9}
print("Before quicksort call....")
t=quicksort(#x,x)
if t==0 then
for i,v in ipairs(x) do print(i,v) end
else
print(string.format("%s %d","Error occurred. Errorcode is:: ",t))
end
Thanks
The C API for Lua only copies low-level C types, such as numbers and booleans. For all others, including strings, it uses pointers to internal Lua data.
I'm not sure that gettable() copies the value to the Lua stack, I think it copies a reference or pointer of the value... (especially when that value is a table itself, that table 's content is not copied).
And given that Lua may do magic processing, I believe your answer is no.
Lua being free software you can download it and look in the source code. With lua-5.2.0-rc4 the lua_gettable function is in file src/lapi.c
LUA_API void lua_gettable (lua_State *L, int idx) {
StkId t;
lua_lock(L);
t = index2addr(L, idx);
api_checkvalidindex(L, t);
luaV_gettable(L, t, L->top - 1, L->top - 1);
lua_unlock(L);
}
so the actual work is done by luaV_gettable from file src/lvm.c which is
void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {
int loop;
for (loop = 0; loop < MAXTAGLOOP; loop++) {
const TValue *tm;
if (ttistable(t)) { /* `t' is a table? */
Table *h = hvalue(t);
const TValue *res = luaH_get(h, key); /* do a primitive get */
if (!ttisnil(res) || /* result is not nil? */
(tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */
setobj2s(L, val, res);
return;
}
/* else will try the tag method */
}
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))
luaG_typeerror(L, t, "index");
if (ttisfunction(tm)) {
callTM(L, tm, t, key, val, 1);
return;
}
t = tm; /* else repeat with 'tm' */
}
luaG_runerror(L, "loop in gettable");
}
so I think the answer is no. However, you could patch or enhance the code. I don't understand why the question bothers you. Only simple data is copied (very quickly), unless magic occurs (and the magic i.e. metatable is an essential part of Lua semantics); aggregate data content is not copied.
I've implemented a stack in C, using a stackADT struct and a set of functions:
#ifndef _stack_h
#define _stack_h
// Macros
#define MaxStackSize 100
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// The type of element that may
// be stored in the stack
typedef int stackElementT;
// The stackADT represents the abstract type used to store
// the elements that have been pushed
typedef struct stackCDT
{
// A pointer to an array of elements
stackElementT* elements;
// Number of elements on the stack
int count;
// Number of elements we can push onto
// the stack before having to resize
int size;
}* stackADT;
// This function allocates and returns a new stack, which is
// initially empty... AKA - The Constructor
stackADT NewStack(void)
{
// The stack to return
stackADT stack;
// Instanitate a new stack
stack = (stackCDT*)(malloc(sizeof(stackCDT)));
// Start with 0 elements of course
stack->count = 0;
// Allocate memory for 50 integers
stack->elements = (stackElementT*)(malloc(50*sizeof(stackElementT)));
// Establish the size of the stack
stack->size = 50;
return stack;
}
/********* GETTER FUNCTIONS *********/
// Returns the number of elements currently pushed
// onto the stack
int StackDepth(stackADT stack)
{
return (stack->count);
}
// This function returns the element a the specified index in
// the stack, where the top is defined as index 0
stackElementT GetStackElement(stackADT stack, int index);
// Function to print contents of stack
void PrintStack(stackADT stack)
{
int i = 0;
printf("count = %d\nsize = %d\n",stack->count,stack->size);
for(i = (stack->count - 1); i >= 0; i--)
{
if((i%10 == 0) && (i != 0))
printf("\n");
printf("%d\t",*(stack->elements + i));
}
}
// Functions to determine if stack is empty or full
int StackIsEmpty(stackADT stack)
{
if(stack->count == 0)
return 1;
else
return 0;
}
int StackIsFull(stackADT stack)
{
if(stack->count == stack->size)
return 1;
else
return 0;
}
// This function pushes the specified element onto the stack
void Push(stackADT stack, stackElementT element)
{
// A temporary array that we may use later on
stackElementT* temp = NULL;
int oldCount = stack->count;
int i = 0;
// If the stack if full we need to do a
// a transfer, resize, and retransfer, then push
if(StackIsFull(stack))
{
// temp will be the same size as the old stack
temp = (stackElementT*)(malloc((oldCount)*sizeof(stackElementT)));
// Now we perform the transfer
for(i = 0; i < oldCount; i++)
{
*(temp + i) = *((stack->elements) + i);
}
// Free the old memory
free(stack->elements);
stack->elements = NULL;
// Recreate the stack with a 50% increase in size/capacity
stack->elements = (stackElementT*)(malloc((3*oldCount/2)*sizeof(stackElementT)));
// Re-establish the size
stack->size = 3*oldCount/2;
// Now we perform the transfer back
for(i = 0; i < oldCount; i++)
{
*((stack->elements) + i) = *(temp + i);
}
// Free the temp array and
// remove dangling pointer
free(temp);
temp = NULL;
// Now we push the element onto the stack
*((stack->elements) + oldCount) = element;
// Increase the count
stack->count = oldCount + 1;
}
// If the stack isn't full
else
{
*((stack->elements) + oldCount) = element;
stack->count = oldCount + 1;
}
}
// This function pops the top element from the stack and returns
// that value
stackElementT Pop(stackADT stack);
// This function frees the storage associated with the stack
void FreeStack(stackADT stack)
{
// Start by freeing the elements on the stack
// and remove dangling pointers
free(stack->elements);
stack->elements = NULL;
// Finally free the stack
free(stack);
stack = NULL;
}
#endif
Obviously I'm not completely finished (needs a pop function). My concern is with the bottom function (FreeStack). I tested the code below as such:
#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
int main(void)
{
stackADT stack;
int i = 0;
stack = NewStack();
PrintStack(stack);
for(i = 0; i < 60; i++)
{
Push(stack,i);
}
PrintStack(stack);
FreeStack(stack);
_CrtDumpMemoryLeaks();
return 0;
}
The _CrtDumpMemoryLeaks() function is for Visual Studio and it indicates if there is a memory leak. Apparently I've sealed off any leaks when calling the FreeStack(stackADT stack) function. However, the stack pointer still holds a memory address, which is the issue because the FreeStack function is supposed to free the memory pointed to by the stack variable and set it equal to NULL. This occurs within the function, but when I return to the main function during debugging, I see the memory address still there. What is it I'm missing here? If I'm able to release the memory, why can't I remove the dangling pointer?
You pass the stack to the function by value, instead of by address, modify the function to receive (stackADT *) and you'll be good to go.
Clarification: as Christian commented, the function call, and the use of stack will have to be changed as well of course (since now it's a pointer to pointer...)
You're passing that stackADT object (pointer) by value in your pop method:
void FreeStack(stackADT stack)
So stack refers to the local copy of that pointer. When you set that pointer = NULL, you modify it only within FreeStack. The main method has its own copy of said pointer, not pointing to NULL.