sizeof question

This is a discussion on sizeof question within the C++ Programming forums, part of the General Programming Boards category; Code: char *f; f = "123456789"; cout << "size of f is " << sizeof(f) << endl; Why is this ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26

    sizeof question

    Code:
    char *f;
    f = "123456789";
    cout << "size of f is " << sizeof(f) << endl;
    Why is this 4? Everytime I compile it, its 4.
    No matter what I put the value of f to be, it says 4.

    I would really like to know how I get the size of a char array like this, any reply appreciated.

    Dag
    life is too short smart but hard !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,745
    Why is this 4?
    Probably because sizeof(int) on your system is 4.

    I would really like to know how I get the size of a char array like this
    Use strlen() from <cstring> ?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    Use strlen() from <cstring> ?
    Thanks you!, that is just what I wanted
    life is too short smart but hard !

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Test this
    Code:
                    char first[]="123456789";
    	char* second = "123456789";
    	cout<<sizeof(first)<<endl;
    	cout<<sizeof(second);
    And see what you can come up with!

    Because you're having 32-bit machine that means that every address is 4 byte wide ant that means sizeof(void*) is equal like sizeof(char*) i.e. 4 bytes.
    And see what is the difference between first and second.

    Think about it a little.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by laserlight
    Probably because sizeof(int) on your system is 4.
    Actually, it's because the sizeof the pointer is 4 bytes.
    Code:
    char *f;
    f = "123456789";
    cout << "size of f is " << sizeof(f) << endl;
    Is that legal? First of all, f is uninitalized. You'll also need some memory to hold your string. Second, I think you really want strcpy.
    Code:
    char *f;
    f = new char[20];
    strcpy(f,"123456789");
    cout << "size of f is " << sizeof(f) << endl;
    cout << f << endl;
    delete[] f;
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by pianorain
    Code:
    char *f;
    f = "123456789";
    cout << "size of f is " << sizeof(f) << endl;
    Is that legal? First of all, f is uninitalized.
    Sure it's initialized, it's initialized to the address of the string-literal. It should however probably be a const char*.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,745
    Actually, it's because the sizeof the pointer is 4 bytes.
    hmm... I had the impression that the sizeof of any pointer would be the same as sizeof of int, since a pointer is an integer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>since a pointer is an integer.
    Not sure about this one, it can be interpreted as an integral type but I seem to remember hearing somewhere that it is NOT an integer.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >>but I seem to remember hearing somewhere that it is NOT an integer.

    Like on a 64-bit machine where pointers are... 64 bits. ints and pointers are not gaurenteed to be the same size.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    char *f;
    f = "123456789";
    cout << "size of f is " << sizeof(f) << endl;

    Why is this 4? Everytime I compile it, its 4.
    ...because pointers like f store addresses. "1234567890" is a 'string literal' and it is placed in memory somewhere, and the following assignment:

    f = "123456789";

    assigns the address of the string literal to f. An address on your system is 4 bytes long, and no matter how long the string is, it's address in memory--which is the address of the first character of the string--will always be 4 bytes long.
    Last edited by 7stud; 03-11-2005 at 01:18 AM.

  11. #11
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    wow, lots of replies!
    I would like to thank all who explained this sizeof thing also! I think I understand it a bit more now...!
    greatly appreciated!

    Dag
    life is too short smart but hard !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. sizeof() EZ question
    By V1P3R V3N0M in forum Windows Programming
    Replies: 15
    Last Post: 01-11-2003, 10:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21