4 answers
If you write a type:
int* ptr_int = ... int& link = *ptr_int; sizeof (link);
int* ptr_int = ... int& link = *ptr_int; sizeof (link);
it will give the size of the data, and how to do the same, but without resorting to the link?
In your question, it seems the parser has eaten asterisks. If I put them right, the answer to the question is: sizeof(*ptr_int)
.
sizeof allows you to find out only the size known at compile time. If you have class B
inherited from class A
, and there will be such code:
B b; A* ptrB=&b; cout << sizeof(*ptrB);
then the size of the class A object will be displayed.
If you need to know the size of the pointer, known in runtime, you can define these classes as follows:
class A { virtual unsigned int GetMySize() {return sizeof(A);} int Avalue; }; class B: public A { virtual unsigned int GetMySize() {return sizeof(B);} double Bvalue; };
You can get the real size of the object like this:
cout << ptrB->GetMySize();
It uses virtual functions that increase the size of objects by storing a pointer to a table of virtual functions, so sizes may vary.
If the data type where the pointer points to is unknown, then there is no official method.
- And if the data type is known, but is it a pointer? Simple dereferencing does not help - sizeof will display the size of the pointer. If you write a type: int * ptr_int = ... int & link = * ptr_int; sizeof (link); it will give the size of the data, and how to do the same, but without resorting to the link? - zzlavv
- sizeof can take size from type. - KoVadim
- In this case, sizeof will produce the size of the type, but the size of the data referenced by the pointer is needed. - zzlavv
- If you write a type: int ptr_int = ... int & link = ptr_int; sizeof (link); This will give the size of the data.
sizeof
always gives the size of the type. In this case,sizeof
will display the size of the TD, which is thelink
. That is why it is possible:int* a = NULL; sizeof(*a)
int* a = NULL; sizeof(*a)
; althougha
does not point to any data,sizeof
in compile-time looks at how TD is*a
and returns this value. - fogbit - If in this example, instead of int, we substitute a structure, then sizeof will display the size of the data in the structure. Question: Is it possible to do the same through sizeof without using the link? - zzlavv
If the memory was allocated dynamically, then you can use the malloc_usable_size function. It can return more than the value of * alloc was transferred during allocation, but this is the actual amount of memory that was allocated by this pointer. I only have a bad idea of what situation such a situation might suddenly need ... How did it happen that you don’t know how much you allocate?)
- I have 2 programs exchanging data through a file projected into memory. The size of the data naturally changes, the memory is allocated by the program and I cannot control it at all, but in order to project the data into a file you need to know the size of this data. I can only access them through pointers. So I'm trying to use sizeof to get the size of the data. So far it turns out only when using links. - zzlavv
- 2The problem is not clear. There is a pointer, but it is not clear what? And if it is clear, why do you need exactly sizeof? And then as in win95, "Unknown device detected, install driver" ... - alexlz
- Does the program that writes the data know the size of the data? If yes, then no problem. It can be done in such a way that not the data would lie on the pointer, but the structure of a known structure. And inside this structure, and the size can be put, and a pointer to the data. But if programs exchange pointers, who prevents another 4 bytes of size from being exchanged? - KoVadim
- The question is not how to refine the program, but is it possible with the help of sizeof to get a pointer to a structure or class to find out the size of the data without resorting to a link? - zzlavv
- one> Who has any suggestions?) I was funny to find out) Some left value. Most likely what is contained in the
eax
register, since the returned values are passed through it. > The lecturer forced us to write the function of overloading the operator [] with the return of the link and the condition inside, that is, it turned out something like this: Are you sure you had to do this? It was necessary to crash the program or throw an exception. Even in this code it is not provided that i may be <0. - gammaker
const char* p1="text"; int size1=sizeof(*p1); // == 1 (аналогично sizeof(char) int size2=sizeof(p1); // == 4 (аналогично sizeof(char*) const char p2[]="text"; int size3=sizeof(p2); // == 5
References are not used anywhere.
If the array is dimacic, then sizeof cannot be obtained from it in any way - sizeof calculates the size of the occupied memory area during compilation.