Memory Allocation: Understanding int*p and int x

  • Thread starter Thread starter camel-man
  • Start date Start date
  • Tags Tags
    Memory
Click For Summary

Discussion Overview

The discussion revolves around the concept of memory allocation in C, specifically focusing on the differences between using pointers with dynamic memory allocation (malloc) and stack allocation (local variables). Participants explore the implications of these methods at a hardware level and the rationale behind using pointers.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant questions what address a pointer holds when allocated with malloc, suggesting it points to a memory location on the heap.
  • Another participant clarifies that malloc designates previously unused memory as allocated and that the pointer holds the address of this allocated memory, which initially contains random data.
  • A participant expresses confusion about the necessity of pointers, questioning why all variables aren't global to simplify programming.
  • Multiple participants provide examples of pointer usage, including dynamic data structures like linked lists, arrays of pointers, and their role in object-oriented programming in C++.
  • One participant argues against making variables global, stating it complicates code maintenance and understanding, contrasting it with the benefits of using pointers for efficient object references.

Areas of Agreement / Disagreement

Participants generally agree on the utility of pointers in programming, particularly for dynamic memory management and data structure implementation. However, there is no consensus on the necessity of pointers versus global variables, as some participants express confusion about their use.

Contextual Notes

The discussion includes assumptions about memory management and programming practices that may not be universally applicable. The implications of using pointers versus global variables are not fully resolved, and the technical details of memory allocation are acknowledged as complex.

Who May Find This Useful

This discussion may be useful for students and practitioners of C programming, particularly those interested in memory management, data structures, and the implications of using pointers in software design.

camel-man
Messages
76
Reaction score
0
int *p=malloc(sizeof(int));

and

int x;
int*p=&x;



I know that if you take the second one and print p it gives the address of x correct?
now what I want to know is when I malloc a pointer what address does it hold?? does it still hold the local address of p? Since I am not pointing it to a variable does malloc create memory that has an address and point it to that address on the heap?
 
Technology news on Phys.org
camel-man said:
int *p=malloc(sizeof(int));

does malloc create memory that has an address and point it to that address on the heap?

Yes, using the word "create" loosely. The memory is always there, of course; malloc() simply designates some unallocated ("unused") memory as now being allocated ("in use"). p now contains the address of the memory that was allocated.

The allocated memory contains random garbage (whatever bits happened to be at that location when it was allocated) until you store store something in it, with e.g. *p = 42.
 
Ok thank you jtbell, also I just don't really quite understand why we use pointers as far as the hardware level is concerned. how come every variable isn't global wouldn't that make more sense, eliminating pointers all together, but I am sure there are restrictions to be taken into consideration that I don't understand.
 
A major use of pointers is for building dynamic data structures such as linked lists.
 
Another example would be an array of pointers to strings (of characters). In the case of a sort program, you could sort the pointers instead of the strings.
 
Another would be classes in C++. The "this" pointer used to allow methods to work on all instances of a class. They're also used in vtables which are what allow polymorphism to work.
 
Last edited:
pointers are also used as efficient object references.
making variables global is not a good practice. there are certain cases that it is a must but in general, it creates hard to understand, modify, maintain, test code. It basically oppositive of divide and concur.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
Replies
3
Views
2K
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
16
Views
6K
Replies
12
Views
2K