Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pointer [modified]

pointer [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
19 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jith iii
    wrote on last edited by
    #1

    Hi everyone, i dont have any experience in VC++.I know some C and C++, thats all.May be silly,but out of curiosity I am asking this question. If we delare a pointer as int * p ,it will create an integer pointer p.It will reserve 2 bytes in memory also.How this two memory locations and *p is tagged together or who is allocating these memory locations to p(Like,the rule p should hold only addresses). say the locations are 1000 and 1001. consider this, and please check my interpretations are correct or not; int j; *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) (will this work .here p does not hold any address value and storing value 2 directly in their address space) printf("%d",p) //prints 1000 p=&j; // now doubt,here how 1000,1001 store the address of j j=4; // *p and j will print the same value. Please give your comments about this or please direct me if you know some good stuffs regarding the memory concepts in programming. Thank you -- modified at 0:55 Friday 26th May, 2006

    PJ ArendsP S U S 4 Replies Last reply
    0
    • J jith iii

      Hi everyone, i dont have any experience in VC++.I know some C and C++, thats all.May be silly,but out of curiosity I am asking this question. If we delare a pointer as int * p ,it will create an integer pointer p.It will reserve 2 bytes in memory also.How this two memory locations and *p is tagged together or who is allocating these memory locations to p(Like,the rule p should hold only addresses). say the locations are 1000 and 1001. consider this, and please check my interpretations are correct or not; int j; *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) (will this work .here p does not hold any address value and storing value 2 directly in their address space) printf("%d",p) //prints 1000 p=&j; // now doubt,here how 1000,1001 store the address of j j=4; // *p and j will print the same value. Please give your comments about this or please direct me if you know some good stuffs regarding the memory concepts in programming. Thank you -- modified at 0:55 Friday 26th May, 2006

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      http://www.codeproject.com/cpp/pointers.asp[^] http://www.codeproject.com/cpp/pointerprelude.asp[^]


      You may be right
      I may be crazy
      -- Billy Joel --

      Within you lies the power for good, use it!!!

      Within you lies the power for good; Use it!

      1 Reply Last reply
      0
      • J jith iii

        Hi everyone, i dont have any experience in VC++.I know some C and C++, thats all.May be silly,but out of curiosity I am asking this question. If we delare a pointer as int * p ,it will create an integer pointer p.It will reserve 2 bytes in memory also.How this two memory locations and *p is tagged together or who is allocating these memory locations to p(Like,the rule p should hold only addresses). say the locations are 1000 and 1001. consider this, and please check my interpretations are correct or not; int j; *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) (will this work .here p does not hold any address value and storing value 2 directly in their address space) printf("%d",p) //prints 1000 p=&j; // now doubt,here how 1000,1001 store the address of j j=4; // *p and j will print the same value. Please give your comments about this or please direct me if you know some good stuffs regarding the memory concepts in programming. Thank you -- modified at 0:55 Friday 26th May, 2006

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        It's all pretty simple, once you get the hang of it. When you declare a pointer, the compiler allocates a certain number of bytes (depending on the platform) on the stack. The size of the pointer is derived from the maximum address space that it can point to. In a 32 bit environment running Windows, each process gets 4GB of address space, so a pointer must be big enough to point at any address from 0 to 0xFFFFFFFF. The size of the pointer is therefore log2(4294967296), which is equal to 32 bits. You can do the same calculation for a 64 bit environment. This memory allocated by the compiler holds the address of whatever the pointer is pointing to, at runtime.

        jithAtran - ii wrote:

        *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010)

        I don't get what 1000 and 1001 are. If you consider them to be address of the pointer itself (&p), then your comment is incorrect. If you consider them to be the address where the pointer is pointing at, then your interpretation is correct. I'll assume you meant the latter.

        jithAtran - ii wrote:

        p=&j; // now doubt,here how 1000,1001 store the address of j

        All this does is change where p is pointing at. p will no longer hold 1000/1001, instead, it will hold whatever is the address of j (2000, 2001, for example). That explains why *p and j hold the same value.

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        J S 2 Replies Last reply
        0
        • S S Senthil Kumar

          It's all pretty simple, once you get the hang of it. When you declare a pointer, the compiler allocates a certain number of bytes (depending on the platform) on the stack. The size of the pointer is derived from the maximum address space that it can point to. In a 32 bit environment running Windows, each process gets 4GB of address space, so a pointer must be big enough to point at any address from 0 to 0xFFFFFFFF. The size of the pointer is therefore log2(4294967296), which is equal to 32 bits. You can do the same calculation for a 64 bit environment. This memory allocated by the compiler holds the address of whatever the pointer is pointing to, at runtime.

          jithAtran - ii wrote:

          *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010)

          I don't get what 1000 and 1001 are. If you consider them to be address of the pointer itself (&p), then your comment is incorrect. If you consider them to be the address where the pointer is pointing at, then your interpretation is correct. I'll assume you meant the latter.

          jithAtran - ii wrote:

          p=&j; // now doubt,here how 1000,1001 store the address of j

          All this does is change where p is pointing at. p will no longer hold 1000/1001, instead, it will hold whatever is the address of j (2000, 2001, for example). That explains why *p and j hold the same value.

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          J Offline
          J Offline
          jith iii
          wrote on last edited by
          #4

          Thank you Senthil, for the detailed reply.

          S. Senthil Kumar wrote:

          jithAtran - ii wrote: *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) I don't get what 1000 and 1001 are. If you consider them to be address of the pointer itself (&p), then your comment is incorrect. If you consider them to be the address where the pointer is pointing at, then your interpretation is correct. I'll assume you meant the latter.

          No I assumed the address of P itself as 1000/1001.what will happen then if we do * p=2;

          S. Senthil Kumar wrote:

          jithAtran - ii wrote: p=&j; // now doubt,here how 1000,1001 store the address of j All this does is change where p is pointing at. p will no longer hold 1000/1001, instead, it will hold whatever is the address of j (2000, 2001, for example). That explains why *p and j hold the same value

          I know both p and j hold the same value. I think I should state my silly doubt more clearly, if p is a ponter it should hold the address p=&j //should hold the address 2000/2001 as in your example. the address of p is 1000/1001 and how it store the addresses 2000/2001 in bit format considering 1000 will hold 4 bits and 1001 will also hold 4 bits Thank you again.

          S 1 Reply Last reply
          0
          • J jith iii

            Hi everyone, i dont have any experience in VC++.I know some C and C++, thats all.May be silly,but out of curiosity I am asking this question. If we delare a pointer as int * p ,it will create an integer pointer p.It will reserve 2 bytes in memory also.How this two memory locations and *p is tagged together or who is allocating these memory locations to p(Like,the rule p should hold only addresses). say the locations are 1000 and 1001. consider this, and please check my interpretations are correct or not; int j; *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) (will this work .here p does not hold any address value and storing value 2 directly in their address space) printf("%d",p) //prints 1000 p=&j; // now doubt,here how 1000,1001 store the address of j j=4; // *p and j will print the same value. Please give your comments about this or please direct me if you know some good stuffs regarding the memory concepts in programming. Thank you -- modified at 0:55 Friday 26th May, 2006

            U Offline
            U Offline
            User 3037427
            wrote on last edited by
            #5

            hi, pointer holds the address of a variable. u r saying that integer 2 is stored in 1000 and 1001 as 0000 0010. Now p points to 1000, which is the base address of integer 2. In the case of p=&j j=4; pointer will be adjusted to the address of j. Hence *p and j will prints the same value.

            1 Reply Last reply
            0
            • J jith iii

              Hi everyone, i dont have any experience in VC++.I know some C and C++, thats all.May be silly,but out of curiosity I am asking this question. If we delare a pointer as int * p ,it will create an integer pointer p.It will reserve 2 bytes in memory also.How this two memory locations and *p is tagged together or who is allocating these memory locations to p(Like,the rule p should hold only addresses). say the locations are 1000 and 1001. consider this, and please check my interpretations are correct or not; int j; *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) (will this work .here p does not hold any address value and storing value 2 directly in their address space) printf("%d",p) //prints 1000 p=&j; // now doubt,here how 1000,1001 store the address of j j=4; // *p and j will print the same value. Please give your comments about this or please direct me if you know some good stuffs regarding the memory concepts in programming. Thank you -- modified at 0:55 Friday 26th May, 2006

              S Offline
              S Offline
              Saday Sarkar
              wrote on last edited by
              #6

              int * p ; //"say the locations are 1000 and 1001."-it's ok.. //that means The locations 1000 and 1001 are rady to hold address //of an integer. If write *p=2; //means you are trying to assign integer value on the address //which is store on 1000 and 1001. //So it will cause acess violation.at run time. // we can do this after assign p=&j; p=&j; //Then *p=2; //means the Now 1000 and 1001 is holding the address of "j"; //And the value of that Address will be 2; //Now *p and j will print the same value. Saday Chand Sarkar Software Engineer Trek Technology(s)Pte.Ltd.

              J 1 Reply Last reply
              0
              • J jith iii

                Thank you Senthil, for the detailed reply.

                S. Senthil Kumar wrote:

                jithAtran - ii wrote: *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010) I don't get what 1000 and 1001 are. If you consider them to be address of the pointer itself (&p), then your comment is incorrect. If you consider them to be the address where the pointer is pointing at, then your interpretation is correct. I'll assume you meant the latter.

                No I assumed the address of P itself as 1000/1001.what will happen then if we do * p=2;

                S. Senthil Kumar wrote:

                jithAtran - ii wrote: p=&j; // now doubt,here how 1000,1001 store the address of j All this does is change where p is pointing at. p will no longer hold 1000/1001, instead, it will hold whatever is the address of j (2000, 2001, for example). That explains why *p and j hold the same value

                I know both p and j hold the same value. I think I should state my silly doubt more clearly, if p is a ponter it should hold the address p=&j //should hold the address 2000/2001 as in your example. the address of p is 1000/1001 and how it store the addresses 2000/2001 in bit format considering 1000 will hold 4 bits and 1001 will also hold 4 bits Thank you again.

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                jithAtran - ii wrote:

                the address of p is 1000/1001 and how it store the addresses 2000/2001 in bit format considering 1000 will hold 4 bits and 1001 will also hold 4 bits

                Like I said in my previous post, the size of a pointer is dependent on the platform. And address is different from the size of a variable. On Win32, a pointer is 4 bytes. If the address of p is 1000, then bytes 1000, 1001, 1002 and 1003 will be available for p. 4 bytes is equal to 32 bits and 232 is the maximum address value you can point at. Taking your example, assuming a 16 bit environment, then the address of p will be 1000 and it will have two bytes allocated to it, 1000 and 1001. A byte is 8 bits, so 1000 can store 8 bits and 1001 can store 8 bits, for a total of 16 bits. For storing 2000, depending on the endianness, the compiler might do <pre> _________________ __00___|___7D_____ 1000        1001 </pre> 7D is 2000 expressed in hex. Does this answer your question?

                Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                J 1 Reply Last reply
                0
                • S S Senthil Kumar

                  jithAtran - ii wrote:

                  the address of p is 1000/1001 and how it store the addresses 2000/2001 in bit format considering 1000 will hold 4 bits and 1001 will also hold 4 bits

                  Like I said in my previous post, the size of a pointer is dependent on the platform. And address is different from the size of a variable. On Win32, a pointer is 4 bytes. If the address of p is 1000, then bytes 1000, 1001, 1002 and 1003 will be available for p. 4 bytes is equal to 32 bits and 232 is the maximum address value you can point at. Taking your example, assuming a 16 bit environment, then the address of p will be 1000 and it will have two bytes allocated to it, 1000 and 1001. A byte is 8 bits, so 1000 can store 8 bits and 1001 can store 8 bits, for a total of 16 bits. For storing 2000, depending on the endianness, the compiler might do <pre> _________________ __00___|___7D_____ 1000        1001 </pre> 7D is 2000 expressed in hex. Does this answer your question?

                  Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                  J Offline
                  J Offline
                  jith iii
                  wrote on last edited by
                  #8

                  Thanks for the reply, Now I am getting some clues about this and I am recalling my digital electronics lessons.Dont mind when I ask about the basics.I am more with asp.net

                  S. Senthil Kumar wrote:

                  A byte is 8 bits

                  Oh...it was my mistake But I knew that Now the only doubt would be about the declaration. int *p ,float *q //integer pointer. int i // normal integer. regardless of the datatype compiler would allocate 4 bytes to p and q. say 1000/1001 holds an address where an integer is stored.Where would the information about the data type be stored. since 1000/1001 stores only points to some address location where the information regarding "*p='c' should not be allowed" willbe stored. Does the compiler holds anything like a stack to store the datatype? (I hope you will get my mind..I feel I have not clearly stated it ...) Thanks -- modified at 5:07 Friday 26th May, 2006

                  S 1 Reply Last reply
                  0
                  • S Saday Sarkar

                    int * p ; //"say the locations are 1000 and 1001."-it's ok.. //that means The locations 1000 and 1001 are rady to hold address //of an integer. If write *p=2; //means you are trying to assign integer value on the address //which is store on 1000 and 1001. //So it will cause acess violation.at run time. // we can do this after assign p=&j; p=&j; //Then *p=2; //means the Now 1000 and 1001 is holding the address of "j"; //And the value of that Address will be 2; //Now *p and j will print the same value. Saday Chand Sarkar Software Engineer Trek Technology(s)Pte.Ltd.

                    J Offline
                    J Offline
                    jith iii
                    wrote on last edited by
                    #9

                    Thank you.I have doubt only in the memory part..I think I understand how values are being manupulated through pointers can we do like this... int *p,x // allocating 4 bytes for pinter p in memory; float k //allocating 4 bytes. p=&k // this will work very well. Now, k=&p // what will happen now or k=&x;// if k has enough space to store the address of it can hold. But then *k should work ,I know,which will not Thanks -- modified at 3:15 Friday 26th May, 2006

                    S 1 Reply Last reply
                    0
                    • J jith iii

                      Thanks for the reply, Now I am getting some clues about this and I am recalling my digital electronics lessons.Dont mind when I ask about the basics.I am more with asp.net

                      S. Senthil Kumar wrote:

                      A byte is 8 bits

                      Oh...it was my mistake But I knew that Now the only doubt would be about the declaration. int *p ,float *q //integer pointer. int i // normal integer. regardless of the datatype compiler would allocate 4 bytes to p and q. say 1000/1001 holds an address where an integer is stored.Where would the information about the data type be stored. since 1000/1001 stores only points to some address location where the information regarding "*p='c' should not be allowed" willbe stored. Does the compiler holds anything like a stack to store the datatype? (I hope you will get my mind..I feel I have not clearly stated it ...) Thanks -- modified at 5:07 Friday 26th May, 2006

                      S Offline
                      S Offline
                      S Senthil Kumar
                      wrote on last edited by
                      #10

                      jithAtran - ii wrote:

                      Does the compiler holds anything like a stack to store the datatype?

                      No, and there is no reason to. At compile time, the compiler knows that p is a pointer to int and q is a pointer to float, so when you do *p or *q, it knows how many bytes to read to get the value, so it generates code to do that. So if you write to a pointer with code like *p = 2 it generates code to write to the first four (or two, in your example) bytes of the address pointed to by p. So yeah, at runtime, there is no information available about the pointer. The compiler cannot prevent code like this from crashing

                      char a = 'a';
                      char *pChar = &a;
                      void *pVoid = pChar;
                      int *pInt = (int *)pVoid;
                      *pInt = 12345;

                      At runtime, the last line will cause problems, as 4 bytes will be written where only one byte has been allocated. The compiler cannot catch such problems, because it does not store the information anywhere.

                      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                      J 1 Reply Last reply
                      0
                      • S S Senthil Kumar

                        jithAtran - ii wrote:

                        Does the compiler holds anything like a stack to store the datatype?

                        No, and there is no reason to. At compile time, the compiler knows that p is a pointer to int and q is a pointer to float, so when you do *p or *q, it knows how many bytes to read to get the value, so it generates code to do that. So if you write to a pointer with code like *p = 2 it generates code to write to the first four (or two, in your example) bytes of the address pointed to by p. So yeah, at runtime, there is no information available about the pointer. The compiler cannot prevent code like this from crashing

                        char a = 'a';
                        char *pChar = &a;
                        void *pVoid = pChar;
                        int *pInt = (int *)pVoid;
                        *pInt = 12345;

                        At runtime, the last line will cause problems, as 4 bytes will be written where only one byte has been allocated. The compiler cannot catch such problems, because it does not store the information anywhere.

                        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                        J Offline
                        J Offline
                        jith iii
                        wrote on last edited by
                        #11

                        So i am trying to summarize my doubts.Correct me if I am wrong...

                          int *i;
                              static float *k;
                              int j;
                              i=&j;
                              k=&j;//points to the same memory location
                              printf("%d ,%f",*i,*k)
                        

                        The procedure would be like this compiler allocates 4 bytes each for pointer i and integer j. and when we prints i and k compiler reads byte information fromthe same or contigious addresses with respect to the datatype. I still have doubt in this part.How compiler knows i is an integer.where the command "You should read only 4 bytes for i since its an integer" is stored. of course we have declared like int *i and it stores only an address space.Will it throw error if we performed like this. float q; i=&q; Thank you -- modified at 5:34 Friday 26th May, 2006

                        S 1 Reply Last reply
                        0
                        • J jith iii

                          So i am trying to summarize my doubts.Correct me if I am wrong...

                            int *i;
                                static float *k;
                                int j;
                                i=&j;
                                k=&j;//points to the same memory location
                                printf("%d ,%f",*i,*k)
                          

                          The procedure would be like this compiler allocates 4 bytes each for pointer i and integer j. and when we prints i and k compiler reads byte information fromthe same or contigious addresses with respect to the datatype. I still have doubt in this part.How compiler knows i is an integer.where the command "You should read only 4 bytes for i since its an integer" is stored. of course we have declared like int *i and it stores only an address space.Will it throw error if we performed like this. float q; i=&q; Thank you -- modified at 5:34 Friday 26th May, 2006

                          S Offline
                          S Offline
                          S Senthil Kumar
                          wrote on last edited by
                          #12

                          jithAtran - ii wrote:

                          I still have doubt in this part.How compiler knows i is an integer.where the command "You should read only 4 bytes for i since its an integer" is stored.

                          First, i is not an integer, it is a pointer to an integer. Second, the compiler does NOT store the information anywhere, it simply uses the information when it generates the machine code. When the compiler parses the program, it knows that i is of type int *, so it knows that whatever i is pointing at is an integer. Whenever it sees it being referenced/dereferenced, it uses that fact that i is a pointer to an integer to generate the correct code. To summarize, the information that i is a pointer to an integer is not stored anywhere, the compiler just generates code that treats i as a pointer to an integer.

                          jithAtran - ii wrote:

                          Will it throw error if we performed like this. float q; i=&q;

                          Yes, it will. Like I said, at compile time, the compiler knows that i is a pointer to an integer, so it obviously won't allow it to point at a float. Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

                          J 1 Reply Last reply
                          0
                          • S S Senthil Kumar

                            It's all pretty simple, once you get the hang of it. When you declare a pointer, the compiler allocates a certain number of bytes (depending on the platform) on the stack. The size of the pointer is derived from the maximum address space that it can point to. In a 32 bit environment running Windows, each process gets 4GB of address space, so a pointer must be big enough to point at any address from 0 to 0xFFFFFFFF. The size of the pointer is therefore log2(4294967296), which is equal to 32 bits. You can do the same calculation for a 64 bit environment. This memory allocated by the compiler holds the address of whatever the pointer is pointing to, at runtime.

                            jithAtran - ii wrote:

                            *p=2; // integer 2 is stored in 1000 and 1001 as(0000 0010)

                            I don't get what 1000 and 1001 are. If you consider them to be address of the pointer itself (&p), then your comment is incorrect. If you consider them to be the address where the pointer is pointing at, then your interpretation is correct. I'll assume you meant the latter.

                            jithAtran - ii wrote:

                            p=&j; // now doubt,here how 1000,1001 store the address of j

                            All this does is change where p is pointing at. p will no longer hold 1000/1001, instead, it will hold whatever is the address of j (2000, 2001, for example). That explains why *p and j hold the same value.

                            Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #13

                            In general what you say is correct. I have a minor correction to the following however:

                            S. Senthil Kumar wrote:

                            the compiler allocates a certain number of bytes (depending on the platform) on the stack.

                            This is not always the case: the pointer need not be allocated on the stack. In fact the allocation of storage is an issue that concerns any variable and not just pointers. Consider the following: struct Data {     int number;     int *pNumber; }; // Code like this will allocate the memory for a "Data" struct on the stack. // So the member "number" (not a pointer at all) and "pNumber" (is a pointer) // will both be allocated on the stack. void Function() {     Data IsOnTheStack;     // Do stuff... } // Code like this will not use the stack. // The memory will be in a data segment of the executable. Data InDataSegment; // At global scope. // Now code like this will place the struct (and its members) on the heap. // The storage for the actual pointer "pData" may be on the heap, stack or // a data segment in a module but the actual structure and it members are // on the heap. Data *pData = new Data; So in short pointers need not be on the stack and in fact where they are is not related to the fact they are pointers but the context. Steve

                            S 1 Reply Last reply
                            0
                            • J jith iii

                              Thank you.I have doubt only in the memory part..I think I understand how values are being manupulated through pointers can we do like this... int *p,x // allocating 4 bytes for pinter p in memory; float k //allocating 4 bytes. p=&k // this will work very well. Now, k=&p // what will happen now or k=&x;// if k has enough space to store the address of it can hold. But then *k should work ,I know,which will not Thanks -- modified at 3:15 Friday 26th May, 2006

                              S Offline
                              S Offline
                              Stephen Hewitt
                              wrote on last edited by
                              #14

                              An int* pointer can not (without perverse casting) point to a float. Simple pointers like the ones you're using will always be a fixed size; on 32-bit Windows this will be 4 bytes. The actual value of a pointer (the bit pattern in memory) indicates a location in memory where some data starts; the type of the pointer tells the compiler what type of data to expect. The example you gave will not compile - BUT if it did this is what it would be saying:  Make the pointer "p" point the data for the variable "k". When the user indirects through the pointer "p" treat the data as if it was an integer. Steve

                              J 1 Reply Last reply
                              0
                              • S S Senthil Kumar

                                jithAtran - ii wrote:

                                I still have doubt in this part.How compiler knows i is an integer.where the command "You should read only 4 bytes for i since its an integer" is stored.

                                First, i is not an integer, it is a pointer to an integer. Second, the compiler does NOT store the information anywhere, it simply uses the information when it generates the machine code. When the compiler parses the program, it knows that i is of type int *, so it knows that whatever i is pointing at is an integer. Whenever it sees it being referenced/dereferenced, it uses that fact that i is a pointer to an integer to generate the correct code. To summarize, the information that i is a pointer to an integer is not stored anywhere, the compiler just generates code that treats i as a pointer to an integer.

                                jithAtran - ii wrote:

                                Will it throw error if we performed like this. float q; i=&q;

                                Yes, it will. Like I said, at compile time, the compiler knows that i is a pointer to an integer, so it obviously won't allow it to point at a float. Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

                                J Offline
                                J Offline
                                jith iii
                                wrote on last edited by
                                #15

                                Thank you Senthil, Now I think I am pretty clear about these concepts .Yeterday, I referred the book of computer organization by hamacher and pointers in C.Your explanations helped me to sum up the things logically. yeah ..But ..only this

                                S. Senthil Kumar wrote:

                                the compiler knows that i is a pointer to an integer

                                How? Correct me if I am wrong, Lets declare like int *i; compiler parses this. That means i has been given an address space in the memory(1000/1001). Now we are trying to store an address say like ______________ __00__|___7D__ where 007D is the address of a 1000 1001 float variable. Now here is my doubt.How compiler knows this assignment is wrong.Obviously,it is the alloted space for an intger.But how does compiler keeps track that 1001/1002 are for integer. i am sorry,I think ,i have to still understand the concepts Thank you.

                                S 1 Reply Last reply
                                0
                                • S Stephen Hewitt

                                  An int* pointer can not (without perverse casting) point to a float. Simple pointers like the ones you're using will always be a fixed size; on 32-bit Windows this will be 4 bytes. The actual value of a pointer (the bit pattern in memory) indicates a location in memory where some data starts; the type of the pointer tells the compiler what type of data to expect. The example you gave will not compile - BUT if it did this is what it would be saying:  Make the pointer "p" point the data for the variable "k". When the user indirects through the pointer "p" treat the data as if it was an integer. Steve

                                  J Offline
                                  J Offline
                                  jith iii
                                  wrote on last edited by
                                  #16

                                  Thank you stephen, Your reply forcing me to study stack and heap concepts of memory more deeply, though I know it theoratically.It would be nice,If you can point me to some good stuffs and articles. Thank you...

                                  1 Reply Last reply
                                  0
                                  • S Stephen Hewitt

                                    In general what you say is correct. I have a minor correction to the following however:

                                    S. Senthil Kumar wrote:

                                    the compiler allocates a certain number of bytes (depending on the platform) on the stack.

                                    This is not always the case: the pointer need not be allocated on the stack. In fact the allocation of storage is an issue that concerns any variable and not just pointers. Consider the following: struct Data {     int number;     int *pNumber; }; // Code like this will allocate the memory for a "Data" struct on the stack. // So the member "number" (not a pointer at all) and "pNumber" (is a pointer) // will both be allocated on the stack. void Function() {     Data IsOnTheStack;     // Do stuff... } // Code like this will not use the stack. // The memory will be in a data segment of the executable. Data InDataSegment; // At global scope. // Now code like this will place the struct (and its members) on the heap. // The storage for the actual pointer "pData" may be on the heap, stack or // a data segment in a module but the actual structure and it members are // on the heap. Data *pData = new Data; So in short pointers need not be on the stack and in fact where they are is not related to the fact they are pointers but the context. Steve

                                    S Offline
                                    S Offline
                                    S Senthil Kumar
                                    wrote on last edited by
                                    #17

                                    Stephen Hewitt wrote:

                                    This is not always the case: the pointer need not be allocated on the stack.

                                    Agreed. I just didn't want to confuse the OP more by attempting to explain where pointers themselves are allocated :) Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

                                    1 Reply Last reply
                                    0
                                    • J jith iii

                                      Thank you Senthil, Now I think I am pretty clear about these concepts .Yeterday, I referred the book of computer organization by hamacher and pointers in C.Your explanations helped me to sum up the things logically. yeah ..But ..only this

                                      S. Senthil Kumar wrote:

                                      the compiler knows that i is a pointer to an integer

                                      How? Correct me if I am wrong, Lets declare like int *i; compiler parses this. That means i has been given an address space in the memory(1000/1001). Now we are trying to store an address say like ______________ __00__|___7D__ where 007D is the address of a 1000 1001 float variable. Now here is my doubt.How compiler knows this assignment is wrong.Obviously,it is the alloted space for an intger.But how does compiler keeps track that 1001/1002 are for integer. i am sorry,I think ,i have to still understand the concepts Thank you.

                                      S Offline
                                      S Offline
                                      S Senthil Kumar
                                      wrote on last edited by
                                      #18

                                      jithAtran - ii wrote:

                                      That means i has been given an address space in the memory(1000/1001). Now we are trying to store an address say like ______________ __00__|___7D__ where 007D is the address of a 1000 1001 float variable. Now here is my doubt.How compiler knows this assignment is wrong.

                                      Well, you're not doing

                                      int *p = 0x007D;

                                      are you? You are attempting to assign the address of a float variable to an integer pointer, so that's why the assignment is wrong. The compiler knows this because, when it compiles code, it keeps track of the type of all variables, using something called a symbol table[^]. When it's time to generate code, it sees this line

                                      float k = 10.0;
                                      int *p = &k;

                                      it realizes that p is a pointer to int and k is a float and therefore generates an error. If you want to know how it gets that information from the source code, google for "Parsing and Syntax Analysis", which is a pretty huge topic in itself. Again, the compiler doesn't know addresses 1001/1002 are for pointing to integers, rather, it only knows that p is a pointer to an integer. The absolute address of the pointer itself is determined at runtime. Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

                                      J 1 Reply Last reply
                                      0
                                      • S S Senthil Kumar

                                        jithAtran - ii wrote:

                                        That means i has been given an address space in the memory(1000/1001). Now we are trying to store an address say like ______________ __00__|___7D__ where 007D is the address of a 1000 1001 float variable. Now here is my doubt.How compiler knows this assignment is wrong.

                                        Well, you're not doing

                                        int *p = 0x007D;

                                        are you? You are attempting to assign the address of a float variable to an integer pointer, so that's why the assignment is wrong. The compiler knows this because, when it compiles code, it keeps track of the type of all variables, using something called a symbol table[^]. When it's time to generate code, it sees this line

                                        float k = 10.0;
                                        int *p = &k;

                                        it realizes that p is a pointer to int and k is a float and therefore generates an error. If you want to know how it gets that information from the source code, google for "Parsing and Syntax Analysis", which is a pretty huge topic in itself. Again, the compiler doesn't know addresses 1001/1002 are for pointing to integers, rather, it only knows that p is a pointer to an integer. The absolute address of the pointer itself is determined at runtime. Regards Senthil _____________________________ My Blog | My Articles | My Flickr | WinMacro

                                        J Offline
                                        J Offline
                                        jith iii
                                        wrote on last edited by
                                        #19

                                        thanks senthil, Now I got the clue.I was also googling to know more abou this.And I thought I am pretty clear on the topic now and took the C test of brainbench(Don't misunderstand. that was not my aim). But Failed!!!! I got only 2.72 where the cut off mark was 2.75. They said I was weak in pointers and functions.. Ok ...thanks for the help [modified at 5.34 Indian Time] Yeah..I got through this time .Thank you... [/modified] -- modified at 8:07 Saturday 27th May, 2006

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups