using new operator to create an array of pointers ?!

Discussion in 'C++' started by ptek, Sep 19, 2006.

  1. ptek

    ptek Guest

    Hi all,

    I'm quite new to pointers, so this might be a silly question :S


    I need to allocate an array of pointers to unsigned char type...

    So, if I needed instead to allocate an array of unsigned chars, i'll do
    this :

    uchar *pointer = new unsigned [100];


    Since I need an array of ponters I tried :

    uchar *pointer = new *unsigned char[100];


    What is wrong with this code ?
    Does the new operator will do for this ?
    Or should I used malloc ?

    Thanks
    ptek, Sep 19, 2006
    #1
    1. Advertising

  2. ptek

    Rolf Magnus Guest

    ptek wrote:

    > Hi all,
    >
    > I'm quite new to pointers, so this might be a silly question :S
    >
    >
    > I need to allocate an array of pointers to unsigned char type...
    > So, if I needed instead to allocate an array of unsigned chars, i'll do
    > this :
    >
    > uchar *pointer = new unsigned [100];


    This shouldn't work due to a type mismatch.

    >
    >
    > Since I need an array of ponters I tried :
    >
    > uchar *pointer = new *unsigned char[100];
    >
    >
    > What is wrong with this code ?


    If you allocate an array of uchar, new will give you back a pointer to
    uchar. Now guess what it will give you if you allocate an array of pointers
    to uchar. Also, the syntax for a pointer to unsigned char is not
    *unsigned char. Try:

    uchar** pointer = new unsigned char*[100];

    > Does the new operator will do for this ?


    Yes.

    > Or should I used malloc ?


    You should use neither. Instead try a vector.

    std::vector<uchar*> vec(100);
    Rolf Magnus, Sep 19, 2006
    #2
    1. Advertising

  3. ptek

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > Since I need an array of ponters I tried :
    >
    > uchar *pointer = new *unsigned char[100];


    That needs to be:

    unsigned char *pointer = new unsigned char *[100];

    Though, of course we're left wondering why you're using dynamic
    allocation at all. Chances are pretty good you'd be better off with a
    vector -- and quite possibly of std::basic_string<unsigned char> than of
    pointers to char.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 19, 2006
    #3
  4. ptek

    ptek Guest

    Hi Rolf
    Thanks for your answer.

    Man, I was really dumb to swap unsigned char* for *unsigned char
    ....
    And believe it or not, i've tried the unsigned char** pointer
    previously, but with the other error, of course ...

    As for the std::vector, that's pretty unknown to me ... I think i'll
    stick to the new operator for now. Is there advantages to use the
    std::vector thing instead of the new ?

    tnx


    Rolf Magnus wrote:
    >
    > uchar** pointer = new unsigned char*[100];


    >
    > You should use neither. Instead try a vector.
    >
    > std::vector<uchar*> vec(100);
    ptek, Sep 19, 2006
    #4
  5. ptek

    ptek Guest

    Well, the 100 was just an example : in fact, i need to allocate
    quantity that is a product of two variables...

    As for the std::basic_string<unsigned char>, i must say that my C++
    needs some more studing :) Is this a template, right ?


    Jerry Coffin wrote:
    > In article <>,
    > says...
    >
    > [ ... ]
    >
    > > Since I need an array of ponters I tried :
    > >
    > > uchar *pointer = new *unsigned char[100];

    >
    > That needs to be:
    >
    > unsigned char *pointer = new unsigned char *[100];
    >
    > Though, of course we're left wondering why you're using dynamic
    > allocation at all. Chances are pretty good you'd be better off with a
    > vector -- and quite possibly of std::basic_string<unsigned char> than of
    > pointers to char.
    >
    > --
    > Later,
    > Jerry.
    >
    > The universe is a figment of its own imagination.
    ptek, Sep 19, 2006
    #5
  6. ptek

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... top posting fixed ]

    > Jerry Coffin wrote:


    [ ... ]

    > > Though, of course we're left wondering why you're using dynamic
    > > allocation at all. Chances are pretty good you'd be better off with a
    > > vector -- and quite possibly of std::basic_string<unsigned char> than of
    > > pointers to char.

    >
    > Well, the 100 was just an example : in fact, i need to allocate
    > quantity that is a product of two variables...


    Okay -- that would justify some sort of dynamic allocation, but it still
    sounds like you could use an std::vector to do the job:

    std::vector<whatever> p(var1*var2);

    > As for the std::basic_string<unsigned char>, i must say that my C++
    > needs some more studing :) Is this a template, right ?


    std::basic_string is a template. This is an instantiation of the
    template to produce a class. In a typical case, you'd probably want to
    do something like:

    typedef std::basic_string<unsigned char> ustring;

    std::vector<ustring> p(var1*var2);

    In fact, std::string and std::wstring are just like this:

    typedef std::basic_string<char> string;
    typedef std::basic_string<wchar_t> wstring;

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 19, 2006
    #6
  7. ptek

    Marcus Kwok Guest

    ptek <> wrote:
    > As for the std::vector, that's pretty unknown to me ... I think i'll
    > stick to the new operator for now. Is there advantages to use the
    > std::vector thing instead of the new ?


    See the FAQ:
    http://www.parashift.com/c -faq-lite/containers.html#faq-34.1

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Sep 19, 2006
    #7
  8. ptek

    Howard Guest

    "Jerry Coffin" <> wrote in message
    news:...
    > In article <>,
    > says...
    >
    > [ ... ]
    >
    >> Since I need an array of ponters I tried :
    >>
    >> uchar *pointer = new *unsigned char[100];

    >
    > That needs to be:
    >
    > unsigned char *pointer = new unsigned char *[100];


    Actually, that should be:

    unsigned char ** pointer = new unsigned char *[100];

    -Howard
    Howard, Sep 19, 2006
    #8
  9. ptek

    Jerry Coffin Guest

    In article <T%UPg.183681$>,
    says...

    [ ... ]

    > Actually, that should be:
    >
    > unsigned char ** pointer = new unsigned char *[100];


    Oops -- thanks for the correction.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 19, 2006
    #9
    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. Manish_Ganvir
    Replies:
    13
    Views:
    1,539
    Keith Thompson
    Feb 14, 2005
  2. Sean
    Replies:
    2
    Views:
    625
    loufoque
    Sep 24, 2006
  3. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    314
    Chris Torek
    Apr 6, 2007
  4. xmllmx
    Replies:
    6
    Views:
    395
    xmllmx
    Feb 3, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    642
Loading...

Share This Page