无标题文章

2018-07-15  本文已影响0人  lion_good_very

layout: post
title: Linux Memory Management
comments: true
date: 2016-10-15 17:42:32+00:00
categories:

introduction: 'Linux Memory Management'


Linux Memory Management

The Process Address Space

Pages And Paging

Allocating Dynamic Memory

#include <stdlib.h>

void * malloc (size_t size);

#include<stdio.h>
#include<stdlib.h>
int main(){
    char* p;
    p=malloc(2048);
    if(!p){
        perror("malloc failed");
    }
}
void* xmalloc(size_t size){
    void* p;
    p=malloc(size);
    if(!p){
        perror("malloc failed");
        exit(EXIT_FAILURE);
    }
    return p;
}
#include <stdlib.h>

void * calloc (size_t nr, size_t size);
void* xcalloc(size_t nr,size_t size){
    void* p;
    p=calloc(nr,size);
    if(!p){
        perror("calloc failed");
        exit(EXIT_FAILURE);
    }
    return p;

int main(){
    int *p,*q;
    p=(int*)xmalloc(50*sizeof(int));
    q=(int*)xcalloc(50,sizeof(int));
    printf("%d\n",q[0]);
    return 0;
#include<stdio.h>
#include<stdlib.h>
#include "malloc.h"
int main(){
    int *p;
    p=xcalloc(2,sizeof(int));
    printf("size of p %d\n",sizeof(p));
    p=realloc(p,sizeof(int));
    printf("size of p %d\n",sizeof(p));
    //dangerous
    for(int i = 10;i<1<<10;++i){
        p[i] = 1;
    }
}
#include <stdlib.h>

void free (void *ptr);
#include<stdio.h>
#include<stdlib.h>

void print_chars(int n,char c){
    int i;
    for(i=0;i<n;++i){
        char *s;
        s=(char*)calloc(i+2,1);
        int j;
        for(j=0;j<i+1;++j){
            s[j]=c;
        }
        printf("%s\n",s);
        free(s);
    }
}

int main(){
    print_chars(10,'x');
    return 0;
}

result:

x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxxx

Alignment

Managing the Data Segment

上一篇 下一篇

猜你喜欢

热点阅读