strange behaviour with string

P

PaulR

Why does this code screw up my compiler?

#include <iostream>

int main(){
char str[64]={"Qjnlm%nx%f%ywtqq"};
for(int i=0; i<64; i++){out= out-5;}
out[63]=0x7;
std::cout<<str;
}


hmm very strange °_°
 
P

Paul

Copy/paste error, this should work but some Exception problem, why?

#include <iostream>

int main(){
char out[64]={"Qjnlm%nx%f%ywtqq"};
for(int i=0; i<64; i++){out= out-5;}
out[63]=0x7;
std::cout<<out;
}


°_°
 
K

Kevin P. Fleming

Copy/paste error, this should work but some Exception problem, why?

#include <iostream>

int main(){
char out[64]={"Qjnlm%nx%f%ywtqq"};
for(int i=0; i<64; i++){out= out-5;}
out[63]=0x7;
std::cout<<out;
}


You are feeding a string without a terminating null character to
std::cout... so it's going to continue printing beyond the end of your
64-character array until it reaches one or it hits data it cannot
output. In addition, it's possible that all characters in 'out' beyond
the ones you initialized with the string constant will have random
values, since you didn't provide values for them.
 
J

Juha Nieminen

Kevin P. Fleming said:
In addition, it's possible that all characters in 'out' beyond
the ones you initialized with the string constant will have random
values, since you didn't provide values for them.

I thought that if you specified any initialization values for a
static array (even an empty initialization block), all the remaining
elements will be default-initialized (even if the elements are primitive
types). Or does this work differently if you initialize a char array
with a string?
 
K

Kevin P. Fleming

I thought that if you specified any initialization values for a
static array (even an empty initialization block), all the remaining
elements will be default-initialized (even if the elements are primitive
types). Or does this work differently if you initialize a char array
with a string?

The OP's example array wasn't static, it was on the stack. If it was
static, then yes, all elements not explicitly initialized would be set
to zero.

Of course, the OP's code also iterates over all the elements of the
array and subtracts 5 from them... so any elements that were zero
wouldn't be any longer.
 
M

Marcel Müller

Kevin said:
#include <iostream>

int main(){
char out[64]={"Qjnlm%nx%f%ywtqq"};
for(int i=0; i<64; i++){out= out-5;}
out[63]=0x7;
std::cout<<out;
}


You are feeding a string without a terminating null character to
std::cout... so it's going to continue printing beyond the end of your
64-character array until it reaches one or it hits data it cannot
output.


Yes, because the for loop transformed the terminating zeros into 0xfb.
In addition, it's possible that all characters in 'out' beyond
the ones you initialized with the string constant will have random
values, since you didn't provide values for them.

No. They are initially zero. Initializers never init an object only half.


Marcel
 
P

Paul

Kevin P. Fleming said:
The OP's example array wasn't static, it was on the stack. If it was
static, then yes, all elements not explicitly initialized would be set to
zero.

Of course, the OP's code also iterates over all the elements of the array
and subtracts 5 from them... so any elements that were zero wouldn't be
any longer.
So if I change 0x7 to 0x0 will it work ok?
 
K

Kevin P. Fleming

So if I change 0x7 to 0x0 will it work ok?

That depends on how define 'work'. The string will be null-terminated,
and so the operation of emitting it to cout should complete normally.
Whether the output will be what you want or not is debatable, because I
assume you put that 0x7 there for a reason.
 
P

Paul

Kevin P. Fleming said:
That depends on how define 'work'. The string will be null-terminated, and
so the operation of emitting it to cout should complete normally. Whether
the output will be what you want or not is debatable, because I assume you
put that 0x7 there for a reason.
Its just a ding-a-ling, not 100% necessary. What output does this produce on
your compiler?
 
P

Paul

Copy/paste error, this should work but some Exception problem, why?

#include <iostream>

int main(){
char out[64]={"Qjnlm%nx%f%ywtqq"};
for(int i=0; i<64; i++){out= out-5;}
out[63]=0x7;
std::cout<<out;
}


You are feeding a string without a terminating null character to
std::cout... so it's going to continue printing beyond the end of your
64-character array until it reaches one or it hits data it cannot
output. In addition, it's possible that all characters in 'out' beyond
the ones you initialized with the string constant will have random
values, since you didn't provide values for them.


It doesn't seem to compile unless I enable EHsc. Does cout throw or is EHsc
just a prerequisite of using std::cout ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top