Why does this little code consume extra bytes of memory?
In the following program, the structure object nd[0] node consumed 8 bytes of memory. Ideally, nd[0] should consume 5 bytes of memory in total. I mean, 4 bytes for Int type + 1 byte for the char type.
Code:
#include<iostream>
#include<stdio.h>
using namespace std;
struct Node
{
int distance;
char charName;
} nd[] = {5,'b'};
int main()
{
cout << sizeof(nd[0]) << " " << sizeof(nd[0].charName) << " " << sizeof(nd[0].distance) << endl;
return 0;
}
Output : 8 1 4