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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top