![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 9
| Memory/Pointer allocation, simple question -------------------------------------- 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!!! |
| lesodk is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| 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.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Last edited by laserlight; 05-17-2009 at 07:36 AM. |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: May 2009
Posts: 9
| Thanks alot! |
| lesodk is offline | |
| | #4 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| 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 |
| cpjust is offline | |
![]() |
| Tags |
| allocation, array, c++, pointer, simple |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple class question | 99atlantic | C++ Programming | 6 | 04-20-2005 11:41 PM |
| Simple question about pausing program | Noid | C Programming | 14 | 04-02-2005 09:46 AM |
| simple question. | InvariantLoop | Windows Programming | 4 | 01-31-2005 12:15 PM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| dynamic allocation question | vale | C++ Programming | 1 | 08-26-2001 04:23 PM |