Space spent by variables

Asked

Viewed 90 times

0

Hello I would like to know how I do to calculate the space spent by each code variables, functions.

Example I have a code to order a list double chained by Selectsort, but I want to calculate the space spent and I have no idea to use sizeof(int), in each function to add the size but did not work? Someone would have a link or something to 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!

  • 1

    Here we give answers and no links to other places, but to answer we need more details of what you are doing, what problem you had doing what you mentioned, what the purpose of getting this information, etc..

  • try using a debugger, maybe solve your problem, try using the GDB

  • I don’t quite understand what you want, the sizeof() returns the amount in bytes that occupies its variable, as you want to do this calculation?

1 answer

1


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

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

And let’s assume you have that 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);
 }
  • thanks I understood I think it’s like this!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.