Alloc Struct

B

Brian C

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.
 
R

Ron Natalie

Brian said:
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++.
 
B

Brian C

Ron said:
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.
 
J

James Kanze

On 2007-07-08 22:31, Brian C wrote:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top