1

I found this piece of code, and I don't understand what it means. Do we have the right to use the array notation like here?

double *myList = malloc(3 * sizeof(double));
myList[0] = 1.2;
myList[1] = 2.3;
myList[2] = 3.4;

EDIT : I think this notation uses the fact that the memory address of myList[0],myList[1],myList[2] are consecutive. malloc() doesn't guarantee that the addresses are allocated consecutively.

Michael Heidelberg
  • 993
  • 11
  • 24

1 Answers1

5
double *myList = malloc(3 * sizeof(double));

It is allocating memory for 3 double typed data in pointer myList. Following lines assigning double typed data on those locations.

myList[0] = 1.2;
myList[1] = 2.3;
myList[2] = 3.4;

myList[2] is equivalent *(myList+2).

You need to deallocate this memory using free as follows after its usage:

free(myList);

malloc allocates consecutive locations in memory.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Steephen
  • 14,645
  • 7
  • 40
  • 47
  • 1
    there's no need to cast. this is C. – Karoly Horvath May 25 '15 at 23:36
  • You don't need to cast the return of malloc. In fact, some people [advise against it](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). I personally don't think it's a problem either way but since it is debatable it should not be presented as a must do. – kaylum May 25 '15 at 23:37
  • 1
    Is there something wrong with `double *myList = malloc(3 * sizeof *mylist);` If you always use the `sizeof object` you are creating, there is never a chance of error in the cast. – David C. Rankin May 25 '15 at 23:37
  • why should I cast the pointer returned by malloc() ? I read by some answers that it's useless in C . – Michael Heidelberg May 25 '15 at 23:39
  • 3
    You shouldn't, `malloc` returns a memory address (which is always `void`), so there is never a need to cast it. @MichaelHeidelberg if you are confused about the prior comment, the cast referred to `sizeof (double)` verses `sizeof *myList`. If you always use **your** object ,there is never a chance of getting the `sizeof (some type)` wrong. which is what I probably should have said `:p` – David C. Rankin May 25 '15 at 23:40
  • I understand ; but if malloc() returns consecutive memory ; what's the difference between it ( malloc() ) and calloc() ? ( I know that calloc() initialize the pointer to memory ) – Michael Heidelberg May 25 '15 at 23:46
  • 1
    @MichaelHeidelberg You already marked one of the differences. `malloc()`,`calloc()` and `reaclloc()` allocates consecutive memory. – Steephen May 25 '15 at 23:48
  • I didn't know it , just one last question , it's always guaranteed that the memory will be consecutively allocated ? or it's matter of the OS, Hardware ... ? – Michael Heidelberg May 25 '15 at 23:51
  • 1
    @MichaelHeidelberg It is guaranteed as per my knowledge at least in *NIX systems, where I am familiar. – Steephen May 25 '15 at 23:55
  • 1
    One important thing to consider is that `malloc()` returns `NULL` on failure, robust programs must prevent undefined behavior at all cost, and the lack of checking the returned pointer, is potentially Undefined Behavior. – Iharob Al Asimi May 26 '15 at 00:13
  • I cannot find a `man` page that actually states that it is contiguous. But maybe because it is so obvious. – Jongware May 26 '15 at 00:23
  • 2
    @MichaelHeidelberg: Consider this: If the allocated memory were *not* contiguous, how would you find out where it is? `malloc()` and friends always allocate a contiguous block of memory of the requested size. – Greg Hewgill May 26 '15 at 00:25
  • @jongware : same probleme for me. I didn't read that it allocates contiguous memory , but for calloc() they state it . question : why for calloc() and not for malloc() ? . – Michael Heidelberg May 26 '15 at 09:10
  • @Greg Hewgill : I understand . but I think we can develop a way to find where the memory is even if it's not contiguous. it is often possible to do something if it doesn't produce paradoxes. but for a reason of simplicity I think, malloc() allocates contiguous memory I think . – Michael Heidelberg May 26 '15 at 09:16
  • 1
    @MichaelHeidelberg: it's stated explicitly in `calloc` because one may otherwise think it behaves *different* from `malloc`, and on a call such as `calloc(3,5)` (with `count` as 3 and `size` as 5) it *could* return "three blocks of 5 bytes each" (which it does – **but** also contiguously). – Jongware May 26 '15 at 09:28