Cuando corro sólo el fragmento de códigoTamaño de la estructura con un char, un doble, un int y al
int *t;
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(double) << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(t) << std::endl;
me da un resultado como este:
1
8
4
4
Total: 17.
Pero cuando pruebo sizeof struct que contiene estos tipos de datos me da 24, y estoy confundido. ¿Cuáles son los 7 bytes adicionales?
Este es el código
#include <iostream>
#include <stdio.h>
struct struct_type{
int i;
char ch;
int *p;
double d;
} s;
int main(){
int *t;
//std::cout << sizeof(char) <<std::endl;
//std::cout << sizeof(double) <<std::endl;
//std::cout << sizeof(int) <<std::endl;
//std::cout << sizeof(t) <<std::endl;
printf("s_type is %d byes long",sizeof(struct struct_type));
return 0;
}
: editar
He actualizado mi código como este
#include <iostream>
#include <stdio.h>
struct struct_type{
double d_attribute;
int i__attribute__(int(packed));
int * p__attribute_(int(packed));;
char ch;
} s;
int main(){
int *t;
//std::cout<<sizeof(char)<<std::endl;
//std::cout<<sizeof(double)<<std::endl;
//std::cout<<sizeof(int)<<std::endl;
//std::cout<<sizeof(t)<<std::endl;
printf("s_type is %d bytes long",sizeof(s));
return 0;
}
y ahora me muestra 16 bytes. ¿Es bueno o he perdido algunos bytes importantes?
Esto es C++, no C. – habnabit