What happens in memory during DMA using pointers?

In summary: I've been trying to understand pointers for a while now and this was a great article to help me. In summary, p is a pointer to the address 0x2000.d.
  • #1
shivajikobardan
674
54
Homework Statement
pointers in c
Relevant Equations
none
C:
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p=(int *) malloc(4);
    if(p==NULL)
    {
        printf("Insufficient memory");
        return;
    }

    printf("Enter a number\n");
    scanf("%d",p);
    printf("Value of p=%d",*p);
}

1) What happens in background when we do int *p? Can you show a figure about what it does in memory?

2) What happens in memory "p=(int *) malloc(4);" when we run this line? In surface, I've rote memorized, it allocates 4 bytes of memory, but I don't know in detail what happens in background.

3) printf("Enter a number\n");
scanf("%d",p);

I need to store a number in address of p, so should not I do &p instead of just p?

4) Value of p=p isn't it? Why *p? I've again rote memorized *p gives value of p, but I'm not clear about what's going inside memory during this all.

I'm asking these questions knowing I'm wrong because I've tested the code in codeblocks already. It's not to get the correct answer but a way to think to get a correct answer.

If we'd done;
int x=10;
int *p=&x;
I'd understand what's being done here.
It means content of p is initialzed with address of x. Like this:
d_iEr26oBuLPMp6s6LBbsyRL4t8ShEwtSaCY4gL01CrB9clnuA.jpg

but I don't understand the above case that I mentioned in question where we are only doing int *p. I find it meaningless. p is a pointer variable. But it contains whose address?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
You may want to read up on stack vs heap allocation of variables in general.

shivajikobardan said:
p is a pointer variable. But it contains whose address?
p is assigned the result from the call to the malloc function, so if you know (look up) what malloc do you should be able to answer your own question.
 
  • #3
shivajikobardan said:
1) What happens in background when we do int *p? Can you show a figure about what it does in memory?
Uninitialized storage for p is allocated on the stack, storage large enough to hold a memory address. The amount of storage allocated depends on whether you're building your program in 32-bit mode or in 64-bit mode. Uninitialized means that the value stored there is whatever happens to already be in memory at that location. It's allocated on the stack because it's a variable that is declared inside a function (i.e., inside main()). If it had been declared outside of any function, it would have been allocated in static memory.

Your figure is reasonably accurate regarding the relationship between the p and x variables. The topic of pointers is one that students often have difficulties in understanding. I've taught classes in C many, many times, and I always present the topic of pointers using a diagram similar to yours. Having a diagram is very helpful.
shivajikobardan said:
2) What happens in memory "p=(int *) malloc(4);" when we run this line? In surface, I've rote memorized, it allocates 4 bytes of memory, but I don't know in detail what happens in background.
The malloc() function allocates 4 bytes of storage on the heap and returns the address of the first byte. This address is cast as a pointer to int and is stored in p. In your drawing, the address of this heap storage is 1000H (usually written as 0x1000 for hexadecimal numbers). This value is stored in the location identified as p. In your drawing, p's address is 0x2000.d
shivajikobardan said:
3) printf("Enter a number\n");
scanf("%d",p);

I need to store a number in address of p, so should not I do &p instead of just p?
The scanf() function's second argument here needs to be the address of the memory that will receive a value. p is a pointer to type int, so it already is an address.
shivajikobardan said:
4) Value of p=p isn't it? Why *p? I've again rote memorized *p gives value of p, but I'm not clear about what's going inside memory during this all.
Of course the value of a variable is its value, but that's always true and doesn't tell you anything. What your program intends to do is to print the value that is pointed to by p, not the value in p.
This line of code in your program, printf("Value of p=%d",*p);, will display the value you entered when scanf() was executed. That will be the value in the hypothetical address 0x1000.
 
  • Like
Likes berkeman
  • #4
Thank you all for your replies.
 
  • Like
Likes berkeman

1. What is DMA?

DMA stands for Direct Memory Access, and it is a process where data is transferred directly from one memory location to another without the involvement of the CPU.

2. How does DMA work?

DMA works by using a DMA controller, which is a hardware component that can access the memory independently from the CPU. The DMA controller reads data from a source memory location and writes it to a destination memory location without the involvement of the CPU.

3. What is the role of pointers in DMA?

Pointers are used in DMA to specify the source and destination memory locations for data transfer. The DMA controller uses these pointers to determine where to read and write data in the memory.

4. What happens in memory during DMA using pointers?

During DMA using pointers, the DMA controller reads data from the source memory location and writes it to the destination memory location. This process happens without the involvement of the CPU, allowing for faster data transfer.

5. What are the benefits of using DMA with pointers?

Using DMA with pointers can improve the efficiency and speed of data transfer. It also reduces the workload on the CPU, allowing it to focus on other tasks. Additionally, DMA with pointers can help reduce the chances of data corruption during transfer.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
891
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Back
Top