Find the size of a datatype

P

pete

Ico said:
[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.

I'm not sure about this either, but 0 is not NULL, so this might be
valid.

((char *)0) is a null pointer.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.
 
V

vire

Flash said:
vire wrote:

Please do not top post. Your reply belongs under the text you are
replying to.


No, it does *not* point to address 0. (X *)0 is a null pointer (NULL is
a macro not a value). Pointer arithmetic is only defined for pointers to
objects or 1 past the end of an object (both initial value and result
having to fit). The null pointer explicitly does *not* point at an object.
in <stdio.h> i found:
#define NULL 0
#else
#define NULL ((void *)0)

yes ,it is true that NULL is a macro . but what is a macro ?
and how can you explain these codes:

int *p=NULL;
printf("%p",p);
None of those are defined by the C language.
i am so sorry that i have not study the c standard.but those codes do
make some effect.
then how do they come about?
 
V

Vladimir Oka

vire said:
in <stdio.h> i found:
#define NULL 0
#else
#define NULL ((void *)0)

yes ,it is true that NULL is a macro . but what is a macro ?

Macro is a text substitution construct.
NULL macro expands to a null pointer constant.
and how can you explain these codes:

int *p=NULL;

Variable `p` is assigned a null pointer value.
printf("%p",p);

This is then output to `stdout` in the implementation defined way.
i am so sorry that i have not study the c standard.but those codes do
make some effect.

And their effect is to invoke Undefined Behaviour (them not being
defined by the C Standard). That means that they're allowed to do
*anything*, including what you expect them to do. However, tomorrow
they may do something different.
then how do they come about?

They come about by poor programming skills.
 
C

Chris Dollin

vire said:
in <stdio.h> i found:
#define NULL 0
#else
#define NULL ((void *)0)

yes ,it is true that NULL is a macro . but what is a macro ?

A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?
and how can you explain these codes:

int *p=NULL;
printf("%p",p);

`p` is assigned the null pointer. Then its implementation-specific representation
is printed out. I don't see a problem here.
i am so sorry that i have not study the c standard.but those codes do
make some effect.

And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.
then how do they come about?

Someone typed them.
 
I

Ico

pete said:
Ico said:
pete said:
Ico wrote:


Suppose I am given a datatype say X in C.
How can i find its size
without declaring a variable or pointer variable of that type,
and of course without using sizeof operator.

here, I'm glad to help you doing your assignments. Try this :

[ ..snip questionable code.. ]
I don't think that the result of adding one to a null pointer
constant, is defined.

I'm not sure about this either, but 0 is not NULL, so this might be
valid.

((char *)0) is a null pointer.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.

True, I was wrong, you are right.
 
P

pete

Ico said:
pete said:
Ico said:
Ico wrote:


Suppose I am given a datatype say X in C.
How can i find its size
without declaring a variable or pointer variable of that type,
and of course without using sizeof operator.

here, I'm glad to help you doing your assignments. Try this :


[ ..snip questionable code.. ]

I don't think that the result of adding one to a null pointer
constant, is defined.

I'm not sure about this either, but 0 is not NULL, so this might be
valid.

((char *)0) is a null pointer.

N869
6.3.2.3 Pointers
[#3] An integer constant expression with the value 0, or
such an expression cast to type void *, is called a null
pointer constant. If a null pointer constant is
converted to a pointer type, the resulting pointer, called a
null pointer, is guaranteed to compare unequal to a pointer
to any object or function.

True, I was wrong, you are right.

His instructor probably doesn't even know.
 
V

vire

Chris said:
A name that when used is replaced by (the macro-expansion of) its
replacement text. Surely you know that?
so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

i think the null pointer is indeed point to some place(the address
0).but we just call it null.
and we can not read or write on address 0.is that right?
`p` is assigned the null pointer. Then its implementation-specific representation
is printed out. I don't see a problem here.
what i want to say is , the value of p is 0
so i think a pointer point to address 0 is so called null pointer ,is
that right?
And that effect is not defined by the C language. If it's defined at all,
it's defined by whatever compiler chose to compile it.

int a;
int* p=&a;
p+1;
is p+1 defined by c language ? plz tell me.i really don't have any idea
about it.
 
C

Chris Dollin

vire said:
so you get a constant 0 if you use NULL. right?

That depends on the expansion of NULL. So long as it expands to a
null pointer constant, that's OK. 0 is a null pointer constant,
but there are null pointer constants that are not (literally) 0.
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

No, `int *p=(int*)0x13ff7c;` is either undefined or implementation-defined
(I forget which, but it doesn't matter here). How an implementation
chooses to convert an integer to a pointer isn't specified by C. It's
expected to be useful (or at least not deliberately useless) on your
platform, but that's not part of the definition, that's part of not being
a stupid implementation - not all uses of C need be portable.
i think the null pointer is indeed point to some place(the address
0).but we just call it null.

You can think that if you like, but that doesn't make it true. All that's
required is that assigning a null pointer constant to a pointer gives that
pointer a value that doesn't compare equal to the address of any (C) object.
In particular, there is /no requirement/ that the null pointer /value/ be
some all-bits-zero value. A sufficiently determined implementor could
represent it with the bit-pattern 0x11, or 0x2a, or 0xdeadbeef. And
whatever bit-pattern gets chosen, it can be arranged that there is nothing
at that address, with the co-operation of the operating system.

Using all-bits-zero is frequently possible and convenient and used; but it's
not /required/.
and we can not read or write on address 0.is that right?

C doesn't say. It doesn't say /anything/ about "address 0". If an implementation
has an "address 0", it may (or may not) allow you to read it, and it may (or
may not) allow you to write to it.
what i want to say is , the value of p is 0
so i think a pointer point to address 0 is so called null pointer ,is
that right?

The value of `p` is the null pointer.
int a;
int* p=&a;
p+1;
is p+1 defined by c language ?

Yes. It is legal to point one past the end of an object. (Otherwise a whole
slew of looping idioms would be illegal.)
plz tell me.i really don't have any idea about it.

Books. And the standard.
 
R

Richard Bos

vire said:
so you get a constant 0 if you use NULL. right?

Wrong. You get _a_ null pointer constant. This may be any integer
constant with the value 0, or any such constant cast to void *. 0 is one
of them, but not the only one.
then in
int *p=NULL;
p is a null pointer

Yes, because the Standard explicitly demands that a null pointer
constant, when evaluated in a pointer context, is translated to a null
pointer. No other constants get this special treatment; neither do
integer _objects_ which happen to have the value 0.
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

Wrong. There is nothing above which should lead you to this conclusion;
and in fact it is not one you can rely on. It _may_ happen that way on
some systems; and on another system, it may make p point to address
0x0013ff70; on yet another, it may make p point to address S:13-O:ff7c;
and on a third, it may cause your program to crash, burn, and scream
"Bus error! Bus error! Oh, the ignominy!".
i think the null pointer is indeed point to some place

What you think is completely immaterial if it flies in the face of what
the ISO C Standard says.
(the address 0).but we just call it null.
and we can not read or write on address 0.is that right?

No. That is wrong. Or rather, that is an unwarranted extrapolation from
limited desk-top experience.
so i think a pointer point to address 0 is so called null pointer ,is
that right?
Ditto.


int a;
int* p=&a;
p+1;
is p+1 defined by c language ?

Yes. It points one past _the object_ a. Note that in this case you did
start with an actual object, and the address one past any real object
must exist, and a pointer to it must be valid. You still cannot read
from or write through it, but you _can_ calculate it. (The reason for
this, by the way, is that it's useful in loops.)

Don't use such imbecilic school-boy abbreviations if you want to be
taken for an adult.

Richard
 
F

Flash Gordon

vire said:
so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

It could be anything. If you are working on an implementation with 16
bit pointers (which is legal) it is unlikely to be the address you
suggest! The mapping is implementation defined, and the implementation
does not have to define a mapping for every value.
i think the null pointer is indeed point to some place(the address
0).but we just call it null.

Or you might get ((void*)0)
Or (5-5)
Or anything else that satisfies the standard.

There is absolutely no guarantee though that this corresponds to address
0. The null pointer could have an internal representation of all bits
set, or anything else that the autor(s) of the implementation decide on.
and we can not read or write on address 0.is that right?

That may or may not be true. Regardless, the standard does not define
what happens if you dereference a null pointer.
what i want to say is , the value of p is 0

No, the value of p is a null pointer.
so i think a pointer point to address 0 is so called null pointer ,is
that right?

No. A null pointer is called a null pointer. Address 0 might be a valid
address. It is just that C allows you to use an integer constant 0 in
the *source* as a null pointer constant.
int a;
int* p=&a;
p+1;
is p+1 defined by c language ? plz tell me.i really don't have any idea
about it.

You are allowed to point one passed the end of an object/array, so p+1
is defined in the above. However, you are not allowed to dereference it. So
*(p+1);
would be "illegal". I.e., it invokes undefined behaviour so the program
is allowed to do *anything*. This is despite the value not being used.

You should read the comp.lang.c FAQ (Google will point you at it) in
particular section 6.
 
K

Keith Thompson

vire said:
so you get a constant 0 if you use NULL. right?
then in
int *p=NULL;
p is a null pointer
and
int *p=(int*)0x13ff7c;
p is point to address 0x0013ff7c;

i think the null pointer is indeed point to some place(the address
0).but we just call it null.
and we can not read or write on address 0.is that right?

No.

Section 5 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has a good
explanation of this. After you've absorbed that, read section 4, then
the rest of the FAQ.
 
K

Keith Thompson

pete said:
((char *)0) is a null pointer.

More precisely, it's a null pointer constant; it evaluates to a null
pointer value at run time.

(If I wanted to be really pedantic, I'd mention that the standard
doesn't actually say that a parenthesized null pointer constant is a
null pointer constant; the intent is sufficiently clear, so I won't
mention it.)
 
C

CBFalconer

Keith said:
More precisely, it's a null pointer constant; it evaluates to a
null pointer value at run time.

(If I wanted to be really pedantic, I'd mention that the standard
doesn't actually say that a parenthesized null pointer constant
is a null pointer constant; the intent is sufficiently clear, so
I won't mention it.)

I am grateful that you didn't waste anybodies time mentioning it.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
D

Dik T. Winter

> vire wrote: .... ....
> Using all-bits-zero is frequently possible and convenient and used; but it's
> not /required/.

The Data General MV 4000 was a machine where a null pointer did *not* have all
bits zero.
>
> C doesn't say. It doesn't say /anything/ about "address 0". If an
> implementation has an "address 0", it may (or may not) allow you to
> read it, and it may (or may not) allow you to write to it.

I do not know how C was implemented on the Transputer (if it was implemented)
but on that machine addresses were done as signed integers and address 0
was in the middle of the space that could be used. So I think that on that
machine a null pointer would also not have all bits 0.
 
V

vire

Keith said:
No.

Section 5 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has a good
explanation of this. After you've absorbed that, read section 4, then
the rest of the FAQ.
many thaks to
Chris Dollin, Richard Bos, Flash Gordon, and Keith Thompson.
i think i should have read through the faq and the standard before i
post a new message.
now i am reading them.thank you all guys for your helpful advices.
 
R

rmeenaks

Dik said:
I do not know how C was implemented on the Transputer (if it was implemented)
but on that machine addresses were done as signed integers and address 0
was in the middle of the space that could be used. So I think that on that
machine a null pointer would also not have all bits 0.
--

The underlying implementation of null can be anything as the address
does not necessary have to be 0. The compiler, however, must treat it
as a constant. In the transputer C compiler, the physical address of
null is MINT (minimum integer), but the C compiler allows you to code:

if (p!= NULL)

which is (char *) 0 in the compiler, but gets translated to if (p !=
MINT) in assembly. The spec doesnt say what the PHYSICAL ADDRESS of
NULL has to be, just how the compiler treats it...


Ram
 

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,787
Messages
2,569,630
Members
45,338
Latest member
41Pearline46

Latest Threads

Top