Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer dereference

Reply
Thread Tools

pointer dereference

 
 
somenath
Guest
Posts: n/a
 
      07-12-2007
On Jul 12, 1:55 pm, somenath <somenath...@gmail.com> wrote:
> Hi All,
> I have one doubt regarding pointer .I have one small code as
> mentioned bellow .
>
> #include<stdio.h>
> #include<stdlib.h>
> int main (void)
> {
> int *p;
> int *ptr= malloc(sizeof(int *));
> return 0;}
>
> My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
> to any where .So when I try to dereference will it show undefined
> behavior ?. I was expatiating it will crash .But in gcc version 3.2.2
> 20030222 (Red Hat Linux 3.2.2-5) not crashing .
>
> Regards,
> Somanath


Hi All,
Sorry for the mistake.By mistake i copied int *ptr= malloc(sizeof(int
*));.My intention was to write " int *ptr=
malloc(sizeof(*p));" .Should it have undefined behavour?

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      07-12-2007
somenath <> wrote:

> On Jul 12, 1:55 pm, somenath <somenath...@gmail.com> wrote:
> > #include<stdio.h>
> > #include<stdlib.h>
> > int main (void)
> > {
> > int *p;
> > int *ptr= malloc(sizeof(int *));
> > return 0;}
> >
> > My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
> > to any where .So when I try to dereference will it show undefined
> > behavior ?.


> Sorry for the mistake.By mistake i copied int *ptr= malloc(sizeof(int
> *));.My intention was to write " int *ptr=
> malloc(sizeof(*p));" .Should it have undefined behavour?


You mean, your code would be

#include<stdio.h>
#include<stdlib.h>
int main (void)
{
int *p;
int *ptr= malloc(sizeof *p);
return 0;
}

No, that does not have undefined behaviour, because in the context of
sizeof, *p is not evaluated. sizeof does not evaluate its operand,
simply because there is no need for it to. The size of *p is the size of
an int, no matter what value (or garbage) p actually contains.
(There is one exception to this rule, which involves variable-length
arrays. But that's a C99 problem only. It doesn't apply to your code.)

BTW, it is probably better to write
int *ptr= malloc(sizeof *ptr);
instead, because then you know that ptr will always point at memory
large enough to contain one of what ptr should point at, even if you
later change the type of p or ptr.

Richard
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-12-2007
somenath wrote:
> On Jul 12, 1:55 pm, somenath <somenath...@gmail.com> wrote:
> > Hi All,
> > I have one doubt regarding pointer .I have one small code as
> > mentioned bellow .
> >
> > #include<stdio.h>
> > #include<stdlib.h>
> > int main (void)
> > {
> > int *p;
> > int *ptr= malloc(sizeof(int *));
> > return 0;}
> >
> > My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
> > to any where .So when I try to dereference will it show undefined
> > behavior ?. I was expatiating it will crash .But in gcc version 3.2.2
> > 20030222 (Red Hat Linux 3.2.2-5) not crashing .

>
> Hi All,
> Sorry for the mistake.By mistake i copied int *ptr= malloc(sizeof(int
> *));.My intention was to write " int *ptr=
> malloc(sizeof(*p));" .Should it have undefined behavour?


No, it shouldn't. ptr is an int pointer. sizeof *p yields the size of
the type pointed to by p. p is an int pointer, so, within the context
of sizeof, *p resolves to an int. So you're assigning the result of a
call to malloc with a request to allocate space for a single int
object. If malloc fails, it'll return a null pointer, which you should
check for. If it succeeds ptr points to an int object.

What did you think could be undefined about the statement?

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      07-12-2007
Richard Bos <> wrote:

> BTW, it is probably better to write
> int *ptr= malloc(sizeof *ptr);
> instead, because then you know that ptr will always point at memory
> large enough to contain one of what ptr should point at, even if you
> later change the type of p or ptr.

^

Nit: There's no longer any 'p' involved with the above code, which is
undoubtedly part of the reason to prefer it.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      07-12-2007
Chris Dollin <> wrote:

> I think it's more reasonable to claim that there's exactly one value of
> type void -- we could call it, oh I dunno, sponk or flolo or `unit` -- which
> is conveniently represented with zero bits. (Not bits that are zero, of
> course.)


