malloc 사용시 메모리 할당??
글쓴이: ar / 작성시간: 월, 2003/10/06 - 3:45오후
char *p;
char buf[256];
이런식으로 선언을 하고
p = malloc(256);
이렇게 메모리할당을 하는 프로그램코드가 있다고 할때요..
buf 는 힙 영역에 256byte 크기의 메모리를 할당 받는 것은 알겠는데요..
우선 포인터 변수 p는 스택 영역의 메모리에 4byte 할당이 되고
malloc(256) 하였을때는 힙 영역에 할당된 메모리를 참조만 하는 것인가요?
확실하게 알고 싶어서 질문 올립니다. ^^
Forums:
실행 중인 프로그램의 메모리 구조
우선 실행 중인 프로그램의 메모리 구조를 이해 하실 필요가 있습니다.
어떤 프로그램이 실행이 되면 메모리 상에서
전역 변수 블럭 : 함수 블럭 : 스택 블럭 : 힙 블럭 이렇게 4개의 영역에 각
내용이 담기게 됩니다.
님께서 처음에 적은 buf[256]이 전역 변수라면
전역 변수 블럭에 들어가게 되구요 만약 함수 안에서라면 스택 공간에
들어가게 됩니다. 그리고 p는 4kb가 아니고 4바이트겠지요
그리고 p는 스택 공간에 malloc(256)으로 잡은 것은 힙 공간에 들어
갑니다.
시중에 <How Program works>란 책이 있는데 여기 보면 잘 설명이
되어 있습니다.
댓글 달기