Board index » cppbuilder » new keyword vs. int*

new keyword vs. int*


2005-07-01 09:02:53 PM
cppbuilder45
When I want to create MyClass I use this syntax:
MyClass * mc = new MyClass();
What about int*? Should I do this or what:
int * i = new int();
or
int * i = new int[1];
 
 

Re:new keyword vs. int*

"Petar Popara" <none>wrote:
Quote
When I want to create MyClass I use this syntax:

MyClass * mc = new MyClass();

What about int*? Should I do this or what:
[I'm going to assume you meant what you typed - int*, not int]
Quote
int * i = new int();
That's legal, but doesn't do what you are asking for.
It allocates an int, and returns a pointer to it, which you place in a
pointer. It does not create an int*.
Quote
or

int * i = new int[1];
That's also legal, but also doesn't do what you are asking for.
It allocates an array of int, and returns a pointer to the first
element. It also doesn't create an int*.
(You also need to use "delete [] i;" when getting rid of it.)
If you wish to allocate a pointer to int on the heap, then that's what
you ask for:
int ** i = new int*();
You'll notice that you store the result in an int** - in other words, a
pointer to a pointer.
Exactly the same would apply to creating a MyClass*, by the way. The
same syntax applies in both cases.
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
 

Re:new keyword vs. int*

Petar Popara <none>wrote:
Quote
When I want to create MyClass I use this syntax:

MyClass * mc = new MyClass();

What about int*? Should I do this or what:

int * i = new int();

or

int * i = new int[1];
I'm not sure what you want to do. Are you
perhaps coming from a scripting language
where everything must be 'new'ed? Are you
aware of automatic objects?
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

{smallsort}

Re:new keyword vs. int*

"Petar Popara" <none>wrote in message
Quote

When I want to create MyClass I use this syntax:

MyClass * mc = new MyClass();

What about int*? Should I do this or what:

int * i = new int();

or

int * i = new int[1];
Well, the question is, what did you _mean_? As Alan said, you wrote "int*",
implying that you want to create a pointer, but your code suggests that you
really want an 'int'. So I won't duplicate what Alan said, but if all you
want is a single 'int', you don't need a pointer and you don't need to
"create" it at all. As Hendrik said, you can use an automatic variable:
int i; // set aside space for a single integer
i = 123; // assign a value to the integer
- Dennis
 

Re:new keyword vs. int*

"Dennis Jones" < XXXX@XXXXX.COM >wrote in message news:42c5fed3$ XXXX@XXXXX.COM ...
|
| "Petar Popara" <none>wrote in message
| news: XXXX@XXXXX.COM ...
|>
|>When I want to create MyClass I use this syntax:
|>
|>MyClass * mc = new MyClass();
|>
|>What about int*? Should I do this or what:
|>
|>int * i = new int();
|>
|>or
|>
|>int * i = new int[1];
|
| Well, the question is, what did you _mean_? As Alan said, you wrote "int*",
| implying that you want to create a pointer, but your code suggests that you
| really want an 'int'. So I won't duplicate what Alan said, but if all you
| want is a single 'int', you don't need a pointer and you don't need to
| "create" it at all. As Hendrik said, you can use an automatic variable:
|
| int i; // set aside space for a single integer
| i = 123; // assign a value to the integer
And just to be complete, it is always better to initialise
a variable at the point of it coming into existence:
int i = 0;
Or...
int i( 0 );
Cheers,
Chris Val
 

Re:new keyword vs. int*

I'm porting some code from java and I need to define "int*" (in Java:
"Integer" class) instead of int, so I'll use this:
int * i = new int();
Thanky you all.
 

Re:new keyword vs. int*

"Petar Popara" <none>writes:
Quote
I'm porting some code from java and I need to define "int*" (in
Java: "Integer" class) instead of int, so I'll use this:

int * i = new int();
But make sure that you first check if
int i(0);
doesn't work. Java is very restricted when it comes to object
lifetimes, while C++ has a wealth of them. Automatic lifetime is
usually to be prefered if it does the task, because it is by far the
easiest to deal with.
 

Re:new keyword vs. int*

Thomas Maeder [TeamB] < XXXX@XXXXX.COM >wrote:
Quote
"Petar Popara" <none>writes:

>I'm porting some code from java and I need to define "int*" (in
>Java: "Integer" class) instead of int, so I'll use this:
>
>int * i = new int();

But make sure that you first check if

int i(0);

doesn't work. Java is very restricted when it comes to object
lifetimes, while C++ has a wealth of them. Automatic lifetime is
usually to be prefered if it does the task, because it is by far the
easiest to deal with.
Also, note that you can always get an 'int*'
from an 'int':
int i = 0;
int* pi = &i;
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:new keyword vs. int*

"Hendrik Schober" < XXXX@XXXXX.COM >writes:
Quote
Also, note that you can always get an 'int*'
from an 'int':

int i = 0;
int* pi = &i;
Almost always. Counter-example:
int * x = &123;
:)
--
Chris (TeamB);