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!
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..
– Maniero
try using a debugger, maybe solve your problem, try using the GDB
– Brumazzi DB
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?– gato