Hi all,
if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.
Also, is sizeof(char) guaranteed to always be 1? How does this
interact with CHAR_BIT?
Also, are
sizeof(unsigned sometype) == sizeof(sometype)
sizeof(unsigned sometype) == sizeof(signed sometype)
always guaranteed to be true for types which can be modified by signed
and unsigned?
On a similar track, what about:
( (unsigned sometype*)vptr ) + 1 == ( (sometype*)vptr ) + 1
( (unsigned sometype*)vptr ) + 1 == ( (signed sometype*)vptr ) + 1
Thanks,
viza 5 3382
viza wrote:
if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
It's not legal C, since you can't do arithmetic on void*
values in standard C.
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.
gcc defaults to allowing void* arithmetic, but can be disuaded
with suitable command-line options.
Also, is sizeof(char) guaranteed to always be 1?
Yes.
How does this interact with CHAR_BIT?
It doesn't. 1 is 1 and all alone and ever more shall be so.
--
"It would have to be enough." /Brokedown Palace/
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:
Hi all,
if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.
>
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.
void pointer arithmetic is not allowed by ISO C90/C99. (and ANSI C89)
However, gcc allows it as an extension, (it will *error* in c89/c99
mode)
Also, is sizeof(char) guaranteed to always be 1? How does this
interact with CHAR_BIT?
It does not. sizeof(char) is guaranteed to be 1 and CHAR_BIT at least
8.
Also, are
sizeof(unsigned sometype) == sizeof(sometype)
sizeof(unsigned sometype) == sizeof(signed sometype)
always guaranteed to be true for types which can be modified by signed
and unsigned?
Types are not modified by signed and unsigned, but yes, it is
guaranteed.
>
On a similar track, what about:
( (unsigned sometype*)vptr ) + 1 == ( (sometype*)vptr ) + 1
( (unsigned sometype*)vptr ) + 1 == ( (signed sometype*)vptr ) + 1
Yes vi******@gmail.com writes:
On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:
>if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.
If *you* mean:
(void *)((type *)ptr + 1) == (void *)((char *)ptr + sizeof(type))
then yes. In your example the two pointer types can't be compared
(unless type is a character type).
--
Ben.
On Jun 10, 7:33 pm, vipps...@gmail.com wrote:
On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:Hi all,
if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
Don't you mean
((type*)ptr)+1 == ((char*)ptr)+sizeof(type)
In that case, yes, it is guaranteed.
it fails of course if sometype is void, and gcc sometimes screams
about void pointer arithmetic.
void pointer arithmetic is not allowed by ISO C90/C99. (and ANSI C89)
However, gcc allows it as an extension, (it will *error* in c89/c99
mode)Also, is sizeof(char) guaranteed to always be 1? How does this
interact with CHAR_BIT?
It does not. sizeof(char) is guaranteed to be 1 and CHAR_BIT at least
8.Also, are
sizeof(unsigned sometype) == sizeof(sometype)
sizeof(unsigned sometype) == sizeof(signed sometype)
always guaranteed to be true for types which can be modified by signed
and unsigned?
Types are not modified by signed and unsigned, but yes, it is
guaranteed.
On a similar track, what about:
( (unsigned sometype*)vptr ) + 1 == ( (sometype*)vptr ) + 1
( (unsigned sometype*)vptr ) + 1 == ( (signed sometype*)vptr ) + 1
Yes
Correct me if I am wrong:
sizeof(char) is 1 and CHAR_BITS is >= 8. What exactly sizeof returns?
AFAIK it returns number of bytes. So 1 byte in C is sizeof char but
not necesarily this byte has to be 8 bits. Its not that the sizeof
char is defined in terms of bytes but bytes are defined in terms of
sizeof char.
Ben Bacarisse wrote:
vi******@gmail.com writes:
>On Jun 10, 5:17 pm, viza <tom.v...@gmail.comwrote:
>>if I have:
void *vptr= malloc( ENOUGH );
is this guaranteed to always be true:
( (sometype*)vptr ) + 1 == vptr + sizeof(sometype)
>Don't you mean ((type*)ptr)+1 == ((char*)ptr)+sizeof(type) In that case, yes, it is guaranteed.
If *you* mean:
(void *)((type *)ptr + 1) == (void *)((char *)ptr + sizeof(type))
then yes. In your example the two pointer types can't be compared
(unless type is a character type).
Assuming that vptr is not a null pointer.
--
Thad This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Xavier Decoret |
last post by:
This is a compiler error that I get when I try to do:
void* p;
unsigned int stride;
// ...
p += stride;
I have good reason to do this, namely an iterator over an array of
object whose...
|
by: Martijn |
last post by:
Hi,
I am looking over some of my old implementations. One of them has an
"array" of void pointers defined as a pointer to the first element (without
getting stuck on nomenclature, I am aware of...
|
by: Alex Fraser |
last post by:
From searching Google Groups, I understand that void pointer arithmetic is a
constraint violation, which is understandable. However, generic functions
like qsort() and bsearch() must in essence do...
|
by: Xiangliang Meng |
last post by:
Hi, all.
As far as I know, the speical arithmetic operator on void pointer is an
externsion in GNU CC. However, I could not find the relative topics in C99.
Would someone like to help me find...
|
by: infobahn |
last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
| |
by: Alfonso Morra |
last post by:
Hi,
I'm getting a compiler error of "pointer truncation from 'void *' to
'int'" because I'm not explicitly casting from void* to (datatype*).
Pointers are stored as ints (is that right?), so as...
|
by: Erik de Castro Lopo |
last post by:
Hi all,
The GNU C compiler allows a void pointer to be incremented and
the behaviour is equivalent to incrementing a char pointer.
Is this legal C99 or is this a GNU C extention?
Thanks in...
|
by: arun |
last post by:
Hi,
Why sizeof operator when applied on void returns one when compiled
with gcc compiler ??. When i tried it on VC++ compiler, it gives an
error. But another version of the VC++ compiler on my...
|
by: Angus |
last post by:
I am using a global which is a void*
I have it defined in one file as:
void* hFlag;
and one other header file as:
extern void* hFlag;
But I get this compile error:
|
by: raphfrk |
last post by:
Is this valid?
int a;
void *b;
b = (void *)a; // b points to a
b += 5*sizeof(*a); // b points to a
a = 100;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |