Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamic memory allocating

Reply
Thread Tools

dynamic memory allocating

 
 
filox
Guest
Posts: n/a
 
      04-24-2006
i'm having a really hard time with this one:

class foo
{
private:
char* p;
public:
foo(int);
};
foo::foo(int j)
{
p = new char[j];
p[0] = '1';
p[1] = '2';
};
int main(void)
{
foo k(2);
return 0;
}
why can't i set the value of p[1]? p[0] passes ok, while p[1] = '2' is just
ignored.
please help
--
You're never too young to have a Vietnam flashback


 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      04-24-2006
On Mon, 24 Apr 2006 16:31:17 +0200, "filox"
<(E-Mail Removed)> wrote:

>i'm having a really hard time with this one:
>
>class foo
>{
>private:
>char* p;
>public:
>foo(int);
>};
>foo::foo(int j)
>{
>p = new char[j];
>p[0] = '1';
>p[1] = '2';
>};
>int main(void)
>{
>foo k(2);
>return 0;
>}
>why can't i set the value of p[1]? p[0] passes ok, while p[1] = '2' is just
>ignored.
>please help


What do you mean by "ignored"? Are you trying to create a C-style
string? If so, you will need to leave room for the null byte at the
end.

(BTW, you have a memory leak -- by not calling "delete [] p;" in the
destructor of your class or some other appropriate place).

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
filox
Guest
Posts: n/a
 
      04-24-2006
uhm, never mind. there's actually no problem, i just put too much fait in Ms
Visual Studio debugger
its ok now. thanks for your effort, though

--
You're never too young to have a Vietnam flashback
"Bob Hairgrove" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mon, 24 Apr 2006 16:31:17 +0200, "filox"
> <(E-Mail Removed)> wrote:
>
> >i'm having a really hard time with this one:
> >
> >class foo
> >{
> >private:
> >char* p;
> >public:
> >foo(int);
> >};
> >foo::foo(int j)
> >{
> >p = new char[j];
> >p[0] = '1';
> >p[1] = '2';
> >};
> >int main(void)
> >{
> >foo k(2);
> >return 0;
> >}
> >why can't i set the value of p[1]? p[0] passes ok, while p[1] = '2' is

just
> >ignored.
> >please help

>
> What do you mean by "ignored"? Are you trying to create a C-style
> string? If so, you will need to leave room for the null byte at the
> end.
>
> (BTW, you have a memory leak -- by not calling "delete [] p;" in the
> destructor of your class or some other appropriate place).
>
> --
> Bob Hairgrove
> (E-Mail Removed)



 
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
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
allocating dynamic memory Andreas Lassmann C++ 7 02-04-2005 01:59 AM
Allocating Four Dimensional Dynamic Arrays... fivelitermustang C++ 11 05-23-2004 06:08 AM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM



Advertisments