Space spent by variables

0

Hello, I would like to know how I can calculate the amount of space spent per code variable, function.

Example I have a code to sort a list doubly chained by SelectSort, however I want to calculate the space spent and I have no idea I thought of using sizeof (int), in each function to add the size but did not work? Someone would have a link or something that would help.

Example: I have to calculate the space spent by the variables and function of this algorithm

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

typedef struct cel{
    int conteudo;
    struct cel *seg;
    struct cel *ant;
}celula;


celula *inserirLista(celula *p, int x);
void insertionSort(celula *lst);
void Imprime(celula *lst);

int main(){
    /* inicio variaveis*/
    int i, n;
    celula c;
    celula *lst;
    celula *p;

    clock_t start, end;
    double cpu_time_used;

    /* fim  variaveis*/


    lst = &c;
    lst->seg = NULL;
    lst->ant = NULL;
    p=lst;
    srand(time(NULL));
    n = 100000;

    for(i = 1; i <=n; i++)
        p = inserirLista(lst, rand()%1000);

    Imprime(lst);
    printf("\n\n");
    start = clock();
    insertionSort(lst);
    end = clock();
    cpu_time_used = ((double)(end - start))/CLOCKS_PER_SEC;
    printf("\n\n Tempo: %.7f ", cpu_time_used);


    printf("\n\n");

    Imprime(lst);


}



void insertionSort(celula *lst){

    celula *p = lst;
    celula * q;

    lst = lst->seg->seg;

    while(lst != NULL){

        q = p;

        while(q->seg != lst){

            if (q->conteudo > lst->conteudo){

                int temp = lst->conteudo;
                lst->conteudo = q->conteudo;
                q->conteudo = temp;

            }else{

                q = q->seg;

            }

        }

        lst = lst->seg;

    }

}
celula *inserirLista(celula *p, int x){
    celula *nova;
    nova=(celula*)malloc(sizeof(celula));
    nova->conteudo = x;
    nova->seg=p->seg;
    nova->ant=p;

    p->seg =nova;
    return p;
}
void Imprime(celula *lst){
    celula * p = lst->seg;
    while (p->seg != NULL){
        printf("%d ", p->conteudo);
        p=p->seg;
    }
}

Thank you!

    
asked by anonymous 25.06.2016 / 20:47

1 answer

1

Well, if it's a double-chained list, then you should have something like this:

typedef struct NO {
    struct NO *prox;
    struct NO *anterior;
    int dados;
} NO;

And let's suppose you have this too:

 int numero_elementos_lista(NO *primeiro) {
     int x = 0;
     NO *no;
     for (no = primeiro; no != NULL; no = no->prox) {
         x++;
     }
     return x;
 }

Therefore, the memory used will be:

 int tamanho_em_bytes_da_lista(NO *primeiro) {
     return sizeof(NO) * numero_elementos_lista(primeiro);
 }
    
25.06.2016 / 22:05