C++ Pointer Simple Question!

Asked 2 years ago, Updated 2 years ago, 40 views

typedef struct tagNode
{ 
  int data;
  struct tagNode* NextNode;
} } Node;

Regarding the above structure node

sizeof(Node) : 8 Byte
sizeof(Node*) : 4Byte

It was assigned as

1) How was the node's address capacity 4 bytes determined?

2) The capacity of Node is 8 bytes by int data 4 bytes + Node address + 4 bytes, right?

pointer

2022-09-21 15:27

2 Answers

A pointer is a variable that has a memory address, not the size of the data.

Since it is a memory address, it is 4 bytes for 32-bit os and 8 bits for 64-bit os. A pointer simply has the memory address that the variable points to.

On a 32-bit basis, the process can use a total of 4 gigabytes of memory. Why can I use 4 gigabytes? It's 32 bits, so you can have 32 multipliers in 2 and 32 multipliers in 2 is 4 gigabytes.

This means that the address size must be 4 bytes to represent 4 gigabytes of memory addresses.


2022-09-21 15:27

1) How was the node's address capacity 4 bytes determined?

In C or C++, the size of the pointer depends on the address system of the running system. It's usually said whether the OS is 32-bit or 64-bit, and the bit size at this time is the size of the address. The hardware characteristics are also relevant, as the number of bits of the address system the CPU uses depends on how many bits of OS should be used.

Node* is a pointer type that represents an address. Therefore, if it was a 32-bit system, sizeof(Node*) would be 4 bytes in size.

In C++, most pointers have the same size as sizeof(void*). That is, sizeof(void*) or sizeof(Node*) or sizeof(int*) both have the same size. However, depending on the case, it may be larger than sizeof(void*).

2) The capacity of Node is 8 bytes by int data 4 bytes + Node address + 4 bytes, right?

Yes, that's right.

However, because int is not a fixed size type, it may be 4 bytes or 2 bytes depending on the system. And as I said before, the size of Node* varies from system to system, so sizeof(Node) cannot necessarily be 8 bytes.

It becomes clear by checking that the current system follows the models of LP32, IP32, ILP32, LLP64, LP64. https://en.cppreference.com/w/cpp/language/types#PropertiesSee this link.


2022-09-21 15:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.