What is new int ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is new int ?

int *x; x=new int[5];

29th Jul 2018, 7:23 PM
Gaurav Sahadev
Gaurav Sahadev - avatar
5 Answers
+ 3
Here you define an array that has space to store 5 integers. 'new' is a keyword that creates an object and allocates memory for it.
29th Jul 2018, 7:35 PM
Steppenwolf
Steppenwolf - avatar
+ 6
variableType variableName= new VariableType[arraySize] string *x=new string[8] char *v=new char[66]; as Steppenwolf said.. an array of ints
29th Jul 2018, 8:22 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 3
int *x is a pointer to a spot in memory that will hold an integer. When you use 'new int[5]' you are telling the system to allocate memory for five integers, and by assigning it to x, you are giving the address of those five integers to the pointer. Just remember to deallocate that memory when you're done using it with: delete [ ] x;
30th Jul 2018, 3:08 AM
Zeke Williams
Zeke Williams - avatar
+ 1
Gaurav Sahadev , new is the keyword to allocate memory dynamically... Memory is divided into stack and heap.. In stack, it is static memory and if you do int a or int a[5], static memory is allocated which in turn automatically gets free by end of your code scope... once you do new int[5], memory from heap is allocated, which you should free using delete operator... On stack, for your case, only pointer pointing to your array of 5 int is allocated... that pointer is free automatically at end of scope...
30th Jul 2018, 3:03 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Thank you very much for explaining this🙂 Ketan Lalcheta Steppenwolf ᠌᠌brains
30th Jul 2018, 3:06 AM
Gaurav Sahadev
Gaurav Sahadev - avatar