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

FAQ: What happens in memory during DMA using pointers?

What is DMA and how does it relate to memory?

DMA stands for Direct Memory Access. It is a feature that allows hardware subsystems within a computer to access the main system memory (RAM) independently of the central processing unit (CPU). This means that data can be transferred directly between memory and peripherals, such as disk drives or network cards, without burdening the CPU.

How do pointers play a role in DMA operations?

Pointers are used in DMA operations to specify the memory addresses involved in the data transfer. A pointer can indicate the starting address in memory where data should be read from or written to. By using pointers, the DMA controller can efficiently manage the data transfer process without CPU intervention.

What happens to the CPU during a DMA transfer?

During a DMA transfer, the CPU is generally free to perform other tasks because the DMA controller takes over the responsibility of moving data between memory and the peripheral device. This offloading of data transfer tasks can significantly improve system performance, especially for large or frequent data transfers.

Can DMA cause memory corruption, and how is it prevented?

DMA can potentially cause memory corruption if the DMA controller writes to incorrect memory addresses or if multiple devices attempt to access the same memory location simultaneously. To prevent this, systems often use memory protection mechanisms and DMA channels to ensure that each device has exclusive access to its designated memory areas during a transfer.

What are the advantages of using DMA over traditional CPU-driven data transfers?

Using DMA for data transfers has several advantages over traditional CPU-driven methods. These include reduced CPU overhead, faster data transfer rates, and improved overall system performance. By allowing the CPU to focus on other tasks while the DMA controller handles data movement, systems can achieve higher efficiency and responsiveness.

Similar threads

Replies
1
Views
9K
Replies
12
Views
1K
Replies
3
Views
1K
Replies
3
Views
973
Replies
2
Views
2K
Replies
7
Views
2K
Back
Top