Witam,
Mam program w klasycznym C, sprawdzony pod linuksem -pedantic -Wall. Mianowicie valgrind wyrzuca mi wyciek.
Stack.c
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "stack.h"
void init(struct stack_handle* s)
{
s->top=0;
s->wsk=(int*)malloc(sizeof(int));
}
void finalize(struct stack_handle* s)
{
}
void clear(struct stack_handle* s)
{
s->top=0;
s->wsk=(int*)malloc(sizeof(int));
}
void push(struct stack_handle* s,int a)
{
s->wsk=realloc(s->wsk,(s->top+1)*sizeof(int));
s->wsk[s->top]=a;
s->top++;
}
int pop(struct stack_handle* s)
{
assert(s->top>0);
s->top=s->top-1;
return s->wsk[s->top];
}
Stack.h
struct stack_handle
{
int top;
int *wsk;
};
void push(struct stack_handle* s,int a);
int pop(struct stack_handle* s);
void clear(struct stack_handle* s);
void init(struct stack_handle* s);
void finalize(struct stack_handle* s);
Program testujący:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main()
{
struct stack_handle s1;
init(&s1);
printf("%d\n",s1.top);
push(&s1,1);
push(&s1,12);
pop(&s1);
pop(&s1);
finalize(&s1);
return 0;
}
Byłbym zobowiązany, gdzieś czegoś nie zwalniam, ale nie mogę wpaść na pomysł gdzie.