Low level Memory Q&A - Embedded System Interview

Hot

Thứ Hai, 20 tháng 1, 2020

Low level Memory Q&A

Q: What is the memory layout of C program
A: Memory layout of C program includes sections:
- Text or Code Segment
- Initialized data segment
- Uninitialized data segment
- Stack
- Heap
Text or Code Segment:- The text segment contains a binary of the compiled program.
- The text segment is a read-only segment that prevents a program from being accidentally modified.
- It is sharable so that only a single copy needs to be in memory for frequently executed programs such as text editors etc.
DS(Initialized data segment):- It contains the explicitly initialized global and static variables.
- The size of this segment is determined by the size of the values in the program’s source code and does not change at run time.
- It has read-write permission so the value of the variable of this segment can be changed at run time.
- This segment can be further classified into an initialized read-only area and an initialized read-write area.
#include <stdio.h>

char c[]="emb-interview1";     /*  global variable stored in Initialized Data Segment in read-write area*/
const char s[]="emb-interview2";    /* global variable stored in Initialized Data Segment in read-only area*/

int main()
{
    static int i=44;          /* static variable stored in Initialized Data Segment*/
    return 0;
}

BSS(Uninitialized data segment):- It contain all uninitialized global and static variable.
- All variables in this segment initialized by the zero(0) and pointer with the null pointer.
- The program loader allocates memory for the BSS section when it loads the program.
#include <stdio.h>

int i;               /* Uninitialized variable stored in bss*/

int main()
{
    static int j;     /* Uninitialized static variable stored in bss */
    return 0;
}

Stack:
 - It located at a higher address and grows and shrinks opposite to the heap segment.
 - The stack contains local variables from functions and related book-keeping data.
 - A stack frame will create in the stack when a function is called.
 - Each function has one stack frame.
 - Stack frames contain function’s local variables arguments and return value.
 - The stack contains a LIFO structure. Function variables are pushed onto the stack when called and functions variables are popped off the stack when return.
 - SP(stack pointer) register tracks the top of the stack.

#include <stdio.h>
int main(void)
{
    int i; //local variable stored in stack
    return 0;
}

Heap: - It is used to allocate the memory at run time.
 - Heap area managed by the memory management functions like malloc, calloc, free, etc which may internally use the brk and sbrk system calls to adjust its size.
 - The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
 - It grows and shrinks in the opposite direction of the stack.
#include <stdio.h>
int main()
{
    int *p=(char*)malloc(sizeof(int));    /* memory allocating in heap segment */
    return 0;
}

Q: What is Big endian and little endian? Write a C program to check if the underlying architecture is little endian or big endian.
A: Big endian and little endian are two formats to store multibyte data types into computer's memory.
As an example, if x a four byte integer contains a hex value 0x76543210. If program is run on little endian machine, gives "67 45 23 01" as output. If it is run on big endian machine, gives "01 23 45 67" as output

#include <bits/stdc++.h> 
using namespace std; 
int main()  
{  
    unsigned int i = 1;  
    char *c = (char*)&i;  
    if (*c)  
        cout<<"Little endian";  
    else
        cout<<"Big endian";  
    return 0;  
}  
  

Q: Write a function called malloc2d() which allocates a two dimensional array. Minimize the number of calls to malloc() and make sure that the memory is accessible by the notation arr[i][j]
A:
#include <iostream>

using namespace std;

int** malloc2d(int rows, int cols)
{
    int header = rows * sizeof(int*);
    int data = rows * cols * sizeof(int);
    int** rowptr = (int**)malloc(header + data);
    int* buf = (int*)(rowptr + rows);
    int k;
    for (k = 0; k < rows; ++k) {
        rowptr[k] = buf + k*cols;
    }
    return rowptr;
}

int free2d(int** pt)
{
    free(pt);
    return 0;
}

int main(int argc, char* argv[])
{
    int i, j;
    int **p = malloc2d(3,5);

    for( i = 0; i < 3; i++){
        for(j = 0; j < 5; j++){
            p[i][j] = i + j;
            cout << p[i][j] << " ";
        }
        cout << endl;
    }

    free2d(p);

    return 0;
}

Q: Write an aligned malloc() & free() function that takes number of bytes and aligned byte (which is always power of 2)
A:
#include <iostream>
using namespace std;

int aligned_malloc(void **memptr, size_t alignment, size_t size)
{
    size_t len = size + alignment + sizeof(void *);

    void *ptr = malloc(len);

    if(ptr == NULL){
        return 0;
    }

    *memptr = (unsigned int *)((((unsigned)ptr) + alignment + sizeof(void *)) & ~(alignment - 1));

    *((unsigned int *)(((unsigned int *)*memptr) - 1)) = (unsigned)ptr;
    
    return 1;
}

int aligned_free(void *memptr)
{
    if(memptr == NULL){
        return 0;
    }
    else{
        free((void *)(*((unsigned int *)memptr - 1)));
        return 1;
    }
}

int main(int argc, char* argv[])
{
    for(int i = 1; i < 257; i *= 2){
        void *memptr = NULL;
        aligned_malloc(&memptr, i, 1000);
        cout << "aligned :" << i << " -- " << hex << (unsigned)memptr << endl;
        aligned_free(memptr);
    }
 return 0;
}

Không có nhận xét nào:

Đăng nhận xét

Thường mất vài phút để quảng cáo xuất hiện trên trang nhưng thỉnh thoảng, việc này có thể mất đến 1 giờ. Hãy xem hướng dẫn triển khai mã của chúng tôi để biết thêm chi tiết. Ðã xong