Alloc Struct

Discussion in 'C++' started by Brian C, Jul 8, 2007.

  1. Brian C

    Brian C Guest

    Hello all,
    I was going through various online C++ tutorials trying to possibly
    pick up some things I do not know about already.
    One had a question on it, along the lines of:

    Q. Which of the following lines allocates a struct:

    1. int *p=new int;
    2. *p=1;
    3. int *q=new int(2);
    4. Range r=new Range(2,4);

    I didn't finish this tutorial/quiz as I found some of the wording on
    other questions a bit vague so I didn't get the answers.
    I assume it is choice 4, but I never heard creating a new instance
    "allocating a struct".
    BTW, I know it looks like homework, but believe me, it isn't. I haven't
    had homework to do in years.
    Brian C, Jul 8, 2007
    #1
    1. Advertising

  2. On 2007-07-08 22:31, Brian C wrote:
    > Hello all,
    > I was going through various online C++ tutorials trying to possibly
    > pick up some things I do not know about already.
    > One had a question on it, along the lines of:
    >
    > Q. Which of the following lines allocates a struct:
    >
    > 1. int *p=new int;
    > 2. *p=1;
    > 3. int *q=new int(2);
    > 4. Range r=new Range(2,4);
    >
    > I didn't finish this tutorial/quiz as I found some of the wording on
    > other questions a bit vague so I didn't get the answers.
    > I assume it is choice 4, but I never heard creating a new instance
    > "allocating a struct".
    > BTW, I know it looks like homework, but believe me, it isn't. I haven't
    > had homework to do in years.


    It's quite bad language, first memory is allocated for the struct
    (assuming Range is a struct), then the struct is created (instantiated)
    on that memory address. In C++, unlike C, you don't generally just
    allocate memory, usually you also create one or more objects in it.

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jul 8, 2007
    #2
    1. Advertising

  3. Brian C

    Ron Natalie Guest

    Brian C wrote:
    > Hello all,
    > I was going through various online C++ tutorials trying to possibly
    > pick up some things I do not know about already.
    > One had a question on it, along the lines of:
    >
    > Q. Which of the following lines allocates a struct:
    >
    > 1. int *p=new int;
    > 2. *p=1;
    > 3. int *q=new int(2);
    > 4. Range r=new Range(2,4);
    >
    > I didn't finish this tutorial/quiz as I found some of the wording on
    > other questions a bit vague so I didn't get the answers.
    > I assume it is choice 4, but I never heard creating a new instance
    > "allocating a struct".
    > BTW, I know it looks like homework, but believe me, it isn't. I
    > haven't had homework to do in years.


    All the answers are wrong.

    1 and 3 allocate a single int and initialize a pointer from that.
    2 sets *p (whatever p is) to 1, doesn't allcoate anything.

    4 is highly suspect. "new Range" returns Range*. Unless
    Range happens to be a class type with a constructor that takes
    a pointer to itself (unlikely) it is ill-formed.

    Looks like someone smokes too much Java before attempting C++.
    Ron Natalie, Jul 8, 2007
    #3
  4. Brian C

    Brian C Guest

    Ron Natalie wrote:
    > Brian C wrote:
    > All the answers are wrong.
    >
    > 1 and 3 allocate a single int and initialize a pointer from that.
    > 2 sets *p (whatever p is) to 1, doesn't allcoate anything.
    >
    > 4 is highly suspect. "new Range" returns Range*. Unless
    > Range happens to be a class type with a constructor that takes
    > a pointer to itself (unlikely) it is ill-formed.
    >
    > Looks like someone smokes too much Java before attempting C++.


    Erik/Ron,
    Thanks for the comments. I knew the only possible answer, depending on
    where the person/people who came up with the site were thinking would be 4.
    Thanks again, was bugging me all afternoon.
    Brian C, Jul 8, 2007
    #4
  5. Brian C

    James Kanze Guest

    On Jul 8, 10:54 pm, Erik Wikström <> wrote:
    > On 2007-07-08 22:31, Brian C wrote:


    > > I was going through various online C++ tutorials trying to possibly
    > > pick up some things I do not know about already.
    > > One had a question on it, along the lines of:


    > > Q. Which of the following lines allocates a struct:


    > > 1. int *p=new int;
    > > 2. *p=1;
    > > 3. int *q=new int(2);
    > > 4. Range r=new Range(2,4);


    > > I didn't finish this tutorial/quiz as I found some of the wording on
    > > other questions a bit vague so I didn't get the answers.
    > > I assume it is choice 4, but I never heard creating a new instance
    > > "allocating a struct".
    > > BTW, I know it looks like homework, but believe me, it isn't. I haven't
    > > had homework to do in years.


    > It's quite bad language, first memory is allocated for the struct
    > (assuming Range is a struct), then the struct is created (instantiated)
    > on that memory address. In C++, unlike C, you don't generally just
    > allocate memory, usually you also create one or more objects in it.


    And in C++, you don't generally have struct's. (Depending on
    context, a "struct" is a class declared using the keyword
    struct, or a POD struct. Or maybe just an agglomerate. In all
    cases, it is a non-union class type, and one cannot know whether
    it is a "struct" without seeing the definition.)

    As you say, it's quite bad language.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jul 9, 2007
    #5
    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. Bob
    Replies:
    1
    Views:
    607
    .NET Follower
    Feb 13, 2004
  2. aurgathor

    alloc 2d array with new

    aurgathor, Mar 12, 2005, in forum: C++
    Replies:
    4
    Views:
    7,021
    Axter
    Mar 13, 2005
  3. Adam Hartshorne

    Heap Alloc Exception Problem

    Adam Hartshorne, Jun 2, 2005, in forum: C++
    Replies:
    6
    Views:
    572
    Peter Koch Larsen
    Jun 2, 2005
  4. B. Gandhi

    Memory Alloc/Dealloc query

    B. Gandhi, Sep 23, 2003, in forum: C Programming
    Replies:
    6
    Views:
    597
    Peter Shaggy Haywood
    Sep 25, 2003
  5. Chris Fogelklou
    Replies:
    36
    Views:
    1,374
    Chris Fogelklou
    Apr 20, 2004
Loading...

Share This Page