room for null character

This is a discussion on room for null character within the C++ Programming forums, part of the General Programming Boards category; The char array in the following can take in two values as is shown in the OUTPUT. There were three ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    room for null character

    The char array in the following can take in two values as is shown in the OUTPUT. There were three inputs but it only accepted two. But shouldn't it only accept one character or value because it also needs room to accommodate null character? Please guide me. Thanks.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
            const int C = 2;
            char array[C];
    
            cout << "enter values for the array: ";
            cin >> array;
    
            for (int i=0; i<C; i++)
            {
                    cout << array[i];
            }
    
            cout << endl;
    
            system("pause");
            return 0;
    }
    OUTPUT
    Code:
    enter values for the array: abc
    ab
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by jackson6612
    There were three inputs but it only accepted two. But shouldn't it only accept one character or value because it also needs room to accommodate null character?
    Yes, hence you should use std::setw to ensure this, or perhaps use cin.getline.
    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
    Jan 2010
    Posts
    412
    cin doesn't know the size of 'array', so any input that is to long for the array to hold gets written to whatever happens to be after 'array'
    In your example case I'm guessing that the stack is 4-byte aligned and your input is only 4 bytes long so nothing bad happens. Try inputting a really long string and the application will probably crash from stack corruption.

    Consider the following code:
    Code:
    #include <iostream>
    
    #pragma pack(push, 1) // change data alignment
    struct test
    {
    	test() : foo(12345) {}
    	char str[2];
    	int foo;
    };
    #pragma pack(pop) // restore old alignment
    
    int main(void)
    {
    	test t;
    	std::cout << "t.foo is " << t.foo << std::endl;
    	std::cout << "Enter text for t.str: ";
    	std::cin >> t.str;
    	std::cout << "t.foo is " << t.foo << std::endl;
    	return 0;
    }
    I'm using visual c++ pragmas to make sure the struct is byte-aligned. There are ways to do the same in gcc but I can't remember how exactly.
    But anyways, the output for me was
    Code:
    t.foo is 12345
    Enter text for t.str: abc
    t.foo is 99
    As you can see foo gets overwritten by 99 (ASCII code for 'c') because the input is to long for the array.

    So do what laserlight suggested and limit the length of the input, or use an std::string.

    Edit:
    By the way, when using cin for input; Is there anything in the standard that specifies how long input strings it can handle or is that implementation specific?
    Last edited by _Mike; 06-16-2011 at 11:43 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    I thought you'd already been told never to use cin >> with char arrays? This is almost the most dangerous thing you can do and is a ticking time bomb. Literally.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    By the way, when using cin for input; Is there anything in the standard that specifies how long input strings it can handle or is that implementation specific?
    The std::string member that will tell you how big strings can be is max_size().

    If you mean character arrays, well there are a few ways to answer the question. One way to answer it, would be to remind you that operator>> is white space delimited, meaning that strings read by operator>> "will.be.as.long.as.it.can.go.before" negative space appears. Another way of answering is saying stuff like, well, size_t does have a limit (a maximum number) and strings longer than that are immense. You have to do all sorts of annoying stuff just to reference the data.
    Last edited by whiteflags; 06-17-2011 at 05:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to write a null character to a file
    By nicoeschpiko in forum C Programming
    Replies: 6
    Last Post: 03-04-2010, 11:27 PM
  2. question about Null character
    By genie in forum C Programming
    Replies: 4
    Last Post: 11-06-2008, 08:28 PM
  3. null after every character!!?!
    By the bassinvader in forum C Programming
    Replies: 15
    Last Post: 09-09-2006, 07:11 PM
  4. Character Array: If Null
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:06 PM
  5. ASCII code for a NULL character
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 12-09-2001, 04:40 AM

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