-3
int x=(int)malloc(sizeof(float));

or

int y=(int)calloc(1,sizeof(double));

So in short, if I ask for a greater block of memory using malloc() or calloc() but assign it to a variable of smaller memory then what happens to the extra memory?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Strinzy
  • 41
  • 2
  • 7
  • 2
    `malloc` returns a pointer not a value. Did you actually mean `int* x = (int*)malloc(sizeof(float));` ? – 463035818_is_not_an_ai Sep 25 '16 at 15:14
  • Why would you *do* this? Especially in C++. – Jesper Juhl Sep 25 '16 at 15:17
  • 3
    You should never use `malloc` in C++, and you should never cast the result of `malloc` in C. The errors you will get from the compiler will then indicate that you are doing something very wrong. – n. m. could be an AI Sep 25 '16 at 15:17
  • 8
    The extra bytes will become zombie bytes, that will forever haunt you in your dreams. – Sam Varshavchik Sep 25 '16 at 15:17
  • 2
    Your casts are outright wrong. [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Sep 25 '16 at 15:19
  • I am just asking this out of curiosity because I thought that malloc reserves that much memory. I had run this code and it worked fine: #include #include int main(void) { int t=(int)malloc(sizeof(double)); t=5; printf("%d\n", t); return 0; } – Strinzy Sep 25 '16 at 15:20
  • 1
    I'm sure it worked "fine". The fact that it accomplished absolutely nothing is not changed by that. – Sam Varshavchik Sep 25 '16 at 15:23
  • @Strinzy FYI a more correct way to use `malloc` would be something like `int *x = malloc(sizeof(float))` and `int *y = malloc(sizeof(double))` - I *guess* you really didn't want to convert a pointer to an integer and confuse the people here. – anatolyg Sep 25 '16 at 15:35
  • Can't we have a new close reason to purge all those *"I want to learn programming, except without studying."* type of questions? – IInspectable Sep 25 '16 at 16:15
  • @IInspectable This actually seems like a good question, if only OP would [edit] and clarify it. I can certainly imagine how a curious person would ask himself such a question, but I *cannot* imagine any book that would contain an answer to it. A human teacher would answer it (after some initial struggle to understand it) but not everyone can afford a human teacher. – anatolyg Sep 25 '16 at 17:05
  • @anatolyg: Looking at the code in the question indicates, that the OP does not understand memory (or data types) at all. Their mental model is terribly broken. Whatever curiosity they might have, it is disjoint from reality. I don't even think that your paraphrased question is what the OP meant to ask. If you believe yours is a good question, you could ask it yourself. I'm sure you can ask a way better question than the OP could. – IInspectable Sep 25 '16 at 17:17
  • @IInspectable well, after reading the comments, I've appended my answer to cover the possible cases OP might be looking for, does this make the _question_ look any better? :) – Sourav Ghosh Sep 25 '16 at 17:20
  • @SouravGhosh: You should probably add the link from your comment above into your answer. Other than that, I still don't know, what the OP is trying to ask. I don't know what *"I am using malloc [...] for a float variable"* means, nor do I know what *"assign it to int"* is supposed to mean. It's dubious, which *"2 bytes"* the OP is referring to (seriously, I have no idea). And I honestly don't know whether or not your answer addresses the question the OP **meant** to ask. It remains clear as mud. – IInspectable Sep 25 '16 at 17:30
  • @IInspectable Regarding the 2 bytes, I believe OP is under the impression that size of an int is 2 bytes whereas size of a float is 4 bytes. – Sourav Ghosh Sep 25 '16 at 17:39
  • @SouravGhosh: The point is, I don't know, and you don't know. The right action would be to vote to close this question as *"unclear what you're asking"*. (I did.) – IInspectable Sep 25 '16 at 17:49
  • Thank you @SouravGhosh for helping me. – Strinzy Sep 25 '16 at 18:52
  • And I am so sorry @IInspectable, I really would not have posted this question if I was so smart like you. – Strinzy Sep 25 '16 at 18:52

2 Answers2

3

First of all, malloc() and family returns a pointer to void (void *), which we store into a "pointer-to-type" and later use that pointer to store values of "type" into the memory location.

As for the call to malloc() and family, we pass the required size in bytes and they return a pointer to the requested memory area (in bytes), if success. So, as long as x, y and z evaluates to same,

  int * p = malloc(x);

or,

  int * p = malloc(y);

or

  int * p = malloc(z);

will return the same amount of memory.

Now, if it happens, on your platform, sizeof(int) == sizeof(float) == 4, then, we can write

  int * p = malloc(sizeof(int));

or,

  int * q = malloc(sizeof(float));

or

  int * r = malloc(4);

and they all will return the same amount of memory.

Note: The above statements are just for illustration, the best possible way to use malloc() is to use the form

  <someType> * var = malloc(sizeof(*var));

FWIW, (as you did in your code) assigning a pointer to an integer type is implementation-defined behavior. In general, don't do that.

That said, you're always free to allocate more memory than you need, there's no problem from the C perspective, as long as you free() that. For example, you can allocate space for 10 integers while you end up using only 5 of them. There's no technical problem in that.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Answer to rest of two bytes

if we have following line of code

int *x=(int*)malloc(sizeof(float));

Here malloc returns a pointer which is then typecasted to integer type of pointer.
Assuming that float has size of 4 bytes and int of 2 bytes. Now argument that is passed to malloc is only number of bytes you want to allocate. Here sizeof(float) returns 4 and 4 bytes are allocated and malloc returns a pointer to first bytes that is allocated. So in this case there are two integer spaces allocated and this is continuous allocation. So first two bytes for first int and next 2 bytes is next integer and it becomes an array.

We can access them as

x[0] which is same as *(x+0) and x[1] which is same as *(x+1).

In short rest of two bytes will be the next integer and there is no problem with that.

Community
  • 1
  • 1