how to declare an array of objects without "new"

Discussion in 'C++' started by b83503104@yahoo.com, Jul 11, 2005.

  1. Guest

    Hi,

    class MyClass{
    int array_size;
    HisClass **hisObject;
    };

    I want hisObject to point to an array of HisClass objects.

    The size of the array is given by array_size.

    My question is, if objects of type HisClass must be declared by the
    library provided function CreateObject(), and cannot be declared by
    using "new", how should I declare hisObject?

    If I do this in the constructor, the compiler does not complain, but I
    get Segmentation fault:

    array_size = 5;
    for (int i = 0; i < array_size; i++){
    hisObject = CreateObject(...);
    }

    Thanks in advance!
     
    , Jul 11, 2005
    #1
    1. Advertising

  2. Lionel Guest

    wrote:
    > Hi,
    >
    > class MyClass{
    > int array_size;
    > HisClass **hisObject;
    > };


    All you've done is created a pointer to nothing, it's not allocated any
    space.


    > I want hisObject to point to an array of HisClass objects.


    What you want is an array of pointers to HisClass objects:

    HisClass *hisObject[array_size];

    This will create an array of length array_size, with each element
    allowing enough space for the size of a pointer to a HisClass object.


    >
    > The size of the array is given by array_size.
    >
    > My question is, if objects of type HisClass must be declared by the
    > library provided function CreateObject(), and cannot be declared by
    > using "new", how should I declare hisObject?
    >
    > If I do this in the constructor, the compiler does not complain, but I
    > get Segmentation fault:
    >
    > array_size = 5;
    > for (int i = 0; i < array_size; i++){
    > hisObject = CreateObject(...);
    > }


    The compiler won't complain because it assumes you have done the right
    thing and allocated the memory. However, at run time your app tries to
    access memory that it is not allowed to or isn't a valid address and so
    you get the seg fault.


    Lionel.
     
    Lionel, Jul 11, 2005
    #2
    1. Advertising

  3. David White Guest

    wrote:
    > Hi,
    >
    > class MyClass{
    > int array_size;
    > HisClass **hisObject;
    > };
    >
    > I want hisObject to point to an array of HisClass objects.
    >
    > The size of the array is given by array_size.
    >
    > My question is, if objects of type HisClass must be declared by the
    > library provided function CreateObject(), and cannot be declared by
    > using "new", how should I declare hisObject?
    >
    > If I do this in the constructor, the compiler does not complain, but I
    > get Segmentation fault:


    Because hisObject could be pointing anywhere.

    > array_size = 5;


    hisObject = new HisClass *[array_size];

    > for (int i = 0; i < array_size; i++){
    > hisObject = CreateObject(...);
    > }


    Have you considered using a std::vector<HisClass *> instead?

    DW
     
    David White, Jul 11, 2005
    #3
  4. David White Guest

    Lionel wrote:
    > What you want is an array of pointers to HisClass objects:
    >
    > HisClass *hisObject[array_size];


    It looks from the OP's post that array_size is not a compile-time constant.

    DW
     
    David White, Jul 11, 2005
    #4
  5. Guest

    Thanks. But there is some problem.

    "hisObject" is defined in the class definition, and "array_size" is
    initialized in the constructor, so I cannot (?!) do this:
    HisClass *hisObject[array_size];

    Neither can I use "new". I have to use "CreateObject()" to allocate
    space. How should I do that?

    Thanks!
     
    , Jul 11, 2005
    #5
  6. Guest

    You suggested
    hisObject = new HisClass *[array_size];

    Unfortunately, I must not use "new". I have to use the library
    provided function "CreateObject(...)" to allocate space.

    Still need suggestions. Thanks in advance.
     
    , Jul 11, 2005
    #6
  7. Lionel Guest

    David White wrote:
    > Lionel wrote:
    >
    >>What you want is an array of pointers to HisClass objects:
    >>
    >>HisClass *hisObject[array_size];

    >
    >
    > It looks from the OP's post that array_size is not a compile-time constant.


    I was a little lazy and assumed the OP was doing something sensible with
    array_size . . .
     
    Lionel, Jul 11, 2005
    #7
  8. Jack Klein Guest

    On 10 Jul 2005 22:42:47 -0700, ""
    <> wrote in comp.lang.c++:

    > You suggested
    > hisObject = new HisClass *[array_size];
    >
    > Unfortunately, I must not use "new". I have to use the library
    > provided function "CreateObject(...)" to allocate space.
    >
    > Still need suggestions. Thanks in advance.


    You are not reading the reply carefully enough.

    The allocation David suggested for hisObject does not create any
    HisClass objects at all, it creates an array of array_size pointers to
    HisClass objects.

    Then you iterate through the array:

    for (int count = 0; count < array_size; ++count)
    {
    hisObject [count] = CreateObject();
    }

    You are not using new to bypass calling the object creation function
    at all. You are using new to allocate space for the pointers.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 11, 2005
    #8
  9. Lionel Guest

    wrote:
    > You suggested
    > hisObject = new HisClass *[array_size];
    >
    > Unfortunately, I must not use "new". I have to use the library
    > provided function "CreateObject(...)" to allocate space.
    >
    > Still need suggestions. Thanks in advance.
    >


    This wouldn't happen to be an assignment would it?
     
    Lionel, Jul 11, 2005
    #9
  10. Me Guest

    > class MyClass{
    > int array_size;
    > HisClass **hisObject;
    > };
    >
    > If I do this in the constructor, the compiler does not complain, but I
    > get Segmentation fault:
    >
    > array_size = 5;


    hisObject = new HisClass *[array_size];

    > for (int i = 0; i < array_size; i++){
    > hisObject = CreateObject(...);
    > }


    then in your destructor after destroying each hisObject, don't
    forget to:

    delete [] hisObject;
     
    Me, Jul 11, 2005
    #10
  11. Guest

    Great, it works now. I didn't notice it was creating an array of
    pointers.

    I tried something else, which did not work, and I hope somebody could
    point out why:

    p = new HisClass *[array_size];
    for (int i = 0; i < array_size; i++)
    {
    p = CreateObject(...);
    }

    hisObject = p;

    where "hisObject" was defined in the class as
    HisClass **hisObject;

    Shouldn't this be the same as what David or Jack suggested?
     
    , Jul 11, 2005
    #11
  12. <> wrote in message
    news:...
    > Thanks. But there is some problem.
    >
    > "hisObject" is defined in the class definition, and "array_size" is
    > initialized in the constructor, so I cannot (?!) do this:
    > HisClass *hisObject[array_size];
    >
    > Neither can I use "new". I have to use "CreateObject()" to allocate
    > space. How should I do that?



    You can use new, because you are allocating pointers, not objects.
    When you have your array of pointers, use CreateObject tohave each
    pointer point to a valid object.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 11, 2005
    #12
  13. <> wrote in message
    news:...
    > Thanks. But there is some problem.
    >
    > "hisObject" is defined in the class definition, and "array_size" is
    > initialized in the constructor, so I cannot (?!) do this:
    > HisClass *hisObject[array_size];
    >
    > Neither can I use "new". I have to use "CreateObject()" to allocate
    > space. How should I do that?



    You can use new, because you are allocating pointers, not objects.
    When you have your array of pointers, use CreateObject tohave each
    pointer point to a valid object.

    hth
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 11, 2005
    #13
  14. David White Guest

    <> wrote in message
    news:...
    > Great, it works now. I didn't notice it was creating an array of
    > pointers.
    >
    > I tried something else, which did not work,


    In what way did it not work?

    > and I hope somebody could
    > point out why:
    >
    > p = new HisClass *[array_size];


    Where is your definition of 'p'? You need to show all relevant code.

    > for (int i = 0; i < array_size; i++)
    > {
    > p = CreateObject(...);
    > }
    >
    > hisObject = p;
    >
    > where "hisObject" was defined in the class as
    > HisClass **hisObject;
    >
    > Shouldn't this be the same as what David or Jack suggested?


    On the face of it, yes. You need to show all relevant code and explain why
    you believe there's a problem.

    DW
     
    David White, Jul 11, 2005
    #14
  15. Guest

    Here is the relevant code I should have provided, and I also noticed
    some mistake in my previous post. Sorry about that.

    I have a class like this:

    class MyClass{
    int array_size;
    HisClass **hisObject;
    };

    And I want to declare hisObject using the library provided function
    "CreateObject(...)".

    Here is my question: Aren't these 2 approaches the same? I get seg
    fault for Approach 1:

    // Approach 1
    HisClass *(p[array_size]);
    for (int i = 0; i < array_size; i++)
    {
    p = CreateObject(...);
    }
    hisObject = p;

    // Approach 2
    hisObject = new HisClass *[array_size];
    for (int i = 0; i < array_size; i++)
    {
    hisObject = CreateObject(...);
    }

    Thanks.
     
    , Jul 11, 2005
    #15
  16. "" wrote:
    >
    > Here is my question: Aren't these 2 approaches the same?


    No they are not.
    In 1) the array is allocated 'auto'. Its lifetime is controlled
    be the enclosing scope. When that scope ends, so does that array.

    In 2) the array is allocated on the free store. You have full control
    over when the array gets deleted (and have to do so by yourself) by
    actually deleting it.

    > I get seg
    > fault for Approach 1:
    >
    > // Approach 1
    > HisClass *(p[array_size]);
    > for (int i = 0; i < array_size; i++)
    > {
    > p = CreateObject(...);
    > }
    > hisObject = p;
    >
    > // Approach 2
    > hisObject = new HisClass *[array_size];
    > for (int i = 0; i < array_size; i++)
    > {
    > hisObject = CreateObject(...);
    > }
    >
    > Thanks.



    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jul 11, 2005
    #16
  17. Mike Smith Guest

    wrote:
    > Hi,
    >
    > class MyClass{
    > int array_size;
    > HisClass **hisObject;
    > };
    >
    > I want hisObject to point to an array of HisClass objects.
    >
    > The size of the array is given by array_size.
    >
    > My question is, if objects of type HisClass must be declared by the
    > library provided function CreateObject(), and cannot be declared by
    > using "new", how should I declare hisObject?


    What is the declaration of CreateObject()?

    If it is:

    HisClass *CreateObject(...)

    Then I suspect that what you want is something like:

    MyClass::MyClass(int size) : array_size(size)
    {
    hisObject = new HisClass *[array_size];
    for (int i = 0; i < array_size; ++i)
    hisObject = CreateObject(...)
    }

    But, as indicated elsewhere, perhaps a vector of HisClass *, or even a
    vector of smart pointers to HisClass, might be preferable.

    --
    Mike Smith
     
    Mike Smith, Jul 11, 2005
    #17
    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. Philipp
    Replies:
    5
    Views:
    6,541
    Ron Natalie
    Oct 16, 2003
  2. Replies:
    7
    Views:
    1,442
    Jakob Bieling
    Jul 21, 2005
  3. 7stud
    Replies:
    11
    Views:
    755
    Dennis Lee Bieber
    Mar 20, 2007
  4. Replies:
    5
    Views:
    663
  5. Secondpost

    Dynamically declare objects in javascript

    Secondpost, Nov 10, 2003, in forum: Javascript
    Replies:
    2
    Views:
    121
    Secondpost
    Nov 11, 2003
Loading...

Share This Page