PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > void * in ANSC
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
void * in ANSC

Réponse
 
LinkBack Outils de la discussion
Vieux 03/02/2008, 03h13   #1
Mark Jiao
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut void * in ANSC



void printhex(const void *data,size_t len)
{
unsigned char * tmp = data;
while(tmp < &data[len]) //just warning , ??
printf("%.2X ",*tmp++);
printf("\n");
}
  Réponse avec citation
Vieux 03/02/2008, 03h22   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Feb 3, 5:13 am, "Mark Jiao" <jzg1...@126.com> wrote:
> void printhex(const void *data,size_t len)
> {
> unsigned char * tmp = data;
> while(tmp < &data[len]) //just warning , ??

You are performing arithmetic operations with a void * pointer which
is not allowed.
&data[len] means &*(data + len)
Try this
--
while(tmp < (char *)data + len)
  Réponse avec citation
Vieux 03/02/2008, 03h24   #3
frog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On 2ÔÂ3ÈÕ, ÉÏÎç11ʱ13·Ö, "Mark Jiao" <jzg1...@126.com> wrote:
> void printhex(const void *data,size_t len)
> {
> unsigned char * tmp = data;
> while(tmp < &data[len]) //just warning , ??
> printf("%.2X ",*tmp++);
> printf("\n");
>
>
>
> }- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
>
> - ÏÔʾÒýÓõÄÎÄ×Ö -


??
  Réponse avec citation
Vieux 03/02/2008, 03h30   #4
Mark Jiao
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

ÔÚ Sun, 03 Feb 2008 11:22:09 +0800£¬<vippstar@gmail.com> дµÀ:

> On Feb 3, 5:13 am, "Mark Jiao" <jzg1...@126.com> wrote:
>> void printhex(const void *data,size_t len)
>> {
>> unsigned char * tmp = data;
>> while(tmp < &data[len]) //just warning , ??

> You are performing arithmetic operations with a void * pointer which
> is not allowed.
> &data[len] means &*(data + len)

Sorry!
I am very poor English!
How many the memery address would be add?? 1? or No Standards ??

Thanks!
> Try this
> --
> while(tmp < (char *)data + len)




--
  Réponse avec citation
Vieux 03/02/2008, 07h32   #5
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

"Mark Jiao" <jzg1234@126.com> writes:
> void printhex(const void *data,size_t len)
> {
> unsigned char * tmp = data;
> while(tmp < &data[len]) //just warning , ??
> printf("%.2X ",*tmp++);
> printf("\n");
> }


I think what you're asking is why the compiler prints just a warning
rather than an error message.

Attempting to dereference a void*, as you've done here, is a
constraint violation. The compiler is required to issue some sort of
diagnostic message, but it doesn't have to be a fatal error (the
standard doesn't distinguish between warnings and other diagnostics).

As it happens, at least one major compiler allows, as an extension,
certain operations on void* that are not supported by the language.
If you're using gcc, that explains it.

Most warnings should be treated as errors anyway.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 03/02/2008, 11h27   #6
pete
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Mark Jiao wrote:
>
> ÔÚ Sun, 03 Feb 2008 11:22:09 +0800£¬<vippstar@gmail.com> дµÀ:
>
> > On Feb 3, 5:13 am, "Mark Jiao" <jzg1...@126.com> wrote:
> >> void printhex(const void *data,size_t len)
> >> {
> >> unsigned char * tmp = data;
> >> while(tmp < &data[len]) //just warning , ??

> > You are performing arithmetic operations with a void * pointer which
> > is not allowed.
> > &data[len] means &*(data + len)

> Sorry!
> I am very poor English!
> How many the memery address would be add?? 1? or No Standards ??


No Standards for ((void *)data + 1).

> > Try this
> > --
> > while(tmp < (char *)data + len)


1 memery address would be added for ((char *)data + 1).

--
pete
  Réponse avec citation
Vieux 03/02/2008, 11h31   #7
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Feb 3, 9:32 am, Keith Thompson <ks...@mib.org> wrote:
> "Mark Jiao" <jzg1...@126.com> writes:
> > void printhex(const void *data,size_t len)
> > {
> > unsigned char * tmp = data;
> > while(tmp < &data[len]) //just warning , ??
> > printf("%.2X ",*tmp++);
> > printf("\n");
> > }

>
> I think what you're asking is why the compiler prints just a warning
> rather than an error message.
>
> Attempting to dereference a void*, as you've done here, is a
> constraint violation. The compiler is required to issue some sort of
> diagnostic message, but it doesn't have to be a fatal error (the
> standard doesn't distinguish between warnings and other diagnostics).

I believe, since x[y] expands to (or, is equal to) *(x + y) what the
compiler should diagnose is about arithmetic on a 'void *' pointer.
  Réponse avec citation
Vieux 03/02/2008, 11h47   #8
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Sun, 03 Feb 2008 03:31:07 -0800, vippstar wrote:
> On Feb 3, 9:32 am, Keith Thompson <ks...@mib.org> wrote:
>> "Mark Jiao" <jzg1...@126.com> writes:
>> > void printhex(const void *data,size_t len) {
>> > unsigned char * tmp = data;
>> > while(tmp < &data[len]) //just warning , ??
>> > printf("%.2X ",*tmp++);
>> > printf("\n");
>> > }

>>
>> I think what you're asking is why the compiler prints just a warning
>> rather than an error message.
>>
>> Attempting to dereference a void*, as you've done here, is a constraint
>> violation. The compiler is required to issue some sort of diagnostic
>> message, but it doesn't have to be a fatal error (the standard doesn't
>> distinguish between warnings and other diagnostics).

> I believe, since x[y] expands to (or, is equal to) *(x + y) what the
> compiler should diagnose is about arithmetic on a 'void *' pointer.


You're correct. Dereferencing a void * is allowed, though not
particularly useful, since you can't do anything with the result but
discard it or take its address again. Performing arithmetic on a void *
is not allowed, because void is an incomplete type. It's the same as how
performing arithmetic on int (*)[] is not allowed, because int [] is an
incomplete type.
  Réponse avec citation
Vieux 03/02/2008, 23h18   #9
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Sun, 03 Feb 2008 12:47:45 +0100, Harald van D?k <truedfx@gmail.com>
wrote in comp.lang.c:

> On Sun, 03 Feb 2008 03:31:07 -0800, vippstar wrote:
> > On Feb 3, 9:32 am, Keith Thompson <ks...@mib.org> wrote:
> >> "Mark Jiao" <jzg1...@126.com> writes:
> >> > void printhex(const void *data,size_t len) {
> >> > unsigned char * tmp = data;
> >> > while(tmp < &data[len]) //just warning , ??
> >> > printf("%.2X ",*tmp++);
> >> > printf("\n");
> >> > }
> >>
> >> I think what you're asking is why the compiler prints just a warning
> >> rather than an error message.
> >>
> >> Attempting to dereference a void*, as you've done here, is a constraint
> >> violation. The compiler is required to issue some sort of diagnostic
> >> message, but it doesn't have to be a fatal error (the standard doesn't
> >> distinguish between warnings and other diagnostics).

> > I believe, since x[y] expands to (or, is equal to) *(x + y) what the
> > compiler should diagnose is about arithmetic on a 'void *' pointer.

>
> You're correct. Dereferencing a void * is allowed, though not


Allowed by whom? Certainly not by the C language standard. Can you
cite C&V to the contrary?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Vieux 03/02/2008, 23h50   #10
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Jack Klein <jackklein@spamcop.net> writes:
> On Sun, 03 Feb 2008 12:47:45 +0100, Harald van D?k <truedfx@gmail.com>
> wrote in comp.lang.c:

[...]
>> You're correct. Dereferencing a void * is allowed, though not

>
> Allowed by whom? Certainly not by the C language standard. Can you
> cite C&V to the contrary?


Can you cite C&V that says it's *not* allowed? I checked earlier, and
I was surprised to discover that it does appear to be allowed.

In C99 6.5.3.2, the only constraint for the unary "*" operator is:

The operand of the unary * operator shall have pointer type.

and void* is a pointer type. Obviously there's very little you can do
with the result, but this:

void *ptr = /* some non-null value */
*ptr;

appears to be legal (though not particularly useful).

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 04/02/2008, 00h20   #11
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Keith Thompson wrote:

> In C99 6.5.3.2, the only constraint for the unary "*" operator is:
>
> The operand of the unary * operator shall have pointer type.
>
> and void* is a pointer type. Obviously there's very little you can do
> with the result, but this:
>
> void *ptr = /* some non-null value */
> *ptr;
>
> appears to be legal (though not particularly useful).


This has a curious consequence:
volatile void *ptr = SOMEWHERE;
*ptr;

How many bytes does that access?



--
Army1987 (Replace "NOSPAM" with "email")
  Réponse avec citation
Vieux 04/02/2008, 05h05   #12
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Army1987 <army1...@NOSPAM.it> wrote:
> Keith Thompson wrote:
> > In C99 6.5.3.2, the only constraint for the unary "*"
> > operator is:
> >
> > The operand of the unary * operator shall have pointer
> > type. and void* is a pointer type. Obviously there's very
> > little you can do with the result, but this:
> >
> > void *ptr = /* some non-null value */
> > *ptr;
> >
> > appears to be legal (though not particularly useful).

>
> This has a curious consequence:
> volatile void *ptr = SOMEWHERE;
> *ptr;
>
> How many bytes does that access?


None. [How many bytes does any other void expression access?]

--
Peter
  Réponse avec citation
Vieux 04/02/2008, 11h38   #13
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Peter Nilsson wrote:

> Army1987 <army1...@NOSPAM.it> wrote:
>> Â Â Âvolatile void *ptr = SOMEWHERE;
>> Â Â Â*ptr;
>>
>> How many bytes does that access?

>
> None. [How many bytes does any other void expression access?]

(void)memcpy(foo, bar, 42) accesses 42 bytes.



--
Army1987 (Replace "NOSPAM" with "email")
  Réponse avec citation
Vieux 04/02/2008, 11h39   #14
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Army1987 wrote:

> (void)memcpy(foo, bar, 42) accesses 42 bytes.

84 bytes, I meant.

--
Army1987 (Replace "NOSPAM" with "email")
  Réponse avec citation
Vieux 04/02/2008, 22h08   #15
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Army1987 <army1...@NOSPAM.it> wrote:
> Peter Nilsson wrote:
> > Army1987 <army1...@NOSPAM.it> wrote:
> > > volatile void *ptr = SOMEWHERE;
> > > *ptr;
> > >
> > > How many bytes does that access?

> >
> > None. [How many bytes does any other void expression
> > access?]

>
> (void)memcpy(foo, bar, 42) accesses [84] bytes.


How many bytes does the following access?

memcpy;

--
Peter
  Réponse avec citation
Vieux 04/02/2008, 22h21   #16
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

In article <fo6th2$mls$2@tdi.cu.mi.it>, Army1987 <army1987@NOSPAM.it> wrote:
>Army1987 wrote:


>> (void)memcpy(foo, bar, 42) accesses 42 bytes.


>84 bytes, I meant.


How do you arrive at that number? memcpy() does not define
the behaviour if the fields overlap. memmove() is the function
that defines the copying "as if" the data were copied into a
temporary buffer, and if such temporary copies were to take
place then Yes that would increase the byte count, but as you
have not defined the extent to which foo overlaps with bar,
you cannot say with any degree of certainty -exactly- how many
bytes would be accessed or how many times those bytes would be
accessed.
--
"Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson
  Réponse avec citation
Vieux 04/02/2008, 22h23   #17
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Mon, 04 Feb 2008 14:08:56 -0800, Peter Nilsson wrote:
> How many bytes does the following access?
>
> memcpy;


None. You've got a function designator which is not the operand of sizeof
or &, so it automatically gets converted to a pointer to memcpy. Besides,
functions aren't objects, and don't have to be made up of bytes at all,
and in some cases really might not be -- consider a C program running on
a virtual machine, where memcpy is implemented as a system call to the
host.
  Réponse avec citation
Vieux 04/02/2008, 22h25   #18
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Mon, 04 Feb 2008 22:21:04 +0000, Walter Roberson wrote:
> In article <fo6th2$mls$2@tdi.cu.mi.it>, Army1987 <army1987@NOSPAM.it>
> wrote:
>>Army1987 wrote:

>
>>> (void)memcpy(foo, bar, 42) accesses 42 bytes.

>
>>84 bytes, I meant.

>
> How do you arrive at that number? memcpy() does not define the behaviour
> if the fields overlap.


So assuming the fields don't overlap, memcpy reads 42 bytes, and writes
42 different bytes, so accesses 84 bytes in total.
  Réponse avec citation
Vieux 04/02/2008, 22h37   #19
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

> Army1987 <army1...@NOSPAM.it> wrote:
> > Peter Nilsson wrote:
> > > Army1987 <army1...@NOSPAM.it> wrote:
> > > > volatile void *ptr = SOMEWHERE;
> > > > *ptr;
> > > >
> > > > How many bytes does that access?
> > >
> > > None. [How many bytes does any other void expression
> > > access?]

> >
> > (void)memcpy(foo, bar, 42) accesses [84] bytes.

<snip>

[In light of Harald van Dijk's point on my followup...]
How many bytes does the following access?

memcpy(foo, bar, 0);

If it's none, then why should *memcpy(foo, bar, 0) be
any different? If it isn't different, then why should
dereferencing a volatile void pointer be any different?

--
Peter
  Réponse avec citation
Vieux 04/02/2008, 22h38   #20
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

In article <1fad2$47a790c3$541dfcd3$20968@cache6.tilbu1.nb.ho me.nl>,
=?UTF-8?q?Harald_van_D=C4=B3k?= <truedfx@gmail.com> wrote:
>On Mon, 04 Feb 2008 22:21:04 +0000, Walter Roberson wrote:
>> In article <fo6th2$mls$2@tdi.cu.mi.it>, Army1987 <army1987@NOSPAM.it>
>> wrote:
>>>Army1987 wrote:


>>>> (void)memcpy(foo, bar, 42) accesses 42 bytes.


>>>84 bytes, I meant.


>> How do you arrive at that number? memcpy() does not define the behaviour
>> if the fields overlap.


>So assuming the fields don't overlap, memcpy reads 42 bytes, and writes
>42 different bytes, so accesses 84 bytes in total.


I must have been having a Duh moment.

Okay, so I'll turn it around: since the overlap of foo and bar is
not defined, we don't know that they occupy 42 distinct bytes each;
for example they might be offset by one byte from each other and
perhaps only 43 distinct bytes are accessed, 41 of them twice each.
The memcpy result is not defined for overlap, so we don't know
what the answer will be (fault for overlapping DMA perhaps), but
we can't say 84 bytes accessed for sure.

--
"Any sufficiently advanced bug is indistinguishable from a feature."
-- Rich Kulawiec
  Réponse avec citation
Vieux 04/02/2008, 23h46   #21
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Walter Roberson wrote:
>> Army1987 wrote:

>
>>> (void)memcpy(foo, bar, 42) accesses 42 bytes.

>
>> 84 bytes, I meant.

>
> How do you arrive at that number? memcpy() does not define
> the behaviour if the fields overlap. memmove() is the function

.... snip ...

There is a from and a to parameter. They have restrict applied, so
they are different. Each points to a 42 byte area, which is either
examined or modified. 42 + 42 is roughly equal to 84. In base 10.

7.21.2.1 The memcpy function

Synopsis
[#1]
#include <string.h>
void *memcpy(void * restrict s1,
const void * restrict s2,
size_t n);
Description
[#2] The memcpy function copies n characters from the object
pointed to by s2 into the object pointed to by s1. If
copying takes place between objects that overlap, the
behavior is undefined.

Returns
[#3] The memcpy function returns the value of s1.

Please don't strip attributions for any material you quote.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 05/02/2008, 05h41   #22
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Mon, 04 Feb 2008 18:46:13 -0500, CBFalconer wrote:
> Walter Roberson wrote:

[stripped attribution re-inserted]
>>In article <fo6th2$mls$2@tdi.cu.mi.it>, Army1987 <army1987@NOSPAM.it>

wrote:
>>> Army1987 wrote:

>>
>>>> (void)memcpy(foo, bar, 42) accesses 42 bytes.

>>
>>> 84 bytes, I meant.

>>
>> How do you arrive at that number? memcpy() does not define the
>> behaviour if the fields overlap. memmove() is the function

>
> Please don't strip attributions for any material you quote.


Please don't strip attributions for any material you quote. You stripped
attributions. Walter Roberson didn't, or at least not in the message you
replied to.
  Réponse avec citation
Vieux 05/02/2008, 16h46   #23
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

Peter Nilsson wrote:
> How many bytes does the following access?
>
> memcpy(foo, bar, 0);
>
> If it's none, then why should *memcpy(foo, bar, 0) be
> any different? If it isn't different, then why should
> dereferencing a volatile void pointer be any different?

Ok, another example:

volatile struct incomplete *foo = a_function_returning_a_struct_pointer();
*foo;
How many bytes does the statement access?


--
Army1987 (Replace "NOSPAM" with "email")
  Réponse avec citation
Vieux 05/02/2008, 20h21   #24
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: void * in ANSC

On Tue, 05 Feb 2008 16:46:26 +0000, Army1987 wrote:
> Peter Nilsson wrote:
>> How many bytes does the following access?
>>
>> memcpy(foo, bar, 0);
>>
>> If it's none, then why should *memcpy(foo, bar, 0) be any different? If
>> it isn't different, then why should dereferencing a volatile void
>> pointer be any different?

> Ok, another example:
>
> volatile struct incomplete *foo =
> a_function_returning_a_struct_pointer(); *foo;
> How many bytes does the statement access?


The behaviour is undefined, and no sane behaviour could be defined. Two
compilers I've tried generate an error, one warns and doesn't access any
bytes. (A fourth has problems with respecting the volatile keyword and
doesn't access any bytes even if the type is complete, but let's ignore
that one.) Quoting 6.3.2.1p2:
"Except when it is the operand of the sizeof operator, the unary &
operator, the ++ operator, the -- operator, or the left operand of the .
operator or an assignment operator, an lvalue that does not have array
type is converted to the value stored in the designated object (and is
no longer an lvalue). If the lvalue has qualified type, the value has the
unqualified version of the type of the lvalue; otherwise, the value has
the type of the lvalue. If the lvalue has an incomplete type and does
not have array type, the behavior is undefined."

This applies to struct incomplete but not to void *, because an
expression of type void is by definition never an lvalue, even if it is
the result of the unary * operator.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,34610 seconds with 32 queries