Hello ladies and gents,

I'm reading The C++ PL from B.Stroustrup and in chapter 5 the second exercise goes like this:

What on your system, are the restrictions on the pointer types char*, int*, and void*? For example, may an int* have an odd value? Hint: alignement.

I actually have no idea what is asked here, I was thinking that it was related towards what the max values of the different pointer types can hold as value?

Could someone explain what is actually requested?

Thank you.

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.

commented: Great help, nice person. +3

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.

Hi s.o.s,

Thanks for the reply, I know about the multiples of 4, because if you take the code below here, the sizeof(address) should give 22 and gives 24 as result. It's written that if you want to minimize wasted space you should simply order the members(from large to small). I tried this in the below written code but to no avail, sizeof(address) still remains 24.

#include <iostream>

struct address
{
	char *name;
	char *street;
	char *town;
	long int number;
	long zip;
	char state[2];
};

int main()
{
	std::cout << sizeof(char*) << '\n';
	std::cout << sizeof(char*) << '\n';
	std::cout << sizeof(char*) << '\n';
	std::cout << sizeof(long int) << '\n';
	std::cout << sizeof(long) << '\n';
	std::cout << sizeof(char[2]) << '\n';
	std::cout << sizeof(address) << '\n';

	std::cin.get();

	return 0;
}

Thing is, the part for instance about "For example, may an int* have an odd value? Hint: alignement."

What a question is that, a pointer to an int points to an address, aslong as the value in that pointer which is pointed to is of an integer value, it doesn't matter what value it has right?

Really confusing question, "restrictions on pointer types char*, int*, and void* ?" :confused:

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.

Hi AD,

But, so how can I find out what my restrictions are to pointer types char*, int*, and void* ?

The three have a size of 4Bytes, no matter what type it is. But, how to find out the restrictions?

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* )

Hey thanks for those links s.o.s, I'll read them tomorrow, I tried to find something online about it, but never seem to use the correct words to find the good links :cheesy:

Thanks again.

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.

I know about that, but thanks anyway.

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];
};

Hehe, thanks AD, think I got it now ;)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.