C++ Pointers question

Brushing up on my C++ by working through some C+++ test questions.

Could someone elaborate on the differences in behaviour of the following pointer declarations:

1) SomeClass *psc1 = new SomeClass();
2) SomeClass *psc2 = new SomeClass;

Thanks
Last edited on
What kind of difference are you talking about? about the missing semi-colon at the end? that won't compile anyway till you put it back... other than that they're both exactly the same with different pointers names.
I don't see any difference either, besides the first variable ending with '1', the second ending with '2', and the missing semicolon.

The only wrong thing that can happen is that while allocating memory for the first class, you filled your entire PC's Ram and you have nomore memory for the second class.
Hello guys - I've edited the original posting, i.e. what's the difference in behaviour between the following,

1) SomeClass *psc1 = new SomeClass();
2) SomeClass *psc2 = new SomeClass;
i believe nothing...

both will use the default constructor to create it
There is no difference. They are identical.

Those empty parenthesis are optional when using new.

EDIT:

Actually the above is true only for complex types, not for built-in types. To be more precise:

1
2
3
4
5
6
7
// These two are identical
SomeClass *psc1 = new SomeClass();
SomeClass *psc1 = new SomeClass;

// These two have a subtle difference:
int* i1 = new int();  // makes a new int, initialized with "0"
int* i2 = new int;   // makes a new int, but the int is uninitialized 
Last edited on
OK - I thought they would both behave in an identical manner. However in the test questions - the following set of responses were offered (I believe the correct choice to be b).

a) The first line declares a pointer and allocates memory for a SomeClass object and calls default class constructor; the second line declares a pointer but does not allocate memory for an object.

b) They both declare a pointer to a SomeClass object. The first line returns NULL if the system runs out of memory; the second throws an exception.

c) The first line is correct; the second line generates a complier error on build.

d) Both lines declare a pointer to a SomeClass object. The first line class the SomeClass constructor; the second line initialises all member variables or 0 or NULL

e) Both lines allocate memory for a SomeClass object, call the default constructor of that object, and return a pointer to that object.
I would say e... because practically there's not different at all between them.

b isn't right. They both would throw an exception, you just have to catch bad_alloc.
Last edited on
Gotcha - thanks.
It might also be d.

The parenthesis version may initialize all members to zero/null if there is no default constructor provided. To be honest I'm fuzzy on exactly how that works.

a, b, and c are definitely all wrong, though.

EDIT: although d mixed up the first and second versions. The second version (no parenthesis) will definitely NOT initialize any members to zero unless the class's default constructor explicitly does so.

EDIT2: after re-reading d more carefully, I realize that it is suggesting something other than I originally thought. And what it is suggesting is definitely not true.

So yeah. Go with E.
Last edited on
Thanks.
Topic archived. No new replies allowed.