why memory for primitive data types is not allocated?

This question already has an answer here:

  • What and where are the stack and heap? 25 answers

  • When we create an NSString * variable, we don't allocate the memory for this either.

    We only allocate memory when alloc is called, either directly by us or inside a method we call.

    An NSString object exists on the heap in memory we've allocated, but the NSString * variable (which is, a pointer to an NSString object) exists in memory on the stack which we do not allocate.

    For example, given these two variables:

    NSString *stringOne;
    NSString *stringTwo;
    

    So far, neither has been allocated any memory on the heap, although they do exist in memory in the exact same way a char , BOOL , or int exists in memory.

    NSString *stringOne = [[NSString alloc] initWithString:@"Hello world"];
    NSString *stringTwo = stringOne;
    

    Now what has happened? We allocated some memory on the heap for an NSString object. We then initialized this memory to represent the string "Hello world" and then returned a pointer to this object and assigned it to stringOne .

    Next, we simply copied that pointer over into the stack memory we're using for stringTwo . We didn't allocate any extra memory on the heap. We simply made our two string variable point to the same allocated memory on the heap.

    The question and answer jsd linked in the comments has more explanation on stack and heap memory which will answer some of your questions.

    It's also worth noting that a lot of other programming languages such as C++ allow objects to be created on the stack, in which case we don't allocate them, as we would with heap objects. They do exist in memory, just more similarly to primitive data types.


    At the risk of being over simplistic, there are three classes of memory for data: 1) static, 2) stack 3) heap.

    They are allocated in different ways.

    if you have

     static char something ;
    

    defined in a function or

    char something ;
    

    outside of a function, that data is defined by the linker using instructions from the compiler and allocated by the program loaders.

    Nearly every processor in existence uses a stack to support nested data (eg, function calls). The stack is a block of memory that exists for every process (and for every processor mode). There is aa hardware register called the Stack Pointer that identifies the current position of the stack. Usually the SP starts at the high end of the stack and works downward. To allocate memory on the stack, the program subtracts the number of bytes required from the stack pointer. To deallocate, it adds to the stack pointer. The allocations and deallocations always take place at the same end.

    There are then two operations on the stack. PUSH means put something on the stack. POP removes it. Most processors have instructions to PUSH and POP

    If you have

     char something
    

    defined within a function, that memory is allocated by the program as directed by the compiler by doing something like this to adjust the stack pointer (I'm leaving out a frame pointer for now)

     SUB   BYTESNEEDED, SP
    

    upon entering the function and freed by doing

    ADD BYTESNEEDED, SP
    

    before leaving the function. During the execution of the function, the local variables are at offsets from the stack pointer.

    This usually done by using a second register, usually called a frame pointer. A function usually does something like this at the start

    PUSH  FP       ; Save the old Frame Point 
    MOV   SP  FP   ; Save the stack pointer
    SUB   BYTESNEEDED, SP
    

    at the end the function does something like

    MOV FP, SP ; Free all the stack allocated by the function POP FP ; Restore the old stack pointer

    The reason for using two registers is that it is possible to dynamically allocate data from the stack.

    THere is a common function (although I believe it is not a standard C function) called alloca that is an alternative to malloc that allocates from the stack

    void dosomething (int amount)
    {
        char *data = alloca (amount) ;
    } 
    

    With alloca, the data is automatically freed when the function returns and resets the stack.

    That is a long winded answer to your question. Yes, when declare a char, there has to be an allocation for it. However, this allocation is done behind the scenes without effort on your part.

    链接地址: http://www.djcxy.com/p/2266.html

    上一篇: 为什么堆栈大小有限制?

    下一篇: 为什么没有分配原始数据类型的内存?