1.11M Members

Restrictions on pointer tyeps?

 
0
 

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.

 
1
 

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.

 
0
 

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.

 
0
 

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:

 
0
 

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?

 
0
 

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

 
0
 

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.

 
0
 

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

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

Question Answered as of 7 Years Ago by Ancient Dragon and ~s.o.s~
 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article