(언어별) 메모리 구조 차이 (stack, heap, ...)
이 글은 C/C++ 관점에서 정리, 언어별 차이는 따로 정리해둠
대충 이렇게만 알고 있었는데, 이참에 정리!
메모리 동적 할당 → 힙 메모리
함수 호출 시 생성되는 지역 변수와 매개 변수 → 스택 메모리
👍 참고 굿
https://stackoverflow.com/questions/14588767/where-in-memory-are-my-variables-stored-in-c/14588866
언어별 변수 저장 장소
C++
지역 변수로 선언한 배열이나 클래스 인스턴스 → 스택 메모리;
STL 컨테이너 → 힙 메모리 필요에 따라 자동 할당
전역 변수 → Data 영역
(종만북 Ch3 p.55)
Java
All objects are dynamically allocated on heap.
Variables assigned to the object are all references, not the object itself! - 참고
https://www.geeksforgeeks.org/g-fact-46/
Python
⭐️ https://www.youtube.com/watch?v=OdQSWuG78Sk
'B', 4 같은 data → heap에 저장
allocates memory in the heap to store data
in stack, creates reference to the object
variables are all references
str1 = "Hello" # Actually data stored in heap memory
print(id(str1)) # 2647618227368
str2 = "Hello"
print(id(str2)) # 2647618227368 (Same!)
# str1 and str2 are mere references stored in stack which points to data in heap
print(str1 is str2) # True
* No references to the data in the heap → reference count 0 → garbage collector collects data from the heap
* Implementation 별로 다른 듯 (Cpython, Jython, PyPy, ...)
https://stackoverflow.com/questions/17130975/python-vs-cpython
Virtual memory picture
https://stackoverflow.com/questions/32418750/stack-and-heap-locations-in-ram
스택 : 함수 호출 시, 매개 변수, 지역 변수, return address 등 저장하는데 쓰임. (컴파일 타임) (시스템프로그래밍 참고)
힙 : 할당해야 하는 메모리 크기를 프로그램이 실행되는 동안 결정해야 하는 경우 (런타임)
SomeObject arr[10]; // stack allocation (compile time)
SomeObject *arr = new SomeObject[10]; // heap (run time)
컴파일 타임 vs 런타임 참고)
https://www.youtube.com/watch?v=iChalAKXffs&ab_channel=javidx9
영상 18'15''~19'30''
stack allocation (compile time) vs heap allocation (run time)
배열 선언 : 컴파일 타임 vs 런타임
https://dsnight.tistory.com/50
[C] 스택(Stack), 힙(Heap), 데이터(Data)영역
C언어의 메모리 구조 프로그램을 실행시키면 운영체제는 우리가 실행시킨 프로그램을 위해 메모리 공간을 할당해준다. 할당되는 메모리 공간은 크게 스택(Stack), 힙(Heap), 데이터(Data)영역으로 나
dsnight.tistory.com
www.tcpschool.com/c/c_memory_structure
https://suyeon96.tistory.com/26
[C++] 메모리 정적 할당 vs 동적 할당 (Stack vs Heap)
📃 이전 글 : [C++] 포인터 한방에 이해하기 (Call by Value vs Call by Reference) Memory 영역 (Stack vs Heap) 컴퓨터에서 메모리 영역 은 아래와 같이 나뉘어있다. Code : 실행한 프로그램의 코드..
suyeon96.tistory.com
'<기타 공부> > [기타 프로그래밍 및 범용 CS]' 카테고리의 다른 글
[Memory] Memory access에 대해 (0) | 2021.10.02 |
---|---|
[Linux] gcc, makefile (0) | 2021.07.10 |
[Git] Branch 생성 방식과 네이밍 규칙 + 커밋도 (0) | 2021.05.16 |
정규 표현식 (Regular expression) (0) | 2021.01.07 |
변수 이름 짓기 참고 (0) | 2020.10.25 |