Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > junk characters generated with new operator

Reply
Thread Tools

junk characters generated with new operator

 
 
bintom
Guest
Posts: n/a
 
      03-09-2011
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();}
}
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      03-09-2011
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.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-09-2011
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.
 
Reply With Quote
 
bintom
Guest
Posts: n/a
 
      03-10-2011
Thanks Paul,

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

Bintom
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-10-2011
Pete Becker <> 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.
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      03-10-2011
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.
 
Reply With Quote
 
Sprechen sie C++
Guest
Posts: n/a
 
      03-10-2011
"bintom" wrote in message
news:376aea8a-2187-4ac0-aa31-...

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the key differences between operator new and operator new[]? xmllmx C++ 6 02-03-2010 04:11 PM
Remove only special characters and junk characters from a file rvino Perl 0 08-14-2007 07:23 AM
Problem with .next() method adding junk characters. Rainy Python 6 10-03-2006 12:26 AM
Allocation with new operator and Destroying with free operator saikishore.vanga@wipro.com C++ 8 12-22-2005 01:35 PM
Interrogating string for number of characters, response.writing identical number of characters on new line Ken Fine ASP General 2 02-05-2004 03:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57