programing

스택 또는 힙에서 C ++의 글로벌 메모리 관리?

nasanasas 2020. 10. 31. 09:48
반응형

스택 또는 힙에서 C ++의 글로벌 메모리 관리?


C ++ 응용 프로그램에서 데이터 구조를 전역 적으로 선언하면 스택 메모리 또는 힙 메모리를 사용합니까?

예를 들어

struct AAA
{

.../.../.
../../..
}arr[59652323];

나는 대답에 만족하지 않았고, 같은 카르 자 카르가 단순한 예 / 아니오 대답 이상의 것을 배우기를 원했기 때문에 여기에 있습니다.

일반적으로 프로세스에는 5 개의 서로 다른 메모리 영역이 할당됩니다.

  1. 코드-텍스트 세그먼트
  2. 초기화 된 데이터 – 데이터 세그먼트
  3. 초기화되지 않은 데이터 – BS 세그먼트
  4. 더미
  5. 스택

저장 위치를 ​​정말로 알고 싶다면 다음을 읽고 북마크하십시오.

컴파일러, 어셈블러, 링커 및 로더 : 간단한 이야기 (표 w.5 참조)

메모리에있는 프로그램의 구조

대체 텍스트


여기서 문제는 질문입니다. 다음과 같은 작은 C (++) 프로그램이 있다고 가정 해 보겠습니다.

/* my.c */

char * str = "Your dog has fleas.";  /* 1 */
char * buf0 ;                         /* 2 */

int main(){
    char * str2 = "Don't make fun of my dog." ;  /* 3 */
    static char * str3 = str;         /* 4 */
    char * buf1 ;                     /* 5 */
    buf0 = malloc(BUFSIZ);            /* 6 */
    buf1 = malloc(BUFSIZ);            /* 7 */

    return 0;
}
  1. This is neither allocated on the stack NOR on the heap. Instead, it's allocated as static data, and put into its own memory segment on most modern machines. The actual string is also being allocated as static data and put into a read-only segment in right-thinking machines.
  2. is simply a static allocated pointer; room for one address, in static data.
  3. has the pointer allocated on the stack and will be effectively deallocated when main returns. The string, since it's a constant, is allocated in static data space along with the other strings.
  4. is actually allocated exactly like at 2. The static keyword tells you that it's not to be allocated on the stack.
  5. ...but buf1 is on the stack, and
  6. ... the malloc'ed buffer space is on the heap.
  7. And by the way., kids don't try this at home. malloc has a return value of interest; you should always check the return value.

For example:

char * bfr;
if((bfr = malloc(SIZE)) == NULL){
   /* malloc failed OMG */
   exit(-1);
}

Usually it consumes neither. It tries to allocate them in a memory segment which is likely to remain constant-size for the program execution. It might be bss, stack, heap or data.


Neither. It is .data section.


Global memory is pre-allocated in a fixed memory block, or on the heap, depending on how it is allocated by your application:

byte x[10]; // pre-allocated by the compiler in some fixed memory block
byte *y

main()
{
   y = malloc(10); // allocated on the heap
}

EDIT:

The question is confusing: If I allocate a data structure globally in a C++ application , does it consume stack memory or heap memory ?

"allocate"? That could mean many things, including calling malloc(). It would have been different if the question was "if I declare and initialize a data structure globally".

Many years ago, when CPUs were still using 64K segments, some compilers were smart enough to dynamically allocate memory from the heap instead of reserving a block in the .data segment (because of limitations in the memory architecture).

I guess I'm just too old....


Neither declaring a data structure globally in a C++ consumes heap or stack memory. Actually, global variables are typically allocated in a data segment whose size remains unchanged during the whole program. Stacks and heaps are typically used for variables that get created and destroyed during executing the program.

프로그램 메모리 공간


The global object itself will take up memory that the runtime or compiler reserves for it before main is executed, this is not a variable runtime cost so neither stack nor heap.

If the ctor of the object allocates memory it will be in the heap, and any subsequent allocations by the object will be heap allocations.

It depends on the exact nature of the global object, if it's a pointer or the whole object itself that is global.


global variables live on the heap. these are a special case because they live for the life of the program


If you are explicitly allocating the memory yourself by new or malloc, then it will be allocated in heap. If the compiler is allocating the memory, then it will be allocated on stack.

참고URL : https://stackoverflow.com/questions/1169858/global-memory-management-in-c-in-stack-or-heap

반응형