Direct memory access(DMA)

Problem Describe direct memory access (DMA). Can a user level buffer/pointer be used by kernel or drivers? Solution Direct Memory is a feature which provides direct access (read/write) to system memory without interaction from the CPU. So, if DMA is available, then processor can route long read or write requests of disk blocks to the DMA and concentrate on other work. The “DMA Controller” manages this by requesting the System bus access (DMA request) from CPU. [Read More]

zero sized allocation using malloc

int main() { int \*p \= 0; printf("before addr: %pn", p); p \= (int \*) malloc(0); printf("after addr: %pn", p); printf("sizeof: %un", sizeof(\*p)); \*p \= 1; printf("\--- %d -- this is the last statment.n", \*p); free(p); } Output before addr: (nil) after addr: 0x80496c8 sizeof: 4 -– 1 – this is the last statment. Note : Linux allows a ‘read’ of the zero sized allocated memory allows a ‘write’ on the zero sized allocated memory sizeof shows an allocation of 4 bytes. [Read More]

Memory Management In C and C + +

For simple applications, it’s enough just to rely on automatic memory management through local variables. But once the data become larger, it is no longer imperative to request memory from the heap and manage. Content Function Families Allocators Toolbox Smart Pointer STL Containers A function families In principle, belong to the memory always two parts. First, an area of memory are requested, and second, the memory used at the end of the work be returned. [Read More]

Program to check if the stack grows up or down

#include 
#include 

void stack(int *local1);

int main()
{
  int local1;
  stack(&local1);
  exit(0);
}

void stack(int *local1)
{
   int local2;
   printf("\nAddress of first  local : [%u]", local1);
   printf("\nAddress of second local : [%u]", &local2);
   if(local1 < &local2)
   {
     printf("\nStack is growing downwards.\n”);
   }
   else
   {
     printf("\nStack is growing upwards.\n”);
   }
   printf("\n\n”);
}

What is a NULL pointer? How is it different from an unitialized pointer? How is a NULL pointer defined?

A null pointer simply means “I am not allocated yet!” and “I am not pointing to anything yet!”. The C language definition states that for every available pointer type, there is a special value which is called the null pointer. It is guaranteed to compare unequal to a pointer to any object or function. A null pointer is very different from an uninitialized pointer. A null pointer does not point to any object or function; but an uninitialized pointer can point anywhere. [Read More]

What is the difference between malloc() and calloc()?

#include void *calloc(size_t n, size_t size); void *malloc(size_t size); The two functions malloc() and calloc() are functionally same in that they both allocate memory from a storage pool (generally called heap). Actually, the right thing to say is that these two functions are memory managers and not memory allocators. Memory allocation is done by OS specific routines (like brk() and sbrk()). Here are some differences between these two functions.. [Read More]

C code to dynamically allocate one, two and three dimensional arrays (using malloc())

One Dimensional Array int \*myarray = malloc(no\_of\_elements \* sizeof(int)); //Access elements as myarrayii 2 Dimensional Array Method1 int \*\*myarray = (int \*\*)malloc(nrows \* sizeof(int \*)); for(i = 0; i < nrows; i++) { myarrayii = malloc(ncolumns \* sizeof(int)); //allocating 1 D array = number of elements in column } // Access elements as myarrayiijj Method 2 (Contagious Allocation) int \*\*myarray = (int \*\*)malloc(nrows \* sizeof(int \*)); myarray00 = malloc(nrows \* ncolumns \* sizeof(int)); for(i = 1; i < no\_of\_rows; i++) myarrayii = myarray00 + (i \* ncolumns); // Access elements as myarrayiijj In either case, the elements of the dynamic array can be accessed with normal-looking array subscripts: array[i][j]. [Read More]