How memory allocation works in C Programming for storing the different values?

What you see is the size of the short is 2,2 It is two bytes and then the pointer for that is 8 bytes. The pointer is always 8 bytes, you can see it now. A pointer to anything, a pointer to a short or an int, or a long is always 8 bytes because it is holding an address, 8 bytes on a 64-bit. It is indirection as we discussed, by *Sp, it goes into the location, in this location, it is just a memory address for some data type.

It is going to store a short value. The short data type is 2 bytes so I hope this is making things clear for all When you talk about integer, long, float, or double these are interesting ones. Interesting but simple now, I think we all know so we can write in terms of always remember we can write in terms of data type or we can write in terms of variable that is declared When we compile this and get all the 4 lines for 1 2 3 4 and get this out, what you see size of integer 4 4, long is 8 8, float 4 4 and double is 8 8. We can use both mechanisms to get these values.

When you see this basically, you can use both of these mechanisms to get this value. Let’s look into this, another interesting one. So far, we are talking about the size of the void pointer and VP, then print the size of the void, right? So we’re talking about these variables all this while it was commented out. I can see that kind of commented out. So let me uncomment this void v and *VP. Let’s focus and do the compilation. Let’s put the line numbers for this activity. Ok. So you should understand why I put the line number. Let’s compile and then we will come back into this code to understand. You will see a compilation error.

It says that the variable or the field and the name of that is “v” declared void. So, there is a problem at line number 25 exactly. There is a problem void “v” that the one. It is always good to put your LINE numbers whenever you are debugging, you know, unless you are a very Pro and you pretty much understand where you are in your code When you do this, what you are seeing is you are seeing that there is a problem because I told you early on – void void void – the void is not a valid data type in terms of size, so we cannot declare a variable of that type. However, we are free to declare a pointer to that, remember “VP” is a pointer to avoid, it is storing the address of somebody and at this point of time we don’t know who is that somebody – is it a character, array, structure, or it’s an integer or anything else? Right. this perfectly valid.

Let’s focus on “VP” now. We will discuss sizeof VP. We will focus on this line at this point. So this time compilation is through and then you know that size of the void pointer, is 8, 8, basically, both phase we are getting 8 because I told you now a pointer is a pointer doesn’t matter, I repeat a function pointer is also is a pointer, its stores an address. It has to be an 8-byte entity on a 64-bit platform and this is where you know people have doubts because we have declared the size of “v” right? So there was a “v” which I commented out and what we are doing is we are doing the size of “void”, we are not saying the size of “v” because the compiler does not recognize “v”. After all, it will say, you have declared an object, but it is “void”, I cannot recognize you and it’s an error.

So, instead of writing “v”, I am writing the data type “void” and let’s run this code, and these are the gray areas in which people have problems. Problems understanding it. When you run this code, it will happily throw out 1 You know 1, so the problem is why it’s 1 actually, right? Because void means nothing now, I think this is just an implementation choice. Ok. So what it really says is that I have to deduce the size of this computation for a data type, so the size of generally gives you the size of the computation, it expects something here, if there is nothing, if it is a “void”, I think it just defaults to 1, technically speaking it doesn’t make sense because void means nothing, it should return 0 for reasons I don’t have an idea at this point of time, but it just gives one So, this gives one but don’t assume that the size of the void is 1. No. Anyway, you can’t declare a variable of the type.

Let’s look into the very last part in terms of the 64-bit environment. Size of 100 Right. Size of F 8-times and size of F 10-times. Ok. So what’s the point here? What are we going to achieve here, right? We wanted to know what does the size of return when you write a hundred, what is a hundred? hundred is just an integer size of looks into the argument and if its an integer some integral value, some numerical value, some real number with decimal, and some numbers after decimal dot then its a double or float value. Anything other, like a whole number of integers, Those numbers are technically integers. This is an integer. What is the data type for integer in terms of memory allocation 4 bytes, by default it will be 4 bytes now? F is eight times, right.

When you look into the numeric representation of this in Hex this representation as ff can be stored in a single byte as the largest value. Then FF, another FF, … Totally, we are looking at 1-byte x 4 = four bytes. This number is the largest possible unsigned value kind of stuff. So this number can be stored in a 4-byte identity. So this also is a 4-bit integral value. So, it’s an integer. When you look next into FF, it’s 5 times. You require 5 bytes to store.

In reality, an integer value cannot be in terms of bytes for every new value, it has to be in a bucket, so it has to be an integer if it is any value that is less than or equal to ff ff ff ff value, but anything more than that. It has to be upgraded to something like a long data type. Can we say that we require only 5 bytes to store? But in reality, we have to upgrade to a long integer value, this has to be 8 bytes. This is 4, this is the largest integer and this is 8. And that’s what when you compile and run this particular code, you get a value of 4 4 8. That’s what we deduced here.

The summary of this entire exercise is twofold. You have to really do it even though I have given the demo you should do your own coding, coding will make you perfect in terms of writing this properly. I have seen in my classes many people understand it, but when they need to write they fumble, there are some issues here and then sometimes it is difficult for them to fully write it properly, the important thing to remember is the difference between a pointer dereferencing and a name of a pointer, here a lot of people create mistakes.

This is talking about memory allocation for fundamental data types. Let me quickly jumped over into a 32-bit environment. We will not go so elaborately on that. We just need to understand a few important things in terms of data size. This is an i686 system There are slightly older versions, these are the Intel series. So, 386, 486, 586, 686 series are generally a 32-bit environment.

Leave a Comment