Memory Allocation: Understanding int*p and int x

  • Thread starter camel-man
  • Start date
  • Tags
    Memory
In summary: It would be much easier and cleaner to have all the variables be global and sort of manage them that way.
  • #1
camel-man
76
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
  • #2
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.
 
  • #3
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.
 
  • #4
A major use of pointers is for building dynamic data structures such as linked lists.
 
  • #5
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.
 
  • #6
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:
  • #7
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.
 

Related to Memory Allocation: Understanding int*p and int x

1. What is memory allocation in programming?

Memory allocation is the process of assigning memory space to different elements in a program. This is an important concept in programming as it determines how much memory is available for a program to use and how it is managed.

2. What is the difference between int* and int x?

Int* and int x both refer to variables in a program, but they have different data types. Int* is a pointer variable that stores the memory address of another variable, while int x is a regular integer variable that stores a specific value.

3. How does memory allocation work in C++?

In C++, memory allocation is managed by the programmer using functions such as malloc() and free(). These functions allow the programmer to dynamically allocate and deallocate memory as needed during the execution of the program.

4. Why is understanding memory allocation important?

Understanding memory allocation is important because it allows programmers to optimize their code and use memory efficiently. It also helps to prevent memory leaks and other memory-related errors that can cause a program to crash.

5. How can we avoid memory allocation errors?

To avoid memory allocation errors, it is important to carefully manage memory usage in a program. This can be done by properly allocating and deallocating memory, using data types that are appropriate for the data being stored, and avoiding unnecessary memory usage.

Similar threads

  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
3
Views
827
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
772
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
5K
Back
Top