Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ dynamically allocated memory

I don't quite get the point of dynamically allocated memory and I am hoping you guys can make things clearer for me.

First of all, every time we allocate memory we simply get a pointer to that memory.

int * dynInt = new int;

So what is the difference between doing what I did above and:

int someInt;
int* dynInt = &someInt;

As I understand, in both cases memory is allocated for an int, and we get a pointer to that memory.

So what's the difference between the two. When is one method preferred to the other.

Further more why do I need to free up memory with

delete dynInt;

in the first case, but not in the second case.

My guesses are:

  1. When dynamically allocating memory for an object, the object doesn't get initialized while if you do something like in the second case, the object get's initialized. If this is the only difference, is there a any motivation behind this apart from the fact that dynamically allocating memory is faster.

  2. The reason we don't need to use delete for the second case is because the fact that the object was initialized creates some kind of an automatic destruction routine.

Those are just guesses would love it if someone corrected me and clarified things for me.

like image 857
user1066113 Avatar asked Dec 14 '11 11:12

user1066113


People also ask

Does C supports dynamic memory allocation?

Dynamic allocation is not supported by C variables; there is no storage class “dynamic”, and there can never be a C variable whose value is stored in dynamically allocated space.

What is dynamic memory allocation in C with example?

Example. ptr = (float*) malloc(100 * sizeof(float)); The above statement allocates 400 bytes of memory. It's because the size of float is 4 bytes. And, the pointer ptr holds the address of the first byte in the allocated memory.

Why do we use dynamic memory allocation in C?

Dynamic memory allocation is the process of assigning the memory space during the execution time or the run time. Reasons and Advantage of allocating memory dynamically: When we do not know how much amount of memory would be needed for the program beforehand.

What are the types of dynamic memory allocation in C?

Dynamic Memory Allocation is a process in which we allocate or deallocate a block of memory during the run-time of a program. There are four functions malloc(), calloc(), realloc() and free() present in <stdlib.


1 Answers

The difference is in storage duration.

  • Objects with automatic storage duration are your "normal" objects that automatically go out of scope at the end of the block in which they're defined.

    Create them like int someInt;

    You may have heard of them as "stack objects", though I object to this terminology.

  • Objects with dynamic storage duration have something of a "manual" lifetime; you have to destroy them yourself with delete, and create them with the keyword new.

    You may have heard of them as "heap objects", though I object to this, too.

The use of pointers is actually not strictly relevant to either of them. You can have a pointer to an object of automatic storage duration (your second example), and you can have a pointer to an object of dynamic storage duration (your first example).

But it's rare that you'll want a pointer to an automatic object, because:

  1. you don't have one "by default";
  2. the object isn't going to last very long, so there's not a lot you can do with such a pointer.

By contrast, dynamic objects are often accessed through pointers, simply because the syntax comes close to enforcing it. new returns a pointer for you to use, you have to pass a pointer to delete, and (aside from using references) there's actually no other way to access the object. It lives "out there" in a cloud of dynamicness that's not sitting in the local scope.

Because of this, the usage of pointers is sometimes confused with the usage of dynamic storage, but in fact the former is not causally related to the latter.

like image 97
Lightness Races in Orbit Avatar answered Sep 21 '22 12:09

Lightness Races in Orbit