junk characters generated with new operator

Discussion in 'C++' started by bintom, Mar 9, 2011.

  1. bintom

    bintom Guest

    Hey, can anybody tell me what I'm doing wrong in the following
    program?
    The output generated is some junk characters followed by 5 (the size
    of "Texas").
    Thanks,
    Bintom


    #include <iostream.h>

    class state
    { char *name;
    int size;
    public : state(char* s)
    { size = strlen(s);
    name = new char[size + 1];
    }

    void display()
    { cout << name << " " << size << "\n"; }
    };

    int main()
    { state S1("Texas");

    S1.display();}
    }
    bintom, Mar 9, 2011
    #1
    1. Advertising

  2. bintom

    Paul N Guest

    On Mar 9, 9:01 pm, bintom <> wrote:
    > Hey, can anybody tell me what I'm doing wrong in the following
    > program?
    > The output generated is some junk characters followed by 5 (the size
    > of "Texas").
    > Thanks,
    > Bintom
    >
    > #include <iostream.h>
    >
    > class state
    > { char *name;
    >   int size;
    >   public : state(char* s)
    >              { size = strlen(s);
    >                name = new char[size + 1];
    >              }
    >
    >              void display()
    >              { cout << name << "  " << size << "\n";   }
    >
    > };
    >
    > int main()
    > { state S1("Texas");
    >
    >   S1.display();}
    >
    >
    >
    > }


    Your problem is that, while you are allocating some space for "name"
    which is big enough to hold the name of the state, you aren't actually
    copying the name into that space.

    If I were writing the program I'd probably use strcpy(name, s) to copy
    the name in.

    Most people here would probably recommend that you use a string
    instead of a char pointer. And they'd probably suggest changing
    <iostream.h> to <iostream>, which will mean putting a "std" somewhere
    in your code. It's up to you how much of this you go along with.

    Hope that helps.
    Paul.
    Paul N, Mar 9, 2011
    #2
    1. Advertising

  3. bintom

    red floyd Guest

    On Mar 9, 1:23 pm, Paul N <> wrote:

    >
    > And they'd probably suggest changing
    > <iostream.h> to <iostream>, which will mean putting a "std" somewhere
    > in your code. It's up to you how much of this you go along with.
    >


    So you recommend using non-standard headers? Even MSVC doesn't have
    <iostream.h> any more.
    red floyd, Mar 9, 2011
    #3
  4. bintom

    bintom Guest

    Thanks Paul,

    I don't know how I goofed up on that.

    Bintom
    bintom, Mar 10, 2011
    #4
  5. Pete Becker <> wrote:
    > On 2011-03-09 17:23:46 -0500, red floyd said:
    >
    >> On Mar 9, 1:23 pm, Paul N <> wrote:
    >>
    >>>
    >>> And they'd probably suggest changing
    >>> <iostream.h> to <iostream>, which will mean putting a "std" somewhere
    >>> in your code. It's up to you how much of this you go along with.
    >>>

    >>
    >> So you recommend using non-standard headers? Even MSVC doesn't have
    >> <iostream.h> any more.

    >
    > Um, no, the suggestion is just the opposite. The original code used
    > <iostream.h>, and the suggestion was to change that to <iostream>.


    Read it again. It's not suggesting it, it's saying that "people
    will suggest it, it's up to you to decide". Writing non-standard code
    is not a good idea, so the "decision" is a no-brainer.
    Juha Nieminen, Mar 10, 2011
    #5
  6. bintom

    Paul N Guest

    On Mar 10, 8:05 am, Juha Nieminen <> wrote:
    > Pete Becker <> wrote:
    > > On 2011-03-09 17:23:46 -0500, red floyd said:

    >
    > >> On Mar 9, 1:23 pm, Paul N <> wrote:

    >
    > >>> And they'd probably suggest changing
    > >>> <iostream.h> to <iostream>, which will mean putting a "std" somewhere
    > >>> in your code. It's up to you how much of this you go along with.

    >
    > >> So you recommend using non-standard headers?  Even MSVC doesn't have
    > >> <iostream.h> any more.

    >
    > > Um, no, the suggestion is just the opposite. The original code used
    > > <iostream.h>, and the suggestion was to change that to <iostream>.

    >
    >   Read it again. It's not suggesting it, it's saying that "people
    > will suggest it, it's up to you to decide". Writing non-standard code
    > is not a good idea, so the "decision" is a no-brainer.


    Perhaps I should have stuck to just answering the question, which the
    OP seemed perfectly happy with...

    I don't know the OP's circumstances. Possibly he is using an old
    compiler which (like the older of my two compilers) simply doesn't
    have <iostream>. Or perhaps he would be better off concentrating on
    getting his code working on his system rather than worrying about how
    it will port to other systems. Or, conversely, perhaps he would indeed
    be better off learning about standard code at this stage. I was just
    trying to give a pointer without being too prescriptive.

    Incidentally, I hardly ever use either myself; for DOS programs I
    either use stdio.h or access the screen memory directly, for Windows
    programs I have to use other techniques anyway. I suspect I'm not
    typical in this group, just as I suspect even more strongly that my
    preference for C-type string handling over C++ strings is not typical
    of the group.

    Hope that clears up any doubt.
    Paul.
    Paul N, Mar 10, 2011
    #6
  7. "bintom" wrote in message
    news:...

    Thanks Paul,

    I don't know how I goofed up on that.


    Microsoft gives away the express editions of Visual Studio for free. The
    2010 version supports modern programming constructs
    Sprechen sie C++, Mar 10, 2011
    #7
    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. Peter Row
    Replies:
    0
    Views:
    289
    Peter Row
    Mar 3, 2004
  2. =?Utf-8?B?TEFDSA==?=

    Re: SmtpMail - Hotmail Junk e-mail

    =?Utf-8?B?TEFDSA==?=, Sep 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    441
    =?Utf-8?B?TEFDSA==?=
    Sep 16, 2004
  3. Rainy
    Replies:
    6
    Views:
    270
    Terry Reedy
    Oct 3, 2006
  4. rvino
    Replies:
    0
    Views:
    4,649
    rvino
    Aug 14, 2007
  5. xmllmx
    Replies:
    6
    Views:
    412
    xmllmx
    Feb 3, 2010
Loading...

Share This Page