C++ Why does my code give the desired results on Ubuntu but has undefined behavior on Windows 7 -
i taking c++ course in university, every week required write program given specifications. it's basic tasks resolve around topics discussed in class week before. discussing pointers arrays , dynamic memory naturally week's program using arrays , dynamic memory allocation.
to break down, program asked write supposed take random user input (no white spaces, random characters) , divide user input in 3 categories: upper case, lower case , special characters.
the method used not efficient 1 job, considering libraries allowed use iostream , cmath, here comes problem. in class wrote program in no time, compiled , ran straight away no issues. if give user input such aaghc4 have output ac agh 4
so went ahead , downloaded program university folder on private machine @ home work on little, make more efficient practice. when compiled (unedited , original) .cpp on windows machine it's output random, undefined behavior. both in class , @ home use g++ compile files, @ home run windows 7, in class use ubuntu 14.04.
i greatful if take @ code , tell me why doesn't work on windows machine.
#include<iostream> using namespace std; int main(){ char input[80]; int counter = 0; cin >> input; for(int = 0; input[i] != '\0'; i++){ counter++; } char *uppercase = new char[counter]; char *lowercase = new char[counter]; char *special = new char[counter]; for(int = 0; < (counter+1); i++){ switch(input[i]){ case 'a': lowercase[i] = input[i]; break; case 'b': lowercase[i] = input[i]; break; // edited part goes on entire // alphabet, first lowercase letters uppercase case 'y': uppercase[i] = input[i]; break; case 'z': uppercase[i] = input[i]; break; default: special[i] = input[i]; break; } } for(int = 0; <= counter; i++){ cout << uppercase[i]; } cout << " "; for(int = 0; <= counter; i++){ cout << lowercase[i]; } cout << " "; for(int = 0; <= counter; i++){ cout << special[i]; } delete[] uppercase; delete[] lowercase; delete[] special; cout << endl; }
this entire source code, nothing more , nothing less, thank replies!
you have undefined behavior in both programs, no matter if run on ubuntu or windows 7; lucky shows behavior expected on former.
reading uninitialized memory = undefined behavior
you assuming valid range of elements read in of uppercase
, lowercase
, , special
of [0, counter]
isn't case; reading uninitialized memory in loops iterate on elements of these arrays (and undefined behavior emerge).
the reason think it's working when running code on ubuntu because memory of 3 arrays happens zero-initialized; meaning loops print bunch of '\0'
(normally not visible in terminal) when haven't assigned particular element value.
char * p = new char[counter] ; // default-initialized `char`s (could have value) char * p = new char[counter] (); // value-initialized, elements `char()` ie. 0
reading past end of array = undefined behavior
there's problem loops prints contents of uppercase
, lowercase
, , special
has off-by-one problem.
the last element offset of these loops counter-1
, assume counter
(i <= counter
).
solution
don't print elements haven't, explicitly, given value.
this can accomplished separate counter each of arrays, or other means (like initializing every element of arrays
0
, stop iterating when such value found).don't read elements aren't part of memory allocated.
Comments
Post a Comment