+ 2

A very strange and rare issue

https://code.sololearn.com/c2dTx4FBloO5/?ref=app I was trying to code the remove duplicate array and I think all is fine now but anyway I have a BIG problem, I cant print nothing and I havent errors. Possibly my code has some mistake which I cant.see. Could somebody try to help me? Thanks

16th Nov 2017, 5:29 AM
Daniel
Daniel - avatar
3 Réponses
+ 4
I'm in a spotty network but quickly, in case someone else can carry the ball: line 21: nlist size is not initialized (edit: "and") on line 5 sizeof==4 and l contains garbage. The function mostly returns false ... try a cout at line 7 to prove it's running against garbage values: cout<<n<<endl; // or l[i] ... same difference ref: http://en.cppreference.com/w/c/language/array_initialization "In C, the braced list of an initializer cannot be empty. C++ allows empty list: int a[3] = {0}; // valid C and C++ way to zero-out a block-scope array int a[3] = {}; // invalid C but valid C++ way to zero-out a block-scope array..." (technical: the uninitialized variable may be causing you to accidentally overwrite the stack, which can drop the runtime)
16th Nov 2017, 5:47 AM
Kirk Schafer
Kirk Schafer - avatar
+ 3
endl placement: for{...) cout << l[i] << ' '; // spaces between values } cout << endl; // end of line when done with loop These lines from other link are 0-inits: int a[3] = {0}; // valid C and C++ way to zero-out a block-scope array int a[3] = {}; // invalid C but valid C++ way to zero-out a block-scope Note: sizeof returns 4 because of int base type. Arrays *could* give you size(): http://www.cplusplus.com/reference/array/array/size/ ...but I hardcoded 15 for sizeof() because you already have the hardcoded init of the other array. [lots of edits to fixup; hopefully this is GOod enough]
16th Nov 2017, 6:51 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Thanks Kirk I saw garbage but 2 questions; 1 - If you understand my code, how must to initialize nlist? 2 - When must I use cout << "Original list " << endl; and when cout << "Original list "; I mean, when must I use endl Thanks
16th Nov 2017, 5:56 AM
Daniel
Daniel - avatar