I have read this somewhere in the context of Processor architecture. The compiler tries to align the data and the pointers along the boundaries i.e. in multiples of 4.
So the addresses are given along the lines of multiples of 4.
Not very sure about it but atleast this should give you a starting point.
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 54
Microsoft compilers has an alignment option to align data on 1, 2, 4, 8, or 16 byte boundries. I think Dev-C++ and other compilers do that too. This is to help improve memory access time.
Ancient Dragon
Achieved Level 70
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68
Maybe you should read this regarding your size query. It is an excellent example and maybe will suit your needs.
Also regardign your pointer query read this.
Hope it sufficed bye.
The three have a size of 4Bytes, no matter what type it is. But, how to find out the restrictions?
Pointer of type void can point to any type of data if thats what you asking while int type pointers point only to int data and char type pointers point to char type data.
Previous versions of malloc returned char* so we had to typecast them to suit the needs of the program but it has changed in C99 where malloc returns void pointer (void* )
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 54
Pointer of type void can point to any type of data if thats what you asking while int type pointers point only to int data and char type pointers point to char type data.
That is not true. You can make char* point to int* and vice versa. That happens frequently, especially in database and socket programming.
Here is an example:
int x = 123;
char buf[5];
memcpy(buf,&x,sizeof(int));
// now buf contains the binary representation of the integer
//
// convert back to int
int y = *(int*)buf;
>>But, so how can I find out what my restrictions are to pointer types char*, int*, and void* ?
There are none that applies to one type of pointer an not to the other types. typecasting may be necessary to convert one type of pointer to another, as shown in the above example.
Alignment does NOT change the size of a structure, only the address where the object is located. Packing will change the size of the structure by either adding or removing holes. If you want to remove all holes from the structure pack the structure on 1 byte alignment as in the example below. Change the value of the pack and you will get different structure size.
#pragma pack(1)
struct address
{
char *name;
char *street;
char *town;
long int number;
long zip;
char state[2];
};
Ancient Dragon
Achieved Level 70
32,109 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 68
Question Answered as of 6 Years Ago by
Ancient Dragon
and
~s.o.s~ That is not true. You can make char* point to int* and vice versa.
I said you can't make a int* point to char data and not char pointer.
Of couse when dealing with commands like memcpy which deal in raw bits, you can use any type of pointer, it doesnt bother -- all it does is copy the bit representation from the source location to the destinatino location.
What I meant was you cant do something like:
int* ptr_int = 0 ;
char* ptr_char = 0 ;
char ch = '\0' ;
int i = 0 ;
ptr_int = & i ; // ok
ptr_char = &ch; // ok
ptr_int = &ch ; //error
ptr_char = &i ; // error
Of couse you can make it go away with typecasting but thats a different story altogether.
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 54