简体   繁体   中英

What is the purpose of the byte size of the type of a variable if I know the address of the variable?

I am not getting the whole purpose of working with the byte size of a variable by knowing the address of it. For example, let's say I know where an int variable is stored, let's say it is stored in address 0x8C729A09 , if I want to get the int stored in that address I can just dereference the address and get the number stored on it.

So, what exactly is the purpose of knowing the byte size of the variable? Why does it matter if the variable has 4 bytes (being int ) or 8 bytes if I am able to get the value of the variable by just dereference the address ? I am asking this, because I am working on dereferencing some address and I thought that I needed to go through a for loop to get the variable (By knowing the start address, which is the address of the variable, and the size of the variable in bytes) but whenever I do this I am just getting other variables that are also declared.

A little bit of context: I am working on a tool called Pin and getting the addresses of the global variables declared in another program.

The for case looks something like this:

for(address1 = (char *) 0x804A03C, limit = address1 + bytesize; address1 < limit; address1++)
      cout <<  *(address1) << "\n";

Michael Krelin gave a very compact answer but I think I can expand on it a bit more.

In any language, not just C, you need to know the size for a variety of reasons:

  • This determines the maximum value that can be stored
  • The memory space an array of those values will take (1000 bytes will get you 250 ints or 125 longs).
  • When you want to copy one array of values into another, you need to know how many bytes are used to allocate enough space.
  • While you may dereference a pointer and get the value, you could dereference the pointer at a different portion of that value, but only if you know how many bytes it is composed of. You could get the high-value of an int by grabbing just the first two bytes, and the low value by getting the last two bytes.
  • Different architectures may have different sizes for different variables, which would impact all the above points.

Edit:

Also, there are certainly reasons where you may need to know the number of bits that a given variables is made of. If you want 32 booleans, what not a better variable to use than a single int, which is made of 32 bits? Then you can use some constants to create pointers to each bit and now you have an "array" of booleans. These are usually called bit-fields (correct me if I am wrong). In programming, every detail can matter, just not all the time for every application. Just figured that might be an interesting thought exercise.

The answer is simple: the internal representation of most types needs more than one byte. In order to dereference a pointer you (either you or the compiler) need to know how much bytes should be read.

Also consider it when working with strings, you cannot always relay on the terminating \\0 , hence you need to know how many bytes you have to read. Examples of these are functions like memcpy or strncmp .

Supposed you have an array of variables. Where do you find the variable at non-zero index without knowing its size? And how many bytes do you allocate for non-zero length array of variables?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM