Allocating array of pointers to structs

Discussion in 'C++' started by dev_15, Nov 15, 2007.

  1. dev_15

    dev_15 Guest

    Hi, I'm going through some code and thought that this allocates an
    array of structs but its supposed according to comments to allocate an
    array of pointer to structs. What does it actually do

    ptrLogArray = new structDisplayLogData *[iCount];

    // iCount = 50 structDisplayLogData is a strcuture of data

    Thanks
     
    dev_15, Nov 15, 2007
    #1
    1. Advertising

  2. dev_15 <> writes:

    > Hi, I'm going through some code and thought that this allocates an
    > array of structs but its supposed according to comments to allocate an
    > array of pointer to structs. What does it actually do
    >
    > ptrLogArray = new structDisplayLogData *[iCount];


    It behaves as per the comments. What follows 'new' is, syntactically,
    a declaration with the name missed out. If you declared this array
    you'd write:

    structDisplayLogData *an_array_of_ptrs_to_structs[iCount];

    now just remove the name and add 'new':

    new structDisplayLogData *[iCount];

    If you thought

    structDisplayLogData *array[iCount];

    should mean:

    structDisplayLogData (*array)[iCount];

    (i.e. a pointer to an array of structs) rather than what it *does* mean:

    structDisplayLogData *(array[iCount]);

    (an array of pointers) then you need to study the syntax a bit more.

    --
    Ben.
     
    Ben Bacarisse, Nov 15, 2007
    #2
    1. Advertising

  3. dev_15

    dev_15 Guest

    Thanks also in the code later on is

    ptrLogArray[0] = new structDisplayLogData;

    -------------
    more code

    delete [] ptrLogArray;

    Does this delete the structs that the pointers are pointing to?



    Ben Bacarisse wrote:
    > dev_15 <> writes:
    >
    > > Hi, I'm going through some code and thought that this allocates an
    > > array of structs but its supposed according to comments to allocate an
    > > array of pointer to structs. What does it actually do
    > >
    > > ptrLogArray = new structDisplayLogData *[iCount];

    >
    > It behaves as per the comments. What follows 'new' is, syntactically,
    > a declaration with the name missed out. If you declared this array
    > you'd write:
    >
    > structDisplayLogData *an_array_of_ptrs_to_structs[iCount];
    >
    > now just remove the name and add 'new':
    >
    > new structDisplayLogData *[iCount];
    >
    > If you thought
    >
    > structDisplayLogData *array[iCount];
    >
    > should mean:
    >
    > structDisplayLogData (*array)[iCount];
    >
    > (i.e. a pointer to an array of structs) rather than what it *does* mean:
    >
    > structDisplayLogData *(array[iCount]);
    >
    > (an array of pointers) then you need to study the syntax a bit more.
    >
    > --
    > Ben.
     
    dev_15, Nov 15, 2007
    #3
  4. dev_15

    David Harmon Guest

    On Thu, 15 Nov 2007 08:45:56 -0800 (PST) in comp.lang.c++, dev_15
    <> wrote,
    >Hi, I'm going through some code and thought that this allocates an
    >array of structs but its supposed according to comments to allocate an
    >array of pointer to structs. What does it actually do
    >
    >ptrLogArray = new structDisplayLogData *[iCount];


    Array of pointers; that's what the '*' is about.

    However, that's probably undesirable code. What happens next is either
    trying to write via an uninitialized pointer, or else lots of memory
    management headaches. What you wanted instead is probably

    std::vector<structDisplayLogData> LogArray(iCount);
     
    David Harmon, Nov 15, 2007
    #4
  5. dev_15 <> writes:

    > Thanks also in the code later on is
    >
    > ptrLogArray[0] = new structDisplayLogData;
    >
    > -------------
    > more code
    >
    > delete [] ptrLogArray;
    >
    > Does this delete the structs that the pointers are pointing to?


    No. As someone else has pointed out, if you are changing the code
    consider switching to std::vector.

    PS. Don't top post.

    --
    Ben.
     
    Ben Bacarisse, Nov 15, 2007
    #5
  6. dev_15

    Default User Guest

    Re: Allocating array of pointers to structs - TPA

    dev_15 wrote:

    > Thanks also in the code later on is


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
     
    Default User, Nov 15, 2007
    #6
    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. Erik S. Bartul

    allocating for an array of pointers

    Erik S. Bartul, Feb 21, 2004, in forum: C Programming
    Replies:
    3
    Views:
    21,966
    Lewis Bowers
    Feb 21, 2004
  2. Paminu
    Replies:
    5
    Views:
    651
    Eric Sosman
    Oct 11, 2005
  3. Win Sock

    Allocating memory for structs

    Win Sock, Oct 6, 2007, in forum: C Programming
    Replies:
    8
    Views:
    431
    Army1987
    Oct 7, 2007
  4. Rakesh Kumar
    Replies:
    5
    Views:
    690
    James Kanze
    Dec 21, 2007
  5. Tuan  Bui
    Replies:
    14
    Views:
    493
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page