473,473 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Alloc Struct

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.
Jul 8 '07 #1
4 2069
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
Jul 8 '07 #2
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++.
Jul 8 '07 #3
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.
Jul 8 '07 #4
On Jul 8, 10:54 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
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:ja*********@gmail.com
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

Jul 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aurgathor | last post by:
I'm trying to allocate a 2d array in runtime that can subsequently be accessed like arr, to no avail. Is there any to do that in C++? If yes, how? TIA This is what I got thus far: ...
6
by: Adam Hartshorne | last post by:
Hi All, I have the strangest problem, and at an end to try and explain it/know what to do. I have a program written in c++ using visual studio 7.1. It was all working no problems, then after...
6
by: B. Gandhi | last post by:
Hi friends I have a query regarding memory allocation deallocation in C. It is as follows: char *path = getenv("PATH"); In this call to getenv(), where does the returned string stored?...
7
by: sNOiSPAMt | last post by:
7.20.3#1 If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value,...
10
by: Ryan Wang | last post by:
Hi, here is the function: int sum(int a,int b) { int c; c = a + b; return c; }
3
by: -DG- | last post by:
I'm still trying to figure out some of the nuances of access to legacy Win32 DLLs. I need to alloc buffers to be used by the Win32 DLLs. I know that pinning a managed pointer can lead to...
2
by: cnliou | last post by:
Linux 2.4.23 on AMD 450MHz. pgsql 7.4.1 configure --enable-multibyte=UNICODE locale is zh_TW.Big5 IIRC, when I did "initdb -E UNICODE" or "createdb db1", I saw the following message from...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
4
by: JDHawk | last post by:
Hi, when I compile my c++ program, MS Visual C++ (2003) shows this error: Damage: after Normal Block (#139) at 0xXXXXXXXX In Debug Mode I found out that it is the deallocation with "delete" in...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.