Sir, I knew this.I wanted this explained in other words.
I will tell you what I have learnt about it and then please tell me If i am wrong.
Dynamic allocation is related to the space of memory that we use in our program.Sometime we dont how many numbers or character the user will input for a program so we use new operator which works according to the input that the user enters.which means it will work as different types of data like short, int , float ,double and everything.and then we use delete operator to delete that allocated memory in our program.We cannot use delete operator unless we use new operator. because we can delete the data only if we asign.
#include<iostream>
usingnamespace std;
int main()
{ int a;
int *n=newint[a];
cout<<"how many numbers you want to enter";
cin>>a;
cout<<n[a];
return 0;
}
Big problem, you are not initializing 'a' and trying to allocate an array of 'a' variables. BAD
If you want to allocate memory for 'a*sizeof(int)' then YOU MUST assign value of 'a' first. Because right now 'a' has garbage value, so you must provide a value to 'a'. Other than that you are not freeing the memory you allocated. You should delete the allocated memory.
So your updated code should be
1 2 3 4 5 6
int main() {
int a = 2; // initialized value of a
int *n = newint [a]; // array of 2 elements, since a = 2;
new merely allocates memory. It doesn't get user input. That's what std::cin is for. However, it's not enough to take input anywhere in the program. You need to take input before using the data it initializes:
1 2 3
int a;
std::cout <<a; //Wrong. a is uninitialized.
std::cin >>a;
1 2 3
int a;
std::cin >>a; //Right. This initializes a to a the user's input before being used by std::cout.
std::cout <<a;
int size;
//initialize size to something
type *pointer=new type[size];
//do something
delete[] pointer;
That's the basic skeleton. Now here's the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cstdlib>
#include <ctime>
int main(){
srand(time(0)); //ignore this
//Calling rand() returns a random value. This value can only be known at run
//time.
int size=rand()%10;
int *array=newint[size];
for (int a=0;a<size;a++)
array[a]=rand()%10;
for (int a=0;a<size-1;a++)
std::cout <<array[a];
delete[] array;
return 0;
}
Sorry sir , I am new for c++.
this is a complex program.
I didn't get these things.
1 2 3 4 5 6
#include <cstdlib>
#include <ctime>
srand(time(0)); //ignore this
//Calling rand() returns a random value. This value can only be known at run
//time.
int size=rand()%10
;
I applied the theory I learnt to the following program and its not working.
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
usingnamespace std;
int main()
{
int *mypointer=newint[4];
*mypointer[4]={1,2,3,4};
for (int a=0;a<4;a++)
{
cout<<*mypointer[a]<<" ";
}
return 0;
}
Those parts aren't the point of the example. The code as a whole should be understandable even if you ignore them.
Line 6: You can only use the syntax T array[n]={/*...*/}; for static arrays. You cannot use it to initialize arrays created with dynamic allocation.
Line 9: The extra * is a dereference. Notice that I didn't use one in my example on line 14.
What you want to do there, correctly written, would look like this:
1 2 3 4 5 6
//...
for (int a=0;a<4;a++)
mypointer[a]=a+1;
for (int a=0;a<4;a++)
std::cout <<mypointer[a]<<" ";
//...