Thread: Memory/Pointer allocation, simple question

  1. #1
    Registered User
    Join Date
    May 2009

    Memory/Pointer allocation, simple question

    If i run this code
    #include <iostream>
    using namespace std;
    	char string1[5];
    	char string2[6];
    	char string3[7];
    	char string4[8];
    int main (){
    	cout<< "\n 1st text (length 5)";
    	cin>> string1;
    	cout<< "\n 2nd text (length 6)";
    	cin>> string2;
    	cout<< "\n 3rd text (length 7)";
    	cin>> string3;
    	cout<< "\n 4th text (length 8)";
    	cin>> string4;
    	cout << "\n 1st : "<< string1;
    	cout << "\n 2nd : "<< string2;
    	cout << "\n 3rd : "<< string3;
    	cout << "\n 4th : "<< string4;
    	return 0;}
    and enter

    as inputs, i get

    1st : PalinMcCainMcCain
    2nd : McCainMcCain
    3rd : McCain
    4th : BarackObama

    as output. Can anyone explain to me what is actually happening?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You appear to be trying to use null terminated strings, but you forgot about the null character.

    What is probably happening is that the attempt to print string1 results in the null character only being found at the end of string3 (since the name "McCain" is only 6 characters leaving one slot for the null character), with the char arrays being adjacent in memory.

    Oh, and I believe that the null characters are actually stored, but it is just that they are being written out of bounds and later overwritten. The last input is more blatant, where non-null characters are written out of bounds as well. A solution to this is to use std::setw to ensure that what is read does not exceed the given storage. A possibly better solution is to just use std::string.
    Last edited by laserlight; 05-17-2009 at 07:36 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Thanks alot!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    i.e. Your arrays are too small to hold the strings you are typing in, causing buffer overflows.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM

Tags for this Thread