Hello ,
I have a test about dynamic memory alloction and I have studied about it.Can someone give me a program that uses dynamic memory or ask me any question .I will be thankful.
You have the right idea, but your wording is a little strange. The second one allocates one int and assigns a value of 5 to that int (not to the pointer itself, but to what the pointer points to). But like I say it looks lik you understand it -- I'm just playing semantics.
What's the problem with the following code:
1 2 3 4 5 6 7 8 9
void testquestion()
{
char* mybuf = newchar[100];
strcpy(mybuf,"This is a test");
cout << mybuf;
delete mybuf;
}
new is used in c++
and malloc is used in C language.
I haven't studied C so I dont know alot about malloc.
ans 2: when we allocate memory dynamically ,it is stored in stack memory.When we use recursion we have to give it a condition otherwise there will be stack flow.
@ computerquip !
p[5] is wrong , all the elements are used.
it will return a null pointer I think.
*makes incorrect buzzer noise*
The cout line is fine.
1 2
cout << *mybuf; // prints 'T'
cout << mybuf; // prints "This is a test"
Another guess? ;D
ans 2: when we allocate memory dynamically ,it is stored in stack memory
*makes incorrect buzzer noise*
it will return a null pointer I think.
It's actually worse than that... it's an out-of-bounds error, or "buffer overflow" (which leads to RAM corruption, which leads to segfaults and other really nasty and really hard to find bugs).
new is used in c++ and malloc is used in C language.
While this answer is valid but this is not I was looking for. Read more about 'new' and you'll know the difference between the two.
What about other answers on @Disch's post for which he said incorrect answer?
The error occurs when you delete
Elaborate this. What error occurs when you delete?
anymore questions ?
Go little bit easy there tiger. Before new questions I would suggest answer all the questions which you answered incorrectly, mentioned by Disch in his post. And also answer those from this post.
void testquestion()
{
char* mybuf = newchar[100];
strcpy(mybuf,"This is a test");
cout << mybuf;
delete mybuf;
}
the delete mybuf will delete only the first element of the array it should be like this delete mybuf[100]
so this is what is wrong with this program.
For the other question , I know about new but not about malloc but I have heard the name malloc, I cannot tell the difference because i dont about malloc which is not in c++.
and about the other quesiton there are two kind of memories one is called heap and the other is stack.heap is static and stack is dynamic.when we assign value to stack we should delete it too so we dont run out of memory.
It is still wrong. All right now I'll tell you what is wrong.
For deleting array of allocated memory, syntax is
delete [] myBuf;
Difference between new and malloc is new calls constructor when you create a object and calls destructor when you delete the object. While malloc does not call constructor. So if the data member of the class, whose object you just created using new, allocates some memory which you free in destructor. When you'll call free for object you created using malloc(), the memory allocated by data member of that object will not be freed because destructor is not called. Hence memory leak.
And very important thing, for dynamic allocation you assign memory on heap and not stack.
I would suggest re-read about dynamic allocation. As understanding one small topic completely is way better than reading 100 topics and not understanding them.