size of pointer in C?

S

siliconwafer

What is size of pointer in C on DOS?
is it sizeof(int ) or size of (long int)?
If this ans is present in FAQ pls direct me to tht ouestion
 
G

Giannis Papadopoulos

siliconwafer said:
What is size of pointer in C on DOS?
is it sizeof(int ) or size of (long int)?
If this ans is present in FAQ pls direct me to tht ouestion

It is sizeof(void*)...
It may be sizeof(void*)==sizeof(long), but it may not be as well.
Whatever depends on the conditional above is considered not portable.


--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.users.uth.gr
 
P

pete

Giannis said:
It is sizeof(void*)

Pointers to void are the same size as pointers to char.

Pointers to structures are the same size as each other.

The sizes of most other types of pointers
have no special specifications.
 
C

Chris Hills

What is size of pointer in C on DOS?
is it sizeof(int ) or size of (long int)?
If this ans is present in FAQ pls direct me to tht ouestion

plz wrt in engsh
 
S

siliconwafer

siliconwafer said:
What is size of pointer in C on DOS?
is it sizeof(int ) or size of (long int)?
If this ans is present in FAQ pls direct me to tht ouestion

Lets take an example:
char*str = (char*)malloc(1024);
printf("%d",sizeof(str));
what should get printed?
 
C

Chris Dollin

siliconwafer said:
Lets take an example:
char*str = (char*)malloc(1024);
printf("%d",sizeof(str));
what should get printed?

(a) Anything. You have undefined behaviour there. (Clue: what type
does %d expect?)

(b) The sizeof a pointer-to-char in your implementation. Possibilities
include 2, 4, 8, 3, 1, and 17.
 
S

siliconwafer

Hi Chris,
does it make any difference that size of pointer to char is different
than size of pointer to say long int?
A pointer stores address and address is integer or long int.So size of
pointer *must* be either 2 or 4
-Siliconwafer
 
C

Chris Dollin

siliconwafer said:
Hi Chris,
does it make any difference that size of pointer to char is different
than size of pointer to say long int?

Is it? It need not be.
A pointer stores address and address is integer or long int.

That depends on the implementation. I imagine a DOS compiler is
free to choose whatever it finds convenient.
So size of pointer *must* be either 2 or 4

If you say so. Such an answer isn't required by the Standard, though.

It is wise to arrange that your code doesn't care.
 
L

Lawrence Kirby

What is size of pointer in C on DOS?

Whatever the compiler chooses to make it. For most DOS compilers that
depends on the memory model you use to compile. However memory models are
a DOSism or at least processor related, and not part of standard C. A good
place to discuss this is comp.os.msdos.programmer.
is it sizeof(int ) or
size of (long int)? If this ans is present in FAQ
pls direct me to tht ouestion

So it could be either depending on what compiler and compiler options you
use. As far as writing C code is concerned just write you program so that
it wirks either way, that is without making assumptions about pointer size
and representation.

Lawrence
 
F

Flash Gordon

siliconwafer said:
Lets take an example:
char*str = (char*)malloc(1024);

Don't cast the return value of malloc, it's not required.
printf("%d",sizeof(str));
what should get printed?

Anything or nothing since the result of sizeof is size_t which is *not*
int. If you correct all the bugs and produce a conforming C program then
the answer, al others have already stated, is, "whatever the implementer
decided the size of a char* pointer should be." On DOS that is *likely*
to be either 2 or 4 and will probably depend on the options you provide
the compiler.

So, once again, don't write SW that depends on knowing the size of a
pointer.

I believe a corrected program is:

#include <stdio.h> /* Required for printf */
/* If you use malloc, which I don't, you need to include stdlib.h */

int main(void)
{
char *ptr;
/* No need to assign anything since we just want the size of the
pointer */
printf("%lu\n",(unsigned long)sizeof ptr);
/* You get 0 on the Deathstation 9000 in super large pointer
mode where the size of a pointer is one larger than can be
represented in an unsigned long and size_t has a larger range
than unsigned long. */
return 0;
}
 
D

Dik T. Winter

> does it make any difference that size of pointer to char is different
> than size of pointer to say long int?
> A pointer stores address and address is integer or long int.So size of
> pointer *must* be either 2 or 4

Wrong. An address is not necessarily an integer. It can contain things
like ring number, segment number, address within segment, byte number
within a word and a host of other things.
 
C

Clark S. Cox III

Hi Chris,
does it make any difference that size of pointer to char is different
than size of pointer to say long int?

There is nothing wrong with a platform where sizeof(char*) != sizeof(int*).
A pointer stores address and address is integer or long int.

Who says that a pointer must be an integer? The standard certainly doesn't
So size of
pointer *must* be either 2 or 4

That's odd, considering that I'm working on a platform at this very
moment where
(sizeof(void*) == 8).
 
K

Kenneth Brody

Clark S. Cox III said:
On 2005-09-29 08:38:44 -0400, "siliconwafer" <[email protected]> said: [...]
So size of
pointer *must* be either 2 or 4

That's odd, considering that I'm working on a platform at this very
moment where
(sizeof(void*) == 8).