It seems like that value could be called "the null value" without too
much confusion (no more, or less, than with the common term "the
null character"). That said, I read n869 6.2.5 p18 - "The void type
comprises an empty set of values..." - as explicitly contradicting the
notion of there being any values of type void.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      07-12-2007
Christopher Benson-Manica wrote:

> Chris Dollin <> wrote:
>
>> I think it's more reasonable to claim that there's exactly one value of
>> type void -- we could call it, oh I dunno, sponk or flolo or `unit` -- which
>> is conveniently represented with zero bits. (Not bits that are zero, of
>> course.)

>
> It seems like that value could be called "the null value" without too
> much confusion (no more, or less, than with the common term "the
> null character"). That said, I read n869 6.2.5 p18 - "The void type
> comprises an empty set of values..." - as explicitly contradicting the
> notion of there being any values of type void.


I think it's more reasonable to claim that there's exactly one value of
type void -- ie, I think that the Standard's statement otherwise is
a less-optimal choice. But this is discussion about the Standard rather
than the language it defines ...

--
RIP Donald Michie 11 November 1923 - 7 July 2007

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      07-12-2007
On 12 Jul, 12:47, somenath <somenath...@gmail.com> wrote:
> On Jul 12, 1:55 pm, somenath <somenath...@gmail.com> wrote:
>
>
>
> > Hi All,
> > I have one doubt regarding pointer .I have one small code as
> > mentioned bellow .

>
> > #include<stdio.h>
> > #include<stdlib.h>
> > int main (void)
> > {
> > int *p;
> > int *ptr= malloc(sizeof(int *));
> > return 0;}

>
> > My doubt is if (sizeof(int *)); is undefined ? Because "p" is point
> > to any where .So when I try to dereference will it show undefined
> > behavior ?. I was expatiating it will crash .But in gcc version 3.2.2
> > 20030222 (Red Hat Linux 3.2.2-5) not crashing .

>
> > Regards,
> > Somanath

>
> Hi All,
> Sorry for the mistake.By mistake i copied int *ptr= malloc(sizeof(int
> *));.My intention was to write " int *ptr=
> malloc(sizeof(*p));" .Should it have undefined behavour?


No. I think your problem here is you don't understand that, with the
exception of variable length arrays (a special case), sizeof() doesn't
involve evaluating its argument. Note that although it looks like a
function, sizeof() is an _operator_.

In fact, sizeof will be dealt with at compile time rather than runtime
(again with the exception of VLAs), as all the relevant information
will be available at that point.

The compiler knows in your case that p is a pointer to int, so also it
knows the general expression "*p" is of type int and hence has the
size appropriate to ints on your platform.

By the time you have a runnable program, your sizeof() will have been
resolved to a constant integer value of type "size_t" (again VLAs are
a special case as their size is set at runtime).

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-12-2007
santosh said:

> Richard Heathfield wrote:
>> santosh said:
>>

<snip>
>
>> > * A pointer of type void should be able to hold the highest
>> > possible
>> > address of the program's address space.

>>
>> A pointer of type void *, actually. [...]

>
> Maybe this is a language issue. Would I be correct when I say "A
> pointer to type void"? I don't think so.


Better: "a pointer of type void *"

> So I suppose instead of
> saying, for example, "a pointer to type int", I should say "a pointer
> of type int *" or, better yet, "an int pointer"...?


Either works. Personally I prefer the "a pointer of type <type>" style,
but "a <type> pointer" is also correct.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-12-2007
santosh <> writes:
[...]
> Maybe this is a language issue. Would I be correct when I say "A
> pointer to type void"? I don't think so. So I suppose instead of
> saying, for example, "a pointer to type int", I should say "a pointer
> of type int *" or, better yet, "an int pointer"...?


In my opinion, "a pointer to type void" or "a pointer to type int" is
ok, but "a pointer of type void*" or "a pointer of type int*" is
better. Pointer objects don't point to types, they point to objects.
Pointer types don't point to anything, but objects and values of
pointer types may point to objects. But we can colloquially say that
type int* points to type int; it's not 100% accurate, but it's not
ambiguous, and I wouldn't complain about the usage unless I was
invited to.

I wouldn't use the phrase "an int pointer" because it could imply
something that's both an int and a pointer, rather than something that
points to an int.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-12-2007
Chris Dollin <> writes:
> Christopher Benson-Manica wrote:
>> Chris Dollin <> wrote:
>>
>>> I think it's more reasonable to claim that there's exactly one
>>> value of type void -- we could call it, oh I dunno, sponk or flolo
>>> or `unit` -- which is conveniently represented with zero
>>> bits. (Not bits that are zero, of course.)

>>
>> It seems like that value could be called "the null value" without too
>> much confusion (no more, or less, than with the common term "the
>> null character"). That said, I read n869 6.2.5 p18 - "The void type
>> comprises an empty set of values..." - as explicitly contradicting the
>> notion of there being any values of type void.

>
> I think it's more reasonable to claim that there's exactly one value of
> type void -- ie, I think that the Standard's statement otherwise is
> a less-optimal choice. But this is discussion about the Standard rather
> than the language it defines ...


I disagree; I think it would be unreasonable to say that there's a
value of type void.

Why can't you write this?

void *p = /* whatever */
void obj = *p;
void func(void);
obj = func();

If we say that type void has no values, the answer is obvious. If we
say it has one value, we need special-case rules to say that we can't
use that value, and that sizeof(void) is not 0 but a constraint
violation.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer dereference security Nyang A. Phra C Programming 13 12-20-2007 02:03 AM
Function pointer dereference security Nyang A. Phra C Programming 0 11-11-2007 04:52 AM
pointer dereference somenath C Programming 12 08-10-2007 05:16 PM
Can't dereference pointer to stack? TuAmigoFiel@gmail.com C++ 9 02-11-2006 06:31 AM
NULL Pointer Dereference Denis Palmeiro C Programming 10 07-16-2003 12:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57