-5

The syntax of malloc is malloc(num*sizeof(data_type))

I wonder why you need to specify a size at all. You could just specify a "large enough" number and everything will run fine.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 3
    You ask us about [`malloc`](http://en.cppreference.com/w/c/memory/malloc), which is a C function. But why did you then add the Java and C# language tags to your question? Please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 03 '17 at 20:33
  • How do you know that `50` is big enough for what you need? – Barmar Nov 03 '17 at 20:35

1 Answers1

4

The syntax is malloc(size). The only reason you're writing it as, for instance malloc(10*sizeof(int)) is because you're very rarely interested in the exact amount of bytes, but rather the amounts of elements. This also makes it portable and easier to change.

If you know that the size of int is 4 bytes (can vary from system to system) it is perfectly valid to write malloc(40) instead of malloc(10*sizeof(int)) but this can cause bugs if you recompile for a different system, or later decides that you need a long instead of an int.

And you need to specify a size. When you're invoking malloc(20) you will get a pointer that points to a chunk of memory that's 20 bytes. If you later realize this is not enough, you can use realloc to change the size. But you do need to specify a size, cause otherwise the OS will simply not know how much memory to give to you. Also, if malloc fails, it will give you a NULL pointer. One reason to fail is that you ask for more memory than currently available.

You CAN ask for more memory than you need. But what's the reason? The consequence will be that your program will demand more memory. That's not a good thing.

Sidenote:

You MAY get more memory than you request. malloc guarantees that you get at least the amount you ask for, unless the allocation fails. However, you should - for obvious reasons - never count on it.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • and self documented – OznOg Nov 03 '17 at 20:34
  • 1
    An even better way would be to use `sizeof *pointer_variable` as the element size. As in `int *ptr = malloc(10 * sizeof *ptr);` – Some programmer dude Nov 03 '17 at 20:34
  • but why need size if it can allocate memory during runtime . – Abdur Ramij Raj Nov 03 '17 at 20:35
  • 2
    well, when you buy apples, you need to tell how many apple you want, even if the vendor can (dynamically) give you any number of apple. – OznOg Nov 03 '17 at 20:38
  • @AbdurRamijRaj Yes you can allocate 50 or 60 or 70 bytes at runtime. But what if you need to allocate *80* (for example for 20 `int32_t` elements)? If you just guess the amount of memory you need, then either you will allocate to much and waste resources, or you will allocate to little and go out of bounds and have undefined behavior. – Some programmer dude Nov 03 '17 at 20:41
  • 1
    @AbdurRamijRaj And about that "runtime" note, ***all*** allocations with `malloc` is done at runtime. – Some programmer dude Nov 03 '17 at 20:42
  • @oznog you give the example that i can do as array too, but malloc is not like why malloc is dependent of size. if it dont care the size – Abdur Ramij Raj Nov 03 '17 at 20:47
  • 1
    @AbdurRamijRaj: `malloc` *does* care about the size - you have to tell it how many bytes of memory you want at any given time. Dynamic memory in C doesn't automatically grow or shrink as you use it, you have to explicitly allocate and resize it as necessary. – John Bode Nov 03 '17 at 20:50
  • @AbdurRamijRaj Read some numeric input from the user, put it in an integer variable, use that variable to calculate the size. Re-using my previous example: `size_t element_count; /* Read data from somewhere to initialize the element_count variable... */ int *ptr = malloc(element_count * sizeof *ptr);`. Is *that* what you want?How could you not figure it out by yourself? Using a literal integer like `50` or a variable containing the value `50` doesn't matter at runtime. – Some programmer dude Nov 03 '17 at 20:50
  • @AbdurRamijRaj One of the primary reasons to use `malloc` is when you won't know how much memory you'll need _until_ runtime. Imagine a program that prompts the user "How many ints do you want to allocate?" The user could enter 5, or 5000, you can't possibly know ahead of time. You use `malloc` to request the amount of memory you _need_. If you already know how much memory you need at compile time, chances are you don't need to (and shouldn't) use `malloc`,, but there are exceptions to that. – yano Nov 03 '17 at 22:17
  • @klutt nitpick, but I believe `malloc` returns _at least_ as many bytes as requested (or NULL or course).. it's free to allocate a bigger chunk for its own bookkeeping and/or to match alignment requirements, or who knows what other reasons. Of course, none of that is the user's concern and it's UB to access beyond what was requested. See https://stackoverflow.com/a/430172/3476780 and following answers. – yano Nov 03 '17 at 22:26
  • @klutt ahh ok .. I dunno, it may confuse the OP or anyone else into thinking there's really more accessible bytes than requested. It just came to mind when you said, "When you're invoking malloc(20) you will get a pointer that points to a chunk of memory that's 20 bytes." From the user's point of view, that's exactly true.. maybe add a footnote? – yano Nov 03 '17 at 22:39