dynamic memory alloction question

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.
Last edited on
or ask me any question


what is the difference between these two:

1
2
int* p = new int[5];
int* p = new int(5);


That's the only interesting question I could think of. XD
First of all thanks for asking the question.

The first one declares a pointer array of 5 elements all type integer.

The second one assign the value 5 to pointer p.

Very good.

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 = new char[100];
  strcpy(mybuf,"This is a test");

  cout << mybuf;

  delete mybuf;
}
Q1. What is the difference between new and malloc ?

Q2. When you allocate a memory dynamically where is it allocated?
Last edited on
closed account (S6k9GNh0)
Alright I have a question myself. Every array starts with 0. MyArray[0]

So if I were to declare int* p = new int[5]; wouldn't that give 6 elements?

1
2
3
4
5
6
p[0]
p[1]
p[2]
p[3]
p[4]
p[5]


@ disch !

while printing you didn't use *

it should be

cout<<*mybuf

@ kevinchkin !

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.


Last edited on
p[5] is wrong , all the elements are used.
it will return a null pointer I think.


Actually, p[5] will return whatever happens to be there, garbage, invalid data, etc...
while printing you didn't use *


*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).
Last edited on
sorry Disch !

I get it
The error occurs when you delete .you didn;t include [] while deleting.


it should be like this

 
delete mybuf[];


thanks this helps me alot

anymore questions ?


Last edited on
delete mybuf[];
Wrong

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.
Last edited on
Ok sorry kevinchkin !

1
2
3
4
5
6
7
8
9
void testquestion()
{
  char* mybuf = new char[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.

I think it will give runtime error.






Last edited on
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.

Hope this helps !

thanks for the help .

I am reading the tutorial here again.
Last edited on
Topic archived. No new replies allowed.