dynamic memory allocating

Discussion in 'C++' started by filox, Apr 24, 2006.

  1. filox

    filox Guest

    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
     
    filox, Apr 24, 2006
    #1
    1. Advertising

  2. On Mon, 24 Apr 2006 16:31:17 +0200, "filox"
    <> 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
     
    Bob Hairgrove, Apr 24, 2006
    #2
    1. Advertising

  3. filox

    filox Guest

    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" <> wrote in message
    news:...
    > On Mon, 24 Apr 2006 16:31:17 +0200, "filox"
    > <> 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
    >
     
    filox, Apr 24, 2006
    #3
    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. hall
    Replies:
    4
    Views:
    445
  2. soni29
    Replies:
    6
    Views:
    411
    Kevin Goodsell
    Sep 5, 2003
  3. fivelitermustang
    Replies:
    11
    Views:
    812
    John Harrison
    May 23, 2004
  4. Andreas Lassmann

    allocating dynamic memory

    Andreas Lassmann, Jan 21, 2005, in forum: C++
    Replies:
    7
    Views:
    436
    axter
    Feb 4, 2005
  5. Rakesh Kumar
    Replies:
    5
    Views:
    684
    James Kanze
    Dec 21, 2007
Loading...

Share This Page