堆栈小测试

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//栈方向
void func()
{
    printf("func:\n");
    int a,b,c,d;
    printf("%p\n",&a);
    printf("%p\n",&b);
    printf("%p\n",&c);
    printf("%p\n",&d);

    return;
}


int main()
{
    printf("main:\n");
    int a,b,c,d;
    printf("%p\n",&a);
    printf("%p\n",&b);
    printf("%p\n",&c);
    printf("%p\n",&d);

    func();

    return 0;
}

//栈溢出

void func(int n)
{
    if(n == 0)
        return;
    else
    func(n-1);
}



int main()
{
    //char arr[1024*1024*10];
    //strcpy(arr,"china");
    func(1000000000); //栈溢出,导致段错误
    printf("over\n");
    return 0;
}

//堆方向
void func()
{
    printf("in func:\n");
    char *pa,*pb,*pc,*pd;
    pa=malloc(100);
    pb=malloc(100);
    pc=malloc(100);
    pd=malloc(100);
    printf("%p\n",pa);
    printf("%p\n",pb);
    printf("%p\n",pc);
    printf("%p\n",pd);
}


int main()
{
    printf("in main:\n");
    char *pa,*pb,*pc,*pd;
    pa=malloc(100);
    pb=malloc(100);
    pc=malloc(100);
    pd=malloc(100);
    printf("%p\n",pa);
    printf("%p\n",pb);
    printf("%p\n",pc);
    printf("%p\n",pd);
    func();
    return 0;
}

//堆的简单使用
int main (int argc, char *argv[])
{
    char * p = (char*)malloc(1024*1024*1024);
    if(p == NULL)
        printf("malloc error\n");
    strcpy (p,"abcd");
    printf("over\n");
    free(p);
        p=NULL;
    return 0;
}
此条目发表在C分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Protected with IP Blacklist CloudIP Blacklist Cloud