-
where is the problem
Code:
#include<iostream>
#include<conio>
#include<stdlib>
#define MAX 30
struct node{
int age; // 4 bytes
char name[MAX]; // 30 bytes
node *next; // 4 bytes (all pointers are the same size an ints)
}; // this is a 32bit prog so 4 bytes 4 + 4 + 30 = 38bytes.
int main()
{
int amount;
cout<<"How many people to be stored >";
cin>>amount; // If I input 1 here...
if(amount==0)
abort();
else
{
cout<<sizeof(node); // Why does this say 40.
}
getch();
return 0;
}
// also how do I access the command line using turbo c++ 3.0 on win 95 platform
// Thank you for your guidence
-
Code:
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
...
But it looks like thats an ancient compiler so you might have to use the old headers, argh:
Code:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
-
>>cout<<sizeof(node); // Why does this say 40.
Because your compiler aligns the struct members on 1-word boundaries. Try moving the char declaration to after node *next and see what happens.
-
Normally pointers are aligned on a dword boundary. 30+4 = 34, which is not divisable by 4, so two is added to make it divisable. 36+4 = 40. In this case, by making "name" a 32 byte array, it'll still (well, should) give the output of 40, as the pointer will then be aligned.
Also, never assume ints are the same size as pointers - they are on your compiler, but a different compiler may decide to use 2 bytes as an int.