7 Replies - 392 Views - Last Post: 10 June 2012 - 01:25 AM Rate Topic: -----

#1 pablo9891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 19-December 09

Free memory problem in C

Posted 09 June 2012 - 01:20 AM

I've been having some problems trying to free a memory in C for my ADT Stack, the problem is that when i try to free the stack (i want to destroy it) my program seems to colapse, it shows what it has to show but after that it stop's running and i have to cancel the execution. Here you have some code:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TIPODATO char
#define MINELEMENTOS -1

//I create the stack structure

typedef struct {
        TIPODATO *vec;
        int tope;
        int max_elementos;
    }pila;

//Create Stack
pila crear_pila(int max_elementos) {
    pila mi_pila;
    mi_pila.vec = (TIPODATO*)malloc(sizeof(TIPODATO)*max_elementos); //I'm creating an array
    mi_pila.tope = -1;
    mi_pila.max_elementos = max_elementos;

    return mi_pila;
}


//Destroy Stack
void borrar_pila(pila *mi_pila) {
    if(mi_pila == NULL) {
        return;
    }
    free(mi_pila->vec); //when my programm arrives this point it stop's
    mi_pila->vec = NULL; 
    free(mi_pila);
    mi_pila = NULL;
}



    
//This is where i call the function to destroy the Stack

borrar_pila(&mi_pila);


See ya!

Is This A Good Question/Topic? 0
  • +

Replies To: Free memory problem in C

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • Joined: 30-May 10

Re: Free memory problem in C

Posted 09 June 2012 - 01:31 AM

> 08 free(mi_pila);
If you didn't malloc it, don't free it.
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,581
  • Joined: 05-May 12

Re: Free memory problem in C

Posted 09 June 2012 - 01:31 AM

Why are you freeing mi_pila in your destructor, but never allocating it in your constructor?

Salem_c is a faster typer than I am. :-)

This post has been edited by Skydiver: 09 June 2012 - 01:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 pablo9891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 19-December 09

Re: Free memory problem in C

Posted 09 June 2012 - 12:39 PM

Sorry guys but the problem still there. What i have now is the same function for where i create the stack but i changed the deconstructor into the code below, i simply deleted the free for mi_pila, but the problem still there. How would you code the constructor part?, because i believe that there is the probem.

//Delete Stack
void borrar_pila(pila *mi_pila) {
    if(mi_pila == NULL) {
        return;
    }
    free(mi_pila->vec);
    mi_pila->vec = NULL;
}

Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • Joined: 30-May 10

Re: Free memory problem in C

Posted 09 June 2012 - 01:46 PM

If your main is just this
int main ( ) {
   pila myPila = crear_pila( 10 );
   borrar_pila( &myPila );
   return 0;
}


Does it crash, and do you see why you should only free 'vec' in the destructor?

If all this is OK, then the problem is in all the pushing and popping you do in your stack, making a mess of the allocated memory.

Do you change 'vec' at all in any push/pop function? This is a really bad idea if you do.
Was This Post Helpful? 0
  • +
  • -

#6 pablo9891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 19-December 09

Re: Free memory problem in C

Posted 09 June 2012 - 03:32 PM

int main(int argc, const char *argv[]) {

    pila mi_pila = crear_pila(40);

    printf("Hello World \n");

    borrar_pila(&mi_pila);

    return 0;
}


Well according to what Salem_c said i changed my code into the code above, now the program doesn't crash, so perhaps the problem could be in the pop and push functions which i give you below

//VERIFIED IF THE STACK IS FULL
int pila_llena(pila mi_pila) {
    if(mi_pila.tope == mi_pila.max_elementos) {
        return -1;
    }
    else {
        return 0;
    }
}

//VERIFIED IF THE STACK IS EMPTY
int pila_vacia(pila mi_pila) {
    if(mi_pila.tope == MINELEMENTOS) {
        return -1;
    }
    else {
        return 0;
    }
}

//PUSH AN ELEMENT IN THE STACK
int push_pila(pila *mi_pila,char val) {
    if(pila_llena(*mi_pila)) {
        return -1;
    }
    else {
        mi_pila->tope = mi_pila->tope + 1;
        mi_pila->vec[mi_pila->tope] = val;
        return 0;
    }
}

//POP AN ELEMENTO FROM THE STACK
int pop_pila(pila *mi_pila) {
    char val;
    if(pila_vacia(*mi_pila)) {
        return -1;
    }
    else {
        val = mi_pila->vec[mi_pila->tope];
        mi_pila->tope--;
        return val;
    }
}


which error could appear here that doesn't allow me to free the array?
Was This Post Helpful? 0
  • +
  • -

#7 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1575
  • View blog
  • Posts: 2,997
  • Joined: 30-May 10

Re: Free memory problem in C

Posted 09 June 2012 - 11:31 PM

> int pila_llena(pila mi_pila)
> int pila_vacia(pila mi_pila)
It would be a lot more efficient if these two functions accepted a pointer

int pila_llena(pila *mi_pila)
int pila_vacia(pila *mi_pila)

Your check for 'full' is broken.
> mi_pila->tope = mi_pila->tope + 1;
because you then do this and generate an out of bound array access.
Was This Post Helpful? 0
  • +
  • -

#8 pablo9891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 19-December 09

Re: Free memory problem in C

Posted 10 June 2012 - 01:25 AM

The program finally worked, the only thing i changed from it's previous implementation was:

//VERIFIED IF THE STACK IS FULL
int pila_llena(pila mi_pila) {
    if(mi_pila.tope == mi_pila.max_elementos - 1) {
        return -1;
    }
    else {
        return 0;
    }
}


In the third line i'm adding the substraction in order to check if the top of the stack is equal to the maximun quantity of elements - 1. I'm doing this because in C the array start's at 0, i believe that as Salem_c said that was the problem.

After some tests there were no mistakes so i assume that the program is working well. What i would like to know is if there is some suggestion that you could make to the code, about it's efficiency or to make the code cleaner or if you would code it in other way.

Something i'm really insteresting is if there's any possibilitie to expand the stack to other data types, i mean if the user want's to put an structure or floating point on it which are the possibilities.

Finally if you want to see how the complete version, here it is

Interface of the ADT

Spoiler


Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1