#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
分享到:
相关推荐
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 ...
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 ...
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 ...
- **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 ...
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 ...
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 ...
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 ...
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 ...
1. 堆(Heap)与栈(Stack)分配:堆内存主要用于动态分配,大小不固定,由程序员负责管理。栈内存用于存储函数调用时的局部变量,由编译器自动管理,速度快但大小有限制。 2. 内存对齐:为了提高访问效率,内存...
- **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...
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 ...
在Java编程语言中,内存分配主要由Java虚拟机(JVM)负责,它包括堆内存(Heap)和栈内存(Stack)的管理。在"MemoryAllocation.java"这个文件中,我们可以预见到它可能包含了一些关于内存分配的实现或示例。 首先...
可以用来定位开发时遇到的大多内存问题,诸如栈、堆、全局变量越界、alloc-dealloc-mismatch、allocation-size-too-big、new-delete-type-mismatch、heap-use-after-free、stack-use-after-scope、stack-use-after-...
1. **内存区域**:在C/C++中,内存主要分为三个区域:栈(Stack)、堆(Heap)和静态/全局存储区(Static/Global Storage)。栈用于存储函数调用时的局部变量和函数参数;堆用于动态内存分配,程序员需手动管理;...
内存分配主要涉及两个关键区域:堆(Heap)和栈(Stack)。堆是Java对象存储的地方,而栈则存储方法调用及其相关的局部变量。在JVM中,内存分配的过程对程序性能有着直接影响。 1. **堆内存分配**:在Java中,每当...
### C语言内存管理知识点 #### 一、C语言内存管理概览 在C语言中,内存管理是一项核心技能,尤其对于高效程序设计至关重要。本文档将深入探讨C语言中的内存管理机制,帮助开发者理解不同类型的内存分配以及如何...
栈内存分配(Stack Memory Allocation) 栈内存由编译器自动管理,主要用于存储函数内的局部变量及函数调用时传递的参数等。栈内存的分配和释放非常快速,但空间有限,过大的数据可能会导致栈溢出。 **特点总结:...
1. 栈内存(Stack Memory):栈是程序运行时由系统自动分配和释放的内存区域。通常用于存放函数调用时的局部变量、函数参数以及返回地址等。栈内存的大小有限,一般为几MB,且分配和释放速度非常快。但是,由于它的...