`
yaojingguo
  • 浏览: 208168 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Stack and heap allocation in C

阅读更多

 

#include <stdio.h>
#include <stdlib.h>
/*
 * str is a literal. So it is allocated in readonly segment. It is OK to return 
 * it. But the data pointed by the pointer can't be modified.
 */
char *static_pointer_return() 
{
    char *str = "world";
    return str;
}

/*
 * Never to return a pointer pointing to data in stack. GCC issues a warning 
 * for it.
 */
char *stack_pointer_return()
{
    char str[] = "world";
    return str;
}

void pointer_param(char * str) 
{
    printf("param pointer: %s\n", str);
    /*
     * If str is allocated from heap, free succeeds. If str is pointing to data 
     * in stack, core dump.
     */
    free(str);
}


int main(int argc, const char *argv[]) 
{
    printf("static pointer return: %s\n", static_pointer_return());
    printf("stack pointer return: %s\n", stack_pointer_return());

    char str[] = "hello";     
    pointer_param(str);
    return 0;
}
 

Typical memory layout.

  • 大小: 14 KB
分享到:
评论

相关推荐

    Understanding and Using C Pointers 原版pdf by Reese

    The stack and heap are areas of memory used to support functions and dynamic memory allocation, respectively. Pointers are complex enough to deserve more in-depth treatment. This book provides that ...

    英文原版-Problem Solving and Program Design in C 7th Edition

    students learn to implement fundamental data structures such as lists, stacks, queues, and trees in a language that fosters their understanding of stack- and heap-dynamic memory allocation and ...

    Essential C

    In this section, "Essential C" delves into the fundamental data types and operators in C. It explains the various integer types, such as `int`, `short`, and `long`, along with floating-point types ...

    Packt.Mastering.Csharp.and.NET.Programming

    - **The Stack and the Heap**: Explains the difference between stack-allocated and heap-allocated memory and their respective uses. - **Garbage Collection**: Discusses how the .NET runtime ...

    C语言高级编程及实例部.rar

    else if (free(ptr)) /* Block is from C-Ware's heap */ { return ; } } puts("\nAttempt to free unallocated block!\n\7") ; exit(1) ; } char *Allocate(bytes) unsigned bytes ; { BLOCK ...

    Microsoft Codeview and Utilities User's Guide

    Information in this document is subject to change without notice and does not represent a commitment on the part of Microsoft Corporation. The software described in this document is furnished under a ...

    Modern Operating Systems,Tanenbaum,3rd

    4. **The Model of Run Time**: Explains the run-time environment of C programs, including the stack, heap, and global data areas. #### Research on Operating Systems Tanenbaum highlights the ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...

    Allocation & Reclaim

    1. 堆(Heap)与栈(Stack)分配:堆内存主要用于动态分配,大小不固定,由程序员负责管理。栈内存用于存储函数调用时的局部变量,由编译器自动管理,速度快但大小有限制。 2. 内存对齐:为了提高访问效率,内存...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    - **Memory Allocation**: Reference types are stored on the heap, while value types are stored on the stack or directly inside objects. - **Assignment**: Assigning values to variables and objects works...

    BobBuilder_app

    In the event of page getting full and reaching the PageItemCount, MGIndex will sort the keys in the page's dictionary and split the data in two pages ( similar to a b+tree split) and update the page ...

    内存分配源代码MemoryAllocation.rar

    在Java编程语言中,内存分配主要由Java虚拟机(JVM)负责,它包括堆内存(Heap)和栈内存(Stack)的管理。在"MemoryAllocation.java"这个文件中,我们可以预见到它可能包含了一些关于内存分配的实现或示例。 首先...

    VS2019 16的ASAN常用选项

    可以用来定位开发时遇到的大多内存问题,诸如栈、堆、全局变量越界、alloc-dealloc-mismatch、allocation-size-too-big、new-delete-type-mismatch、heap-use-after-free、stack-use-after-scope、stack-use-after-...

    MIT开放课程源码及资料_C/C++内存管理

    1. **内存区域**:在C/C++中,内存主要分为三个区域:栈(Stack)、堆(Heap)和静态/全局存储区(Static/Global Storage)。栈用于存储函数调用时的局部变量和函数参数;堆用于动态内存分配,程序员需手动管理;...

    allocation.zip

    内存分配主要涉及两个关键区域:堆(Heap)和栈(Stack)。堆是Java对象存储的地方,而栈则存储方法调用及其相关的局部变量。在JVM中,内存分配的过程对程序性能有着直接影响。 1. **堆内存分配**:在Java中,每当...

    c语言内存管理.pdf

    ### C语言内存管理知识点 #### 一、C语言内存管理概览 在C语言中,内存管理是一项核心技能,尤其对于高效程序设计至关重要。本文档将深入探讨C语言中的内存管理机制,帮助开发者理解不同类型的内存分配以及如何...

    C语言的内存管理.docx

    栈内存分配(Stack Memory Allocation) 栈内存由编译器自动管理,主要用于存储函数内的局部变量及函数调用时传递的参数等。栈内存的分配和释放非常快速,但空间有限,过大的数据可能会导致栈溢出。 **特点总结:...

    C-P-P-memory-allocation-mode.rar_memory

    1. 栈内存(Stack Memory):栈是程序运行时由系统自动分配和释放的内存区域。通常用于存放函数调用时的局部变量、函数参数以及返回地址等。栈内存的大小有限,一般为几MB,且分配和释放速度非常快。但是,由于它的...

Global site tag (gtag.js) - Google Analytics