0

I have a doubt in dynamic memory allocation (malloc) Say

ivar=(int*)malloc(1*sizeof(int));

What the above code will do? Will it create address for allocations?

Also which is the standard way to get values in malloc? (Say i as loop variable)

scanf("%d",&ivar[i]);

OR

scanf("%d",ivar+i); 
int main()
{

    int *ivar;
    ivar=(int*)malloc(1*sizeof(int));
    printf("%u",ivar); // outputs 2510
    printf("%u",&ivar);// outputs 65524

}  // please explain why it is…

Thanks in advance.

Theo
  • 57,719
  • 8
  • 24
  • 41
Hari Ram
  • 56
  • 5
  • 1
    `ivar` is a pointer and printing it with `%u` invokes undefined behavior. You must [use `%p`](https://stackoverflow.com/q/9053658/995714) instead. Also [don't cast the result of `malloc` in C](https://stackoverflow.com/q/605845/995714) – phuclv Sep 01 '19 at 08:13
  • Note that the memory returned by `malloc()` contains indeterminate data. However, that doesn't matter much here; you're printing pointer values incorrectly. You should be using the `%p` conversion specification — see [Correct format specifier to print pointer or address](https://stackoverflow.com/questions/9053658/correct-format-specifier-to-print-pointer-or-address/9053835#9053835). – Jonathan Leffler Sep 01 '19 at 08:15
  • Both `&ivar[i]` and `ivar + i` produce the same address; they're interchangeable. I use the `&ivar[i]` notation most often — but it depends on the context. – Jonathan Leffler Sep 01 '19 at 08:18
  • Thank You Gentlemen. Say **ivar=(int*)malloc(1*sizeof(int));** Here I am using n=1 (1*sizeof(int)). So if I want to get the value from user, I use **scanf**. If I write **scanf("%d",&ivar[_0_]);** or **scanf("%d",&ivar[_1_]);** , It is getting accepted and returning correct output. I don't understand this and what is the use of number inside subscript operator..Is it index? .. Please Explain gentle man @JonathanLeffler . I am not good with this concepts as I am new to Data Structures. – Hari Ram Sep 01 '19 at 13:57
  • If you allocate one element in the array, trying to read into `&ivar[1]` is writing out of bounds, which is a very bad idea. You can only use `&ivar[0]` or just `ivar` — they're equivalent, and which to use depends on whether you want to consider `ivar` as a very small array or as a (pointer to a) single variable. If as part of an array, use the array notation; if as a single variable, use the pointer notation. – Jonathan Leffler Sep 01 '19 at 19:51

1 Answers1

1

The memory allocated using malloc is created in heap section of RAM.

ivar=(int*)malloc(1*sizeof(int));

The syntax for malloc is

void *malloc(size_t size);

(1*sizeof(int)) gives 4 bytes, so 4 bytes is allocated in Heap.

You cannot directly access memory of heap, so ivar pointer is used to access it.

When you write

printf("%p",ivar); // outputs 2510

printf("%p",&ivar);// outputs 65524

Both of these gives address, first one gives address of the location pointer is pointing at, second one gives address of the pointer

scanf("%d",&ivar[i]);

and

scanf("%d",ivar+i);

both are equal, so you can use either one of them.

arvind
  • 275
  • 2
  • 11
  • 1
    the two `printf` invoke UB because `%u` is not for printing pointers – phuclv Sep 01 '19 at 08:13
  • 1
    Strictly, you should cast the values for `%p` to `void *` — though I'm not aware of a system in current use where that makes a difference (but if the system I learned C on was still around, that _would_ need the explicit cast). – Jonathan Leffler Sep 01 '19 at 08:16