Question for a pointer code fragment

  • Thread starter Thread starter Linda8888
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion clarifies the meaning of the notation "(MyType **)" in C programming, identifying it as a 'cast' that redefines a pointer to a pointer of a specific structure type. The malloc function returns a void pointer, which is then cast to a more specific type for better memory management. Additionally, the conversation addresses memory addresses output by a program, explaining that the values (432 and 1024) represent the locations in memory for specific pointers. These addresses are dynamically assigned by the operating system and can vary with each execution of the program. The focus is on understanding pointer casting and memory allocation rather than the specific values of the pointers.
Linda8888
Messages
7
Reaction score
1
TL;DR Summary
Assume that the function print_mem_addr(void *) takes in a pointer and prints the machine address the pointer points to as an integer value (not hexadecimal). You may use the fact that sizeof(MyType*) is 8 and sizeof(MyType) is 16. What is the value of output (A), (B) and (C)?
(I am not sure what does "(MyType **)" means after the '=' sign at the second line)

截圖 2021-02-27 下午3.51.57.png
The output is :

截圖 2021-02-27 下午3.52.14.png
 
Technology news on Phys.org
It's called a 'cast'. The malloc function returns a pointer of type void*. The (MyType **) redefines the pointer to be a pointer to a pointer to a structure of type MyType. It's still a pointer (a location in memory), it's just more narrowly defined. See the link below. By the way, if you include your code in the code tags (like this above </>), then it is easy to copy and paste the code. This is better than including a picture of the code.

https://www.tutorialspoint.com/cpro...o another,as follows − (type_name) expression
 
phyzguy said:
It's called a 'cast'. The malloc function returns a pointer of type void*. The (MyType **) redefines the pointer to be a pointer to a pointer to a structure of type MyType. It's still a pointer (a location in memory), it's just more narrowly defined. See the link below. By the way, if you include your code in the code tags (like this above </>), then it is easy to copy and paste the code. This is better than including a picture of the code.

https://www.tutorialspoint.com/cprogramming/c_type_casting.htm#:~:text=Converting one datatype into another,as follows − (type_name) expression


Thanks for replying!
Why are the first output 432 and the third 1024? How do those work?
 
Linda8888 said:


Thanks for replying!
Why are the first output 432 and the third 1024? How do those work?

Those are the memory addresses for, respectively, arr + 3 and *arr. The code you posted first allocates enough space on the heap for 10 pointers (addresses), and then iterates 10 times to fill those 10 addresses with the addresses of chunks of heap memory.
 
Linda8888 said:
Thanks for replying!
Why are the first output 432 and the third 1024? How do those work?

The operating system puts those pointers wherever it finds space available in memory. In a real system, if you ran the program multiple times, those values might be different each time. So the exact values of those pointers is not the point. Given those values, you are supposed to calculate A, B, and C.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top