C - wyciek w programie / stos

0

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.

0

hmm... czyżby zabrakło instrukcji free() ?

0

void finalize(struct stack_handle* s)
{
free(s->wsk);
}

dopisałem takie coś, ale brakuje mi linuksa by przetestować, bo tutaj mam tylko visuala. Było by super gdyby ktoś mógł sprawdzić czy pójdzie bez błędów

gcc -pedantic -Wall
valgrind ./a.out

0

Aj już mam sprawdzone. Dzięki serdecznie za pomoc. Oczywiście tyle zwolnień ile powinno być. Pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1