Dynamic Allocation

Can anyone please tell me what dynamic allocation means and how do we use it ??
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.
Last edited on
Can anyone please give me a small example of a program which has bobby=new int ;
I will be thankful.
1
2
3
int *bobby = new int;
*bobby = 123;
cout << *bobby;
can you please tell me what is the difference between

1
2
3
int *bobby = new int;
*bobby = 123;
cout << *bobby;

and

1
2
3
int a;
a=123;
cout<<a;
Last edited on
In first case, as you already know, memory has been allocated and pointer bobby points to the address location of 4 bytes (since int).


bobby - - - - - - - >123 (here 123 is the value stored in the address that pointer bobby is pointing to)

And in the second case you have a static memory allocated for a variable a. That means whether you use that variable or not
it will consume 4 bytes.

|-------------------------------------|
| 123 |
|-------------------------------------|
a


I hope this helps little bit
Last edited on
can you tell me what is wrong with this program.

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;
int main()
{ int a;
	
	int *n=new int[a];
	cout<<"how many numbers you want to enter";
	cin>>a;
	cout<<n[a];
	return 0;
}

Last edited on
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 = new int [a];  // array of 2 elements, since a = 2; 
---
delete [] n; // since array allocated 
return 0; 
}

 



And yeah, depending on the compiler, you might also see a garbage value or 0 at cout<<n[a];. Since, of-course, you did not gave any value to it.
Last edited on
But I learnt that memory is allocated according to user give data .
Except you're allocating the array before initializing a:
1
2
3
4
int a; //a is uninitialized.
int *n=new int[a]; //<--Error! a is still uninitialized at this point.
//...
cin>>a;
I don't know what you mean when you say "memory is allocated according to user give data"

I would suggest you should refer to Mythios's reply above.
Can you tell me what is the use of operator new here .It should determine the value of a during the runtime from the user.
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;
Last edited on
Can you please give me a simple example of using
 
*pointer=new int []
1
2
3
4
5
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=new int[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>
using namespace std;
int main()
{
	int *mypointer=new int[4];
	*mypointer[4]={1,2,3,4};
	for (int a=0;a<4;a++)
	{
		cout<<*mypointer[a]<<"  ";
	}
	return 0;
}
Last edited on
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]<<"  ";
//... 
Topic archived. No new replies allowed.