-10
int a[10];

The above code will create a array of four int variable sizes & thus the programme will be able to store only 4 integers. Now consider the following commands

int *a,*b,*c,*d;
a= (int *)malloc(sizeof(int));
b= (int *)malloc(sizeof(int));
c= (int *)malloc(sizeof(int));
d= (int *)malloc(sizeof(int));

The above part of code will create four int type pointer & will allocate them memory of int size. I learnt that dynamic memory allocation allocates memory at rum time. I want to know that irrespective of using array or malloc(dynamic memory allocation), the user will be getting only four int sized space to store.If we rule out that it is a pointer variable with int size memory, then what will be the use of dynamic memory allocation.In both cases , the user will get only four int spaces & to get more he will need to access the source code.So why do we use malloc or dynamic memory allocation ?

Brijesh Roy
  • 33
  • 2
  • 6
  • 4
    I don't see the relation between `10` and "able to store only 4 integers". – Kerrek SB Jan 21 '17 at 14:35
  • 1
    You can change the amount of dynamically allocated memory _at run time_. – ForceBru Jan 21 '17 at 14:35
  • 3
    There's just a lot of conception to clean up here. I reommend you check out [The definitive C++ book guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – WhiZTiM Jan 21 '17 at 14:36
  • 1
    In the first case, the lifetime of the integers is equal to that of the variable `a`, which essentially extends until the end of the variable's declarative region. In the latter case, the lifetime of the integers is controlled entirely by the user (i.e. the programmer). – Kerrek SB Jan 21 '17 at 14:37
  • `a= (int*) malloc(10 * sizeof(int));` – Cherubim Jan 21 '17 at 14:37
  • 2
    So is the question for C or C++? Please tag accordingly, instead of both. – tambre Jan 21 '17 at 14:39
  • See also: http://stackoverflow.com/a/7284907/596781 – Kerrek SB Jan 21 '17 at 15:22
  • 1
    This is tagged as C++, so why on earth are you using `malloc`? (and C style casts) At the *very least* you probably want `new`, but more likely you want `std::vector`. Interpreted as modern C++ this is absolutely horrible code. – Jesper Juhl Jan 21 '17 at 15:23

4 Answers4

1

Consider

int a,*b;
cin >> a;
b= (int *)malloc(a*sizeof(int));

The user types a number a and gets a ints. The number a is not known to either to programmer or the compiler here.

As pointed out in the comments, this is still bad style in C++, use std::vector if possible. Even new is still better than malloc. But i hope the (bad) example helps to clarify the basic idea behind dynamic memory allocation.

Anedar
  • 4,235
  • 1
  • 23
  • 41
0
int a[10];

The above line of code will allocate an array of 10 int's of automatic storage duration, if it was within a local scope.

int *a,*b,*c,*d;

The above, however, will allocate 4 pointers to int also of automatic storage duration, likewise if it was within a local scope.

a= (int *)malloc(sizeof(int));
b= (int *)malloc(sizeof(int));
c= (int *)malloc(sizeof(int));
d= (int *)malloc(sizeof(int));

And finally, the above will allocate int variable per each pointer dynamically. So, every pointer of the above will be pointing to a single int variable.

Do note that dynamically allocated memory can be freed and resized at runtime unlike static memory allocation. Memory of automatic storage duration are freed when run out of scope, but cannot be resized.

machine_1
  • 4,266
  • 2
  • 21
  • 42
0

You're right that it's all just memory. But there is a difference in usage.

In the general case, you don't necessarily know ahead of time the amount of memory you will need and then time when such memory can be safely released. malloc and its friends are written so that they can keep track of memory used this way.

But in many special cases, you happen to know ahead of time how much memory you will need and when you will stop needing it. For example, you know you need a single integer to act as a loop counter when running a simple loop and you'll be done with it once the loop has finished executing. While malloc and its friends can still work for you here, local variables are simpler, less error prone and will likely be more efficient.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
-1

If you program in C, casting the result of malloc is unnecessary. I suggest you to read this: Do I cast the result of malloc?

Then what your doing in your code with the 4 pointers is unnecessary; in fact you can just allocate an array of 4 int with one malloc:

int *a;
a = malloc(4 * sizeof(int));
Community
  • 1
  • 1
dems98
  • 814
  • 3
  • 9
  • 22