Thread: where is the problem

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    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

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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>

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM