Changing default sizes of data types

A

Anirudh

Is there a way to get different return values for sizeof(int*) etc.
Right now, it is 4 on my machine...I want it to return, say, 3.
Basically, I'd like to play around with alignment issues that might
arise with memory allocation if sizeof(int) != sizeof(int*).

Thanks in advance
Anirudh
 
J

Jens.Toerring

Anirudh said:
Is there a way to get different return values for sizeof(int*) etc.
Right now, it is 4 on my machine...I want it to return, say, 3.

Write your own compiler;-)
Basically, I'd like to play around with alignment issues that might
arise with memory allocation if sizeof(int) != sizeof(int*).

There are easier way to do unaligned access than fiddling with the
compiler, e.g (untested and error checking omitted for obvious
reasons;-)

char *c = malloc( 2 * sizeof( int ) );
int i;

for ( i = 0; i < sizeof( int ); i++ )
{
* ( int * ) ( c + i ) = i;
print( "%d: %d\n", i, * ( int * ) ( c + i ) );
}

If this crashes with a bus error (or whatever it's called on your
system) you have a system that won't allow unaligned access. If it
does not crash te system the alternatives are either that unaligned
accesses are not flagged or that you have sizeof(int) == 1.

Regards, Jens
 
D

Dan Pop

In said:
Is there a way to get different return values for sizeof(int*) etc.
Right now, it is 4 on my machine...I want it to return, say, 3.

Do you realise ALL the implications of changing the size of a data type?
Basically, you have a new implementation, needing its own library and
so on.

Dan
 
M

Malcolm

Anirudh said:
Is there a way to get different return values for sizeof(int*) etc.
Right now, it is 4 on my machine...I want it to return, say, 3.
Basically, I'd like to play around with alignment issues that might
arise with memory allocation if sizeof(int) != sizeof(int*).
As others have siad, the only way to change the size of an int is to write
your own compiler.
However if the code is portable you should be able to compile it with a
different compiler and see if it still works. For example a lot of old DOS
compilers don't have sizeof(int) == sizeof(int *), so you should be able to
pick up code that makes this assumption.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top