size of pointer

MrChips

Joined Oct 2, 2009
30,806
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.
 

Thread Starter

embpic

Joined May 29, 2013
189
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.
 

pujulde

Joined Jul 24, 2013
111
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
 
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.
 
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:

Rich (BB code):
int myInteger = 5;
void* myVoidPointer = &myInteger;            // myVoidPointer now stored the adress of myInteger
int mySecondInteger = myVoidPointer*;      // error, cannot convert void to int
int myThirdInteger = (int*)myVoidPointer*; // should work fine
I havent tested it though, and its been a while since Ive touched C.
 

vpoko

Joined Jan 5, 2012
267
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.
 

WBahn

Joined Mar 31, 2012
30,058
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.
Pointer arithmetic is undefined for a void pointer, so it should throw a syntax error.
 

WBahn

Joined Mar 31, 2012
30,058
and if use void pointer then what is advantages and dis-advantages.
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.
 

WBahn

Joined Mar 31, 2012
30,058
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.
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.
 

fernan82

Joined Apr 19, 2014
26
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.
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.
 

Thread Starter

embpic

Joined May 29, 2013
189
Bit diffdifferent question if I use
Rich (BB code):
Struct
{

Unsigned:1;
};
Then it is related to char or int
 
Top