Although I never worked in C on this platform, the computer I used at
college had 36-bit words and 18-bit addresses. Although there were
opcodes for accessing sub-words, the smallest unit of storage was 36
bits.

I have no idea what "sizeof(void *)" would be on such a platform, but
it is quite possible that it would be "1".

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
M

Martin Ambuhl

siliconwafer said:
Lets take an example:
char*str = (char*)malloc(1024);
printf("%d",sizeof(str));
what should get printed?

Someone who can write
printf("%d",sizeof(str));
has no business worrying about the size of a pointer.
The value from sizeof(str) is an unsigned integer that may be wider than
an int (and often is). "%d" expects a signed value, and so is wrong, and
an integral object no larger than an int, and is wrong a second time.

Also, writing
char*str = (char*)malloc(1024);
strongly suggests a failure to include <stdlib.h>, since there is no
excuse for the (char *) cast.

It would probably be better to concentrate on learning the basics of
writing C programs instead of trying to look under the hood at details
you are not ready to explore and need not be the same on the next model
implementation you use.
 
M

Martin Ambuhl

siliconwafer said:
Hi Chris,
does it make any difference that size of pointer to char is different
than size of pointer to say long int?
A pointer stores address and address is integer or long int.

Wrong. You premise is false.
So size of
pointer *must* be either 2 or 4

Wrong. Not only is your premise wrong, buteven if it were true there is
no logical connection to your conclusion. A non-sequitor based on false
premises is hardly worth considering.
 
S

Serge Paccalin

Le jeudi 29 septembre 2005 à 14:38:44, siliconwafer a écrit dans
comp.lang.c :
A pointer stores address and address is integer or long int.

Bullshit. Remember MS-DOS where pointers were often two ints: a 16-bit
segment and a 16-bit offset.
So size of pointer *must* be either 2 or 4

More bullshit. In Win64 programming:

sizeof (int) == sizeof (long) == 4 [32 bits]
sizeof (void *) == 8 [64 bits]

--
___________ 29/09/2005 18:28:31
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
K

Keith Thompson

siliconwafer said:
Lets take an example:
char*str = (char*)malloc(1024);
printf("%d",sizeof(str));
what should get printed?

Let's fix your example:

char *str = malloc(1024);
printf("%d\n", (int)sizeof str);

The sizeof operator does not evaluate its operand; all it does is
yield its size in bytes. (There is an exception for VLAs
(variable-length arrays), but that's not relevant here.) So the fact
that you initialized str has no effect on the behavior of the code.
(You should normally check whether the malloc() succeeded, but that
doesn't matter here since you're not using the result.) So:

char *str;
printf("%d\n", (int)sizeof str);

The expression "sizeof str" yields the size in bytes of str, which is
the same as sizeof(char*). The standard says very little about what
this value will be. It's guaranteed to be at least 1; on some
systems, it can be 1. Certain other requirements imply that it needs
to be at least 16 bits in a hosted environment, so sizeof(char*)==1
implies CHAR_BIT>=16. If you don't understand these details yet,
don't worry about it.

There is very little guaranteed relationship between integers and
pointers. They are two entirely different things. A C pointer is
*not* some kind of integer in disguise; it's just a pointer. Its
value refers to some memory location, but the manner in which it does
so is implementation-specific. Different kinds of pointers can have
different sizes (though they tend to be the same on most systems).
You can convert integers to pointers and vice versa, but the results
of doing so are system-specific.

A pointer is not necessarily the same size as any particular integer
type; a system where pointers are 128 bits and the largest integer
type is 64 bits would be perfectly legal.

Pointers point to things. Forget everything else you think you know
about them, and start from there. Re-read the section on pointers in
your C textbook; if you don't have one, K&R2 is excellent. Read
section 4 of the C FAQ.

On a typical modern system, CHAR_BIT (the number of bits in a byte) is
likely to be 8, int is likely to be 32 bits, long is likely to be
either 32 or 64 bits, and pointers are likely to be 32 or 64 bits.
Converting a pointer to an unsigned integer of the same size is likely
to yield something that looks meaningful if you know anything about
the underlying memory addressing of the system. But the C standard is
designed to allow for exceptions to all of these things. You can
write useful code that doesn't depend on any of these assumptions, and
it will work everywhere, not just on "typical" systems.
 
E

Emmanuel Delahaye

siliconwafer wrote on 29/09/05 :
Lets take an example:
char*str = (char*)malloc(1024);

Useless for the example.

char *str;
printf("%d",sizeof(str));

printf ("%u\n", (unsigned) sizeof str);

BTW, you just need the type...

printf ("%u\n", (unsigned) sizeof (char *));
what should get printed?

Who knows, it's implementation-dependent.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
J

John Bode

siliconwafer said:
What is size of pointer in C on DOS?
is it sizeof(int ) or size of (long int)?
If this ans is present in FAQ pls direct me to tht ouestion

The quickest way to find out what pointer sizes are on your particular
implementation:

printf("sizeof(char*): %lu\n", (unsigned long) sizeof(char*));
printf("sizeof(int*): %lu\n", (unsigned long) sizeof(int*));
printf("sizeof(short*): %lu\n", (unsigned long) sizeof(short*));

etc.

Note that pointers to different types may be different sizes, and may
be different between implementations.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top