where do they store??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanna88
    New Member
    • Oct 2009
    • 33

    where do they store??

    Code:
    tmain(intargc, _TCHAR* argv[]){
    int*p = new int;
    *p = 0;
    cout<< *p << endl;}
    where will p and *p will be stored? and in what other condition this can be changed to other part in memory like stack,heap,BSS, data,code??

    my ans for this would be p stored in heap due to the new command and *p will be stored in BSS since it is initialised to zero. but im not sure whether that is right.



    need some conformation..

    thanks
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    I agree that the new will generate data from the heap.

    As for the pointer, it is located the stack, because it is a local variable to main.

    BSS usually refers to global variables that are automatically initialized to a 0 value. In this case, the pointer is initialized to 0, but the pointer is not global.

    Comment

    • hanna88
      New Member
      • Oct 2009
      • 33

      #3
      thank you for your input :D

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by RRick
        BSS usually refers to global variables that are automatically initialized to a 0 value. In this case, the pointer is initialized to 0, but the pointer is not global.
        The pointer is not initialised to zero, the pointer is initialised to an unknown value that is the address of an int that has been allocated on the heap.

        The value 0 is then assigned to the int allocated on the heap.

        Nothing in this code initialises anything to 0.

        On the whole it is not very easy to store data in the code segement (which holds program code) , for a description of the various segements read this and then come back here is you have further questions.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          There are some typical rules that are followed by typical compilers for where certain kinds of variables are stored; but I don't believe there are any definitive rules that apply for all C/C++ compilers.

          Do you think you're being asked for the typical rules; or do you think you're being asked how some particular odd-ball compiler does it?

          Comment

          • hanna88
            New Member
            • Oct 2009
            • 33

            #6
            if im right does it means...

            a) bss stores unitialized global variable and static member
            b) data stores the initialized global variable
            c) stack is the local variable without static and new/free/malloc//.... command
            d) heap is the one with new//malloc...etc command

            is that right? so what about text?

            Code:
            #include <string.h>
            
            char*str; [B]// bss[/B]
            int size; [B]// bss[/B]
            
            void foo(char*in){
            
            [B]// str - heap, *str-stack , str= new char[size];  -> text[/B]
            str= new char[size];  
                                       
            int sz = strlen(in); [B]// sz,in in stack[/B]
            if (sz>size)
               strcpy(str,"bye"); [B]// bye in stack[/B]
            else
               strcpy(str,in);
            }
            thanks again

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by hanna88
              c) stack is the local variable without static and new/free/malloc//.... command
              d) heap is the one with new//malloc...etc command
              You said malloc'd data is in the stack and on the heap. It can't be in 2 places. Ignoring for the moment that there is no requirement for there to be a stack or a heap (I have to say I have yet to com accross an implementation where there wasn't), malloc'd (including data allocated with new) data is normally on the heap (if the implementation has one).

              Originally posted by hanna88
              is that right? so what about text?
              The text segment is another name for the code segment, that is it holds program executable code.

              Originally posted by hanna88
              Code:
              #include <string.h>
              
              char*str; [B]// bss[/B]
              int size; [B]// bss[/B]
              
              void foo(char*in){
              
              [B]// str - heap, *str-stack , str= new char[size];  -> text[/B]
              [b]// SEE NOTE 1[/b]
              str= new char[size];  
                                         
              int sz = strlen(in); [B]// sz,in in stack[/B]
              if (sz>size)
                 strcpy(str,"bye"); [B]// SEE NOTE 2 bye in stack[/B]
              else
                 strcpy(str,in);
              }
              1. You have now indicated that str is on bss and stack your first statement on line 4 is correct the code on line 11 does not change the location of str. The stack contains things automatically allocated when a block of code, such as a function is entered. *str is not automatically created you created it with new. It has been created with new therefore it is on the heap. You do not find data in the text segment as it holds executable code. Also you appear tothink that there are 3 objects in 3 different locations but there are only 2 objects str and *str, they are stored in different places.
              2. No "bye" is not an automatically created variable, it is a constant. Some platforms (embedded platforms for instance) have a special constant data segment and store this segment in non-modifiable memory so they are truly constant. Most PC platforms (Linux and Windows) just put these into the data segment since they don't have non-modifiable memory. Numerical constants go into the code or text segment, that is they appear directly in the executable code. A variable declared as const would also go into the constant data segment if a platform has one.

              Comment

              • hanna88
                New Member
                • Oct 2009
                • 33

                #8
                A variable declared as const would also go into the constant data segment if a platform has one
                can you give me an example for this?
                let say the variable is declare in the main func wouldnt it be in stack instead?

                thank you for your reply

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Code:
                  str= new char[size];
                  ...
                  strcpy(str, "bye");
                  The string constant "bye" is not an explicit variable in your program. (What is the name of the variable? It has no name.)


                  Typically, the compiler generates code as if you had written the following.
                  Code:
                  str = new char[size];
                  ...
                  {
                    static const char bye[] = "bye";
                    strcpy(str, bye);
                  }
                  Some compilers will put the implicit variable bye in a special constant segment, some will put it in the text segment, and some will put it in the data segment. The advantage of putting it in the constant or text segment is that this provides run-time support for const-ness.

                  Comment

                  Working...