Memory/Pointer allocation, simple question

This is a discussion on Memory/Pointer allocation, simple question within the C++ Programming forums, part of the General Programming Boards category; If i run this code -------------------------------------- Code: #include <iostream> using namespace std; char string1[5]; char string2[6]; char string3[7]; char string4[8]; ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Memory/Pointer allocation, simple question

    If i run this code
    --------------------------------------
    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
    Palin
    McCain
    McCain
    BarackObama

    as inputs, i get

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

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.

    EDIT:
    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.
    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
    May 2009
    Posts
    9
    Thanks alot!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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, 08:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    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


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