0

I dont get why do you have to do both. Isnt malloc creating dynamic memory for you? Then why do we have to state for example "int " in the beginning when later i will be mallocing that variable. Im new to malloc, sorry if this question has an obvious answer.

Example:

In the main :

int *p;

Then later in the function:

int *p = malloc(1000 * sizeof(int));
user3753834
  • 277
  • 2
  • 5
  • 11
  • Malloc is used with pointers because they point (as the name says) to some object in memory. To store some new value in a pointer you have to allocate(reserve) memory for it. – Igor Pejic Aug 08 '14 at 14:33
  • `malloc` is often used for arrays of unknown size – Drew McGowen Aug 08 '14 at 14:36
  • But why do I have two places in memory for this int? Once in the int i decleared before and then in the malloc – user3753834 Aug 08 '14 at 14:36
  • You cannot malloc a variable, because a variable has a name. Always clearly differentiate between object, pointer to object, and (optional) name of object. – Deduplicator Aug 08 '14 at 14:47
  • The code in the question does not compile. You have two declarations of `p` which is one too many. You are making life needlessly hard for yourself by only revealing small excerpts of your code. If you could manage to show a complete function then our lives would be easier. – David Heffernan Aug 08 '14 at 14:54

4 Answers4

3

Isnt malloc creating dynamic memory for you?

It does. However, you need to be able to hold the address of that memory somewhere, too.

int *ptr = malloc(1000 * sizeof(int));
...
free(ptr); // Once you are done, you need to release the memory by calling free

The address is stored in a pointer ptr, which needs a small amount of memory to be stored. You use that pointer to reference the memory that you have allocated.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • It should be noted that `free` should be called on `ptr` to prevent a memory leak. – Fiddling Bits Aug 08 '14 at 14:39
  • So, basically the int declared in the beginning holds the little memory that the pointer requires. – user3753834 Aug 08 '14 at 14:40
  • It holds the memory required for the pointer to be stored somewhere. – Igor Pejic Aug 08 '14 at 14:41
  • 3
    @user3753834 `ptr` is not an `int`, it's `int*`. Asterisk denotes a pointer, while `int` provides the type of the element pointed to by that pointer. Pointers take a small amount of memory. That size is independent of the size of the memory chunk to which they point. A decent analogy would be locating a passage in a book: no matter how big is the book, a little space needed to write down the page, the line, and the character number is sufficient to record the location of any passage. A pointer could also be considered a very short "passage", so it needs some space to be stored. – Sergey Kalinichenko Aug 08 '14 at 14:41
  • I made an edit to the question above, to make it more clear. Those were the two ints i was refering too. What i was asking , why do i need two pointer ints that can be seen in main and in the other function. – user3753834 Aug 08 '14 at 14:52
  • @user3753834 The first `int *p` is a declaration of a pointer without initialization. It allocates space for the pointer, but the pointer is not pointing to anything. The second declaration has an initialization. Note that making both these declarations in the same function will not compile, because the name `p` is re-declared. – Sergey Kalinichenko Aug 08 '14 at 14:54
1

There is a big difference between the following two definitions:

int i;
int *pI;

i an int. It has a memory location that you can write a value to.

pI, however, is not an int. It is a pointer to an int. It's value is an address. You cannot write a value to the memory location it's pointing to until you point it to a valid memory location big enough to hold an int. For example:

pI = &i;
*pI = 10;

You can create a generic pointer by using the keyword void but cannot dereference a void pointer. The compiler needs to know the date type in order to dereference a pointer.

int i;
void *pV;

pV = &i;
*(int *)pV = 10;
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
0

If you declare int *p in main and again in a function you've then got two pointers with different scopes, p in function is only in scope when function is entered and becomes irrelevant when the function returns unless your function returns the address of the function's p to main.

  • If the address of the function's `p` is returned, it has to be `static` or dereferencing it is invalid. – Fiddling Bits Aug 10 '14 at 11:50
  • 1
    True the pointer is on the stack and the malloc in the function is on the heap, but pointer will be invalid on function return, that's why I don't declare pointers in functions, pass them to functions instead, safer working with arrays. – crypticfool Aug 10 '14 at 23:08
-1

In the specific case of an int you probably do not want to create memory space dynamically. You only would want to do this when you do not know the worst case scenario of you memory usage.

It would be a different story entirely if you would create a int pointer. More information can be found in this post

Community
  • 1
  • 1
Funonly
  • 283
  • 1
  • 13
  • It's possible to declare a structure or array without `malloc`. Furthermore, it's just as valid to use `malloc` for a primitive type. – Drew McGowen Aug 08 '14 at 14:47