I compiled a simple c++ program involving char arrays, i am getting serious undefined behaviour, please help, i am having the problem with TDM-GCC-5.1-32BIT, it works fine on 64bit tdm-gcc-5.1. The program output is here, please check it, this also has the 32 and 64 bit compiled programs : https://drive.google.com/folderview?id=1s3-7P3a92ofY92HbU3v_QDUX8orToeMl
32bit output: https://drive.google.com/file/d/1cZraOCLryR2JXwJj5GyJ35xOk1Fl9GMX/view?usp=drivesdk
But is this some error on my part or its a compiler bug, correct my code if there's a mistake on my part.
There some kind of restraint, so that i cant use std::string
Does this bug exist across all 32bit c++ compilers or is it compiler specific?
And what do you mean by broken, is it doing the same blunder as i described?
You declared char sid[2] which allocates 2 character positions which are sid[0] and sid[1]. Therefore references to sid[2], sid[3], sid[4] are out of bounds references.
@AbstractionAnon, so you didnot see the image(the 32bit compiler output https://drive.google.com/file/d/1cZraOCLryR2JXwJj5GyJ35xOk1Fl9GMX/view?usp=drivesdk
Even though i do sid [2],etc i am seeing charecters stored in id,go ahead and correct the code, the problem will still be present
Please checkout the image, and analyze the code output and the code, maybe you will spot what i mean.
I fixed it (I made id and sid both 5 in size) and ran it with 12345 and I get:
12345
msg: 12345
first loop: 1 1
first loop: 2 2
second loop: 3 3
second loop: 4 4
test sid: 3 4
after operation: 12 34
after atoi: 12 34
from struct :12 34
12 34
sh: pause: command not found
is this the expected output (apart from the OS specific command line snafu)?
Read what @jonnin and @AbstractionAnon wrote. You are trying to access arrays beyond their bounds. Your [] operator will point to any offset from the start of the array that you specify, even if it hasn't been set aside for array sid[]. You will access whatever happens to be there, and this is likely to be different between different compilers (and possibly the same compiler with different compile options).
If it works with one compiler then it is more by luck than judgement.
Don't deliberately try to access arrays beyond bounds.
And please stop asking people to click and access external sites. It's a recipe for malicious software. You can paste code and output directly here. If you need copies of output then redirect your program's output or error logs to a text file.
@lastchance, i know that this should not be done, i did that to show that even though sid [] has size 2, still i can access beyond index 1.
And even if i remove that, even though i assigned sid[] two chars in the second loop, when i cout <<sid i get chars stored in sid+id.
That was my issue, and why did that happen??
I figured that out from @jonin answer
I am using 2 element array, and storing exactly 2 members in it, there is no place to store the "/0" character. That was my error,
Is the reason for my problem correct?
If wrong, please correct me
Thankyou
I am using 2 element array, and storing exactly 2 members in it, there is no place to store the "/0" character. That was my error,
Partly; you won't be able to use functions like atoi() or print it out unless it has '\0' at the end. However, if you simply need to store 2 characters then an array size of 2 is fine. It may run differently on different compilers simply because various memory slots might, by luck or otherwise, contain '\0' at that point, so allowing routines like atoi() to operate on what they think is a valid C-string.
atoi(id) doesn't know anything about the declared length of id. It simply gets a pointer to the start and then keeps going until it finds a '\0'. Then it concludes that the C-string is finished. This may be long after the length of id.