size of pointer

Discussion in 'Programmer's Corner' started by embpic, Apr 16, 2014.

  1. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    what is size of pointer which assign for char, int ??
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,452
    3,371
    A pointer is a memory address. The size of the pointer should be large enough to hold the address. Thus if your addressable memory space is 4GB, the pointer needs to be 32 bits.
     
  3. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    ok meas it store the address so accupy the space for storing this address but i read somewhere that if i used pointer of char then size is 1 byte and if for int then size is 2 byte.
     
  4. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    and if use void pointer then what is advantages and dis-advantages.
     
  5. pujulde

    Member

    Jul 24, 2013
    95
    1
    As i know we have to declare type of data on which our pointer point. So if you point on the char type data it use a byte, if integer it would use the amount of bits of OS
     
  6. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    what about void *
     
  7. robvleugel

    New Member

    Oct 3, 2013
    4
    6
    I'm not into computer science, but my guess is that the size of all pointers are all the same. They just point to a location in memory. The type of the pointer (e.g. int* or char* of my_object*) just indicates what kind of data can be expected. So if you unreference the int* it will return the data the size of an int (for instance 32kb) and give back a number, while if you would use the int* on a my_object it will still give you back the first 32bits of data but this is undefined, while a my_object* pointer would give you back perhaps 256kb containing multiple integers or chars present inside the object.

    Pointer to void therefore still points to a memory location, but you just don't define what kind of data is present there.
     
  8. robvleugel

    New Member

    Oct 3, 2013
    4
    6
    In relation to your other thread: http://forum.allaboutcircuits.com/showthread.php?t=96764

    If you have a void pointer and you want to dereference this, you need to tell explicitly what type as a void pointer normaly returns void (no) data. So:

    Code ( (Unknown Language)):
    1.  
    2. int myInteger = 5;
    3. void* myVoidPointer = &myInteger;            // myVoidPointer now stored the adress of myInteger
    4. int mySecondInteger = myVoidPointer*;      // error, cannot convert void to int
    5. int myThirdInteger = (int*)myVoidPointer*; // should work fine
    6.  
    I havent tested it though, and its been a while since Ive touched C.
     
  9. vpoko

    Member

    Jan 5, 2012
    258
    47
    All pointers are exactly the same size, that being the size of your CPU architecture, so either 32 or 64 bits these days. The only difference between int*, char*, and void* is what they point to is different, and that comes into play when you try to dereference the pointer or do pointer arithmetic.

    For example, say you declare an int array and then declare a pointer, "int* ptr", that contains the address of the first element of the array. If you do "ptr++", you will now be pointing to the second element of the array. If, on the other hand, ptr is a void*, then you will only increment the address by one byte, and you'll be pointing into the middle of the first element, instead.
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    Pointer arithmetic is undefined for a void pointer, so it should throw a syntax error.
     
  11. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    A void pointer allows you to have a pointer that you may not know what kind of data it is going to point to at compile time. Of course, that means that your code will have to deal with it at run time.
     
  12. vpoko

    Member

    Jan 5, 2012
    258
    47
    Hmm, maybe it depends on the compiler. GCC treats a void* exactly like a char* for the purpose of arithmetic, it increments it one byte.
     
  13. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    If you set the options of gcc to be ANSI-compliant, it will throw an error. But gcc (and some other compilers) allow pointer arithmetic on void pointers as a non-standard extension. The result is code that compiles on gcc and then won't compile (or will compile but behave differently) on another compiler.

    The best way is to cast the void pointer as an unsigned char before doing pointer arithmetic on it if you want it to behave as if it is pointing to 1-byte data values.
     
    vpoko likes this.
  14. fernan82

    New Member

    Apr 19, 2014
    26
    9
    Undefined means that the implementation is free to choose how to handle it and not all GCCs will increment it by one byte. It is usually treated as the smallest unit of addressable memory in the architecture which is not always one byte. In some DSPs for example it is 16-bits (2 bytes). Then on those architectures char is normally 2 bytes also so it is almost always treated as char* but not guaranteed.

    Usually you'll use void* when you don't care what the pointer is. An example is memcpy since it simply copies the memory it doesn't care what type of pointer you give it.

    Also pointers are not always the same size. They usually are in most modern desktop environments (at least as far as user mode applications are concerned) but on architectures with banked or segmented memory there may be different types of pointers usually refered to as near and far.
     
    vpoko likes this.
  15. embpic

    Thread Starter Member

    May 29, 2013
    187
    3
    Bit diffdifferent question if I use
    Code ( (Unknown Language)):
    1.  
    2. Struct
    3. {
    4.  
    5. Unsigned:1;
    6. };
    7.  
    Then it is related to char or int
     
Loading...