![]() |
junk characters generated with new operator
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();} } |
Re: junk characters generated with new operator
On Mar 9, 9:01*pm, bintom <binoythomas1...@gmail.com> 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. |
Re: junk characters generated with new operator
On Mar 9, 1:23*pm, Paul N <gw7...@aol.com> 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. |
Re: junk characters generated with new operator
Thanks Paul,
I don't know how I goofed up on that. Bintom |
Re: junk characters generated with new operator
Pete Becker <pete@versatilecoding.com> wrote:
> On 2011-03-09 17:23:46 -0500, red floyd said: > >> On Mar 9, 1:23*pm, Paul N <gw7...@aol.com> 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. |
Re: junk characters generated with new operator
On Mar 10, 8:05*am, Juha Nieminen <nos...@thanks.invalid> wrote:
> Pete Becker <p...@versatilecoding.com> wrote: > > On 2011-03-09 17:23:46 -0500, red floyd said: > > >> On Mar 9, 1:23 pm, Paul N <gw7...@aol.com> 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. |
Re: junk characters generated with new operator
"bintom" wrote in message
news:376aea8a-2187-4ac0-aa31-a2c35422644c@f31g2000pri.googlegroups.com... 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 |
| All times are GMT. The time now is 01:46 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.