views:

196

answers:

5
int *temp = new int[atoi(argv[3])];

what does the above statement mean ?Please explain in detail .

+1  A: 

It allocates a int array with elements of the 3 program argument.

for example: main.exe foo bar 100 will allocated a array with 100 integers.

Shay Erlichmen
A: 

you declare a pointer to a newly allocated array of integers with the size determined by the numeric value that the string argv[3] (i.e. the 3rd command line argument) has

frag
A: 

parses the string argv[3] into an int, then creates an array of ints of that size. Then it assigns the pointer to (the first element of) that array to temp.

CodeInChaos
+8  A: 

In Detail:

int* temp //declares a pointer to int
int* temp = //that points to
int* temp = new //newly allocated memory
int* temp = new int [ //of type array of int of size 
int* temp = new int [ atoi ( //equal to the integer representation (atoi -> alphabetical to integer)
int* temp = new int [ atoi ( argv[3] //of the third command line argument passed to your program.

That is if your third command line arg is "5", temp will point to a dynamically allocated array of 5 ints

Armen Tsirunyan
@Armen - abcdef actually asked for int *temp = new int[ atoi( argv[ 3 ] ) ];, you've explained int *temp = new int( atoi( argv[ 3 ] ) ); ( note the '(' and '[' after "new int" )
Kiril Kirov
@Kiril: edited, thank you
Armen Tsirunyan
You're welcome. +1 for your answer (:
Kiril Kirov
Actually, `int* temp /*...*/;` is a pointer _definition_, a declaration would be `extern int* temp;`. `<sigh>` [Almost everybody gets this wrong.](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632)
sbi
@sbi: As a matter of fact every definition is also a declaration :) So no error here :))
Armen Tsirunyan
@Armen: Yeah, right. And since we humans are primates, too, you surely wouldn't mind me calling you monkey names. Or wouldn't you? `:)`
sbi
@sbi - not at all if you said I was a primate. Not sure if you started calling me names :D
Armen Tsirunyan
@Armen: Anyway, if I spoke of the guy next to me as "the primate next to me", wouldn't you, looking at him, correct me and say he's a human?
sbi
@sbi: OK, in this example you win. What about this example? I give you a plate with apples and say: Take a fruit. Is that too ridiculous? I mean I still insist that there is nothing wrong in terms of terminology in my answer :)
Armen Tsirunyan
@Armen: There's a difference: You can eat as many apples or fruits you wish. There can, however, only ever be one definition, while you can repeat declarations like eating fruits. I just _had_ to come up with something... `:)`
sbi
@sbi: Do share what you're smoking :D You do have quite a healthy imagination, huh? :)
Armen Tsirunyan
@Armen: Never smoked anything in my life. `:)`
sbi
@sbi: as far as your last example is concerned... imagine a plate with different fruits and only one apple. The other fruits may or may not be the same. Now I give you THE apple and still say "Care for a fruit?" How's that? :) by the way we can just agree to a "draw"...
Armen Tsirunyan
@Armen: You're only suggesting a [draw](http://stackoverflow.com/questions/3953696/) because your losing the argument! `:)`
sbi
@sbi: I am? :) I thought I was winning, and decided to let you agree to a draw as a sign of good will :D
Armen Tsirunyan
As an independent adjudicator...I forgot what I was going to say, carry on.
CiscoIPPhone
@CiscolPPhone: thou shalt not scare us with frightful words like... what was that... *adjudicator*?!
Armen Tsirunyan
+3  A: 

Pointers? New? Not very C++ish...

#include <vector>
#include <boost/lexical_cast.hpp>

int main(int argc, char** argv)
{
    std::vector<int> temp(boost::lexical_cast<int>(argv[3]));
}
FredOverflow
I would argue that new is not C++ish :)
Armen Tsirunyan
@Armen: I'm not sure what you mean, but I would replace `new` with a proper container anytime.
FredOverflow
@Armen: That's what, I think, Fred was saying. (`+1` from me, BTW, for such a nice answer to such a stupid question.)
sbi
@FredOverflow, @sbi: OK, I see your point, fair enough. I was just thinking about smart pointers and thought rightfully that their use doesn't eliminate new, because ultimately you use new to create an object and then store it in a smart pointer. But if you meant containers, that's another story.
Armen Tsirunyan
@Armen: You don't need `new` if you use `make_shared`.
FredOverflow
@FredOverflow: Good point :)
Armen Tsirunyan