strange behaviour with string

Discussion in 'C++' started by PaulR, Feb 5, 2011.

  1. PaulR

    PaulR Guest

    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 °_°
     
    PaulR, Feb 5, 2011
    #1
    1. Advertising

  2. PaulR

    Paul Guest

    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;
    }


    °_°
     
    Paul, Feb 5, 2011
    #2
    1. Advertising

  3. On 02/05/2011 02:51 AM, Paul wrote:
    > 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.
     
    Kevin P. Fleming, Feb 5, 2011
    #3
  4. Kevin P. Fleming <> wrote:
    > 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?
     
    Juha Nieminen, Feb 5, 2011
    #4
  5. On 02/05/2011 10:32 AM, Juha Nieminen wrote:
    > Kevin P. Fleming<> wrote:
    >> 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?


    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.
     
    Kevin P. Fleming, Feb 5, 2011
    #5
  6. Kevin P. Fleming wrote:
    > On 02/05/2011 02:51 AM, Paul wrote:
    >> #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
     
    Marcel Müller, Feb 5, 2011
    #6
  7. PaulR

    Paul Guest

    "Kevin P. Fleming" <> wrote in message
    news:4d4d2213$0$7750$...
    > On 02/05/2011 10:32 AM, Juha Nieminen wrote:
    >> Kevin P. Fleming<> wrote:
    >>> 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?

    >
    > 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?
     
    Paul, Feb 5, 2011
    #7
  8. On 02/05/2011 11:42 AM, Paul wrote:
    >
    > "Kevin P. Fleming" <> wrote in message
    > news:4d4d2213$0$7750$...
    >> On 02/05/2011 10:32 AM, Juha Nieminen wrote:
    >>> Kevin P. Fleming<> wrote:
    >>>> 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?

    >>
    >> 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?


    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.
     
    Kevin P. Fleming, Feb 5, 2011
    #8
  9. PaulR

    Paul Guest

    "Kevin P. Fleming" <> wrote in message
    news:4d4d43dd$0$7758$...
    > On 02/05/2011 11:42 AM, Paul wrote:
    >>
    >> "Kevin P. Fleming" <> wrote in message
    >> news:4d4d2213$0$7750$...
    >>> On 02/05/2011 10:32 AM, Juha Nieminen wrote:
    >>>> Kevin P. Fleming<> wrote:
    >>>>> 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?
    >>>
    >>> 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?

    >
    > 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?
     
    Paul, Feb 5, 2011
    #9
  10. PaulR

    Paul Guest


    > On 05/02/2011 06:29, Kevin P. Fleming wrote:
    >> On 02/05/2011 02:51 AM, Paul wrote:
    >>> 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 ?
     
    Paul, Feb 5, 2011
    #10
  11. PaulR

    Geoff Guest

    On Fri, 4 Feb 2011 16:17:32 -0800 (PST), PaulR <>
    wrote:

    >Why does this code screw up my compiler?
    >

    [snip]

    Because you are such a bad coder you can't even write bug-free
    obfuscation.

    >
    >hmm very strange °_°


    Not strange at all for a troll.
     
    Geoff, Feb 5, 2011
    #11
  12. PaulR

    Paul Guest

    "Geoff" <> wrote in message
    news:...
    > On Fri, 4 Feb 2011 16:17:32 -0800 (PST), PaulR <>
    > wrote:
    >
    >>Why does this code screw up my compiler?
    >>

    > [snip]
    >
    > Because you are such a bad coder you can't even write bug-free
    > obfuscation.
    >
    >>
    >>hmm very strange °_°

    >
    > Not strange at all for a troll.
    >


    Someone just ran over your dog?
     
    Paul, Feb 5, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antonio

    Strange encoding behaviour

    Antonio, Dec 29, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    427
    Antonio
    Dec 29, 2004
  2. Jan
    Replies:
    2
    Views:
    1,439
    Mike Treseler
    Dec 16, 2004
  3. Andy Chambers
    Replies:
    1
    Views:
    389
    Daniel Dyer
    May 14, 2007
  4. khany
    Replies:
    6
    Views:
    1,117
    Dave Angel
    Oct 19, 2009
  5. Rajpreet
    Replies:
    1
    Views:
    103
    J. Gleixner
    Feb 12, 2008
Loading...

Share This Page