Why two different execution times?

Time taking operation was just the memory allocation. Memset doesn’t really matter. O(10^5) is very fast when we can do 10^8 operations in one second. It will take 0.001 sec.

1 Like

I got your point.
To Summarize,
In dynamic array i am reallocating for every " t ",hence its taking time and also clearing the whole dynamic array so my code is computing the values again and again

In Vector, i am not clearing it and moreover resizing is not clearing my vector so values are not blown off.

Am i right @ssjgz

No you need to free it explicitly in c++ using free(dp).
Otherwise there will be a memory leak.

That’s approximately right, yes :slight_smile:

Edit:

Wait - you never actually “clear” the array - you’re leaking memory all over the place - but each time you create a new one, you are initialising all of its elements to 0.

Edit2:

Oops - didn’t see @l_return’s post above. Although:

I think you mean delete[] dp :wink:

4 Likes

So you are saying that we when we especially solve dp problems we should reuse the memory and avoid not to reallocate for every " t "?

Right? @l_returns

Not only in dp. I have seen some cases when declaring variable inside the deepest loop can cause a huge difference in execution time and can cause TLE. So keep this in mind that relocation is costly.

2 Likes

okay i didn’t know about this function :stuck_out_tongue: Thanks
I had learnt these things for c and not c++ so :stuck_out_tongue:

2 Likes

I got your points @ssjgz @l_returns
Should i ask a silly doubt?
What is actually memory leak and cache friendly?

It means that some portion of memory is allocated by our program but we don’t have any reference ( a pointer ) pointing to it. So basically that memory is no more usable. None of the other program can use it since it already blocked by our program and hence we are wasting the memory. You need to restart the computer to recover memory.

2 Likes

The OS (correct me?) or the hardware usually caches the frequently executed part of the memory to fasten the execution time.
Cache Memory in Computer Organization - GeeksforGeeks.

2 Likes

On most Operating Systems, just shutting down the offending leaky program will free it up - though I’ve worked on platforms where you really did need to reboot the whole thing :confused:

5 Likes

Okay :slight_smile:

2 Likes

Thank You So So So Much @l_returns @ssjgz !!!
You both helped me a lot in clearing my doubt!!
You both are great people.

2 Likes

free() also works right?

P.S: This thread was very informative.

4 Likes

It might work (for arrays of primitive types, at least), but it would technically be Undefined Behaviour.

For arrays of types whose destructors are not no-ops, it would not only be Undefined Behaviour, but would also have observably-bad results - the destructors of the objects in the array would not be called.

4 Likes

Yes. I’ve been reading about this recently. And similarly malloc doesn’t call the constructor I believe?

Also, are global variables allocated in the heap? Or in the stack?

2 Likes

That’s correct - malloc and free are inherited from C, and they have no notion of C++ classes/ constructors/ destructors.

Neither - they generally have their own special area(s) in memory :slight_smile:

5 Likes

Can You elaborate instances when memory leak occurs?

I think neither any cp contest nor any company interview is going to ask you this.

1 Like

Yep. But this is all about the enthuse to lear something on a deeper level to understand how things work. And I feel that should be appreciated.

1 Like