Question for a pointer code fragment

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

The discussion focuses on understanding pointer casting in C programming, specifically the use of '(MyType **)' to redefine a pointer returned by the malloc function, which is of type void*. This cast indicates a pointer to a pointer of a structure type, enhancing type safety in memory management. Additionally, the conversation addresses memory addresses generated during pointer allocation, explaining that these addresses can vary with each execution of the program due to the operating system's memory allocation strategy.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with dynamic memory allocation using malloc
  • Knowledge of pointer types and dereferencing in C
  • Basic concepts of memory management and addresses
NEXT STEPS
  • Study C programming type casting techniques
  • Learn about dynamic memory allocation and deallocation in C
  • Explore pointer arithmetic and memory address manipulation
  • Investigate the behavior of memory allocation in different operating systems
USEFUL FOR

C programmers, software developers working with memory management, and anyone looking to deepen their understanding of pointers and dynamic memory allocation in C.

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
1K
  • · 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