Question for a pointer code fragment

  • Context:
  • Thread starter Thread starter Linda8888
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around understanding a code fragment involving pointers in C, specifically focusing on the use of type casting with pointers and the output of memory addresses generated by the code. The scope includes technical explanations and clarifications regarding pointer behavior and memory allocation.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants explain that the notation "(MyType **)" is a type cast that redefines a pointer to be a pointer to a pointer of a structure type, which is a more specific definition of a pointer.
  • One participant notes that the output values (432 and 1024) correspond to memory addresses, specifically for "arr + 3" and "*arr", and that the code allocates space for 10 pointers and fills them with addresses of heap memory.
  • Another participant adds that the specific values of the pointers can vary with each execution of the program due to how the operating system allocates memory, emphasizing that the focus should be on calculating values A, B, and C based on those addresses.

Areas of Agreement / Disagreement

Participants generally agree on the nature of type casting and the behavior of memory allocation, but there is some uncertainty regarding the significance of the specific output values and how they relate to the calculations mentioned.

Contextual Notes

There are unresolved aspects regarding the exact calculations for A, B, and C based on the memory addresses, as well as the implications of varying pointer values across different program executions.

Linda8888
Messages
7
Reaction score
1
TL;DR
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
 
  • Informative
Likes   Reactions: berkeman
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.
 

Similar threads

  • · Replies 40 ·
2
Replies
40
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
Replies
7
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
12
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
4K
Replies
3
Views
1K