473,666 Members | 2,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type-casting void pointers?

Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?

hugo ---------

Nov 15 '05 #1
25 10150
"hugo2" <ob****@yahoo.c om> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?


IMO there's no need in this if those are void*.
And unless someone's Clib implementation is broken, size_t is a nonnegative
type, hence no need to put size-->0, simply size would do.
I've seen ssize_t somewhere in linux recently, even in the single unix spec.
Stupid thing IMO. It limits the valid range by allowing signed values asking
for problems...

Alex
Nov 15 '05 #2
hugo2 wrote:
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/
Did you forget an 'e' or get wrong the order of 'i'
and 'l'? SCNR
void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?


In the case of void*, the cast is completely unnecessary.
I do not know the definition of the type "byte" but if
it is anything other than a typedef for "unsigned char", I would
suggest that you have a look at the wisdom to be found in
c.l.c rather than believing this book: Unnecessary casts
are a Bad Thing.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
hugo2 wrote:
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?

You are correct, the cast is not required in C. It would be in C++, so
that could be reason for that usage. It's similar to the casts for the
returns from the *alloc() family, not necessary but a LOT of code
examples out there do it anyway. I don't know anything about that book
you mention.


Brian
Nov 15 '05 #4
hugo2 wrote:

Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?
No.
/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int.
What do you mean by that?
I don't see any code about "unsigned int".
"byte" either means "unsigned char" or the definition is wrong.
memcpy works on objects of all types.
Why not simply byte *pbTo = pvTo; to initialize?


Sure.
Also, the parameter types are wrong.
There should be a const qualifier in front of void *pvFrom,
and if it were there,
then who knows if Steve Maguire's intention
would be to cast the qualifier away or not?

I'm also not to crazy about the "byte" typedef or macro,
which ever it is.

--
pete
Nov 15 '05 #5
"hugo2" <ob****@yahoo.c om> writes:
[snip]
The addresses are all unsigned int.

[snip]

Are you assuming that an address is represented as an unsigned int?
If so, that assumption is neither correct nor necessary. An address
is an address. Addresses/pointers can be converted to and from
integer types, but there are very few guarantees about the results.
And I've worked on machines where unsigned int is 32 bits and pointers
are 64 bits.

A conforming implementation could implement pointers as fixed-length
strings that give instructions, in English, for retrieving the
referenced object ("1st memory board, 3rd chip on the left, 7th word").

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Nov 15 '05 #6


Alexei A. Frounze wrote:
"hugo2" <ob****@yahoo.c om> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?
IMO there's no need in this if those are void*.
And unless someone's Clib implementation is broken, size_t is a nonnegative
type, hence no need to put size-->0, simply size would do.
I've seen ssize_t somewhere in linux recently, even in the single unix spec.
Stupid thing IMO. It limits the valid range by allowing signed values asking
for problems...

Alex

From hugo, July 16, 2005

Mr Alex, I think, took a space out of size-- >0
After first seeing it, I read "subtract 1 from size
and compare to 0, greater? do while... subtract 1
form size, compare to 0, and so on.

hugo-------

Nov 15 '05 #7
"hugo2" <ob****@yahoo.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
....
void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?


IMO there's no need in this if those are void*.
And unless someone's Clib implementation is broken, size_t is a nonnegative type, hence no need to put size-->0, simply size would do.
I've seen ssize_t somewhere in linux recently, even in the single unix spec. Stupid thing IMO. It limits the valid range by allowing signed values asking for problems...

Alex

From hugo, July 16, 2005

Mr Alex, I think, took a space out of size-- >0
After first seeing it, I read "subtract 1 from size
and compare to 0, greater? do while... subtract 1
form size, compare to 0, and so on.


Yeah, I obviously meant this:
....
while(size--)
....
No need to check for the sign, just for 0. If we code way too solid (I'd
rather say overdefensively ), we may end up in a clinic for psychos with a
diagnosis of multiple phobias :)

Alex
Nov 15 '05 #8
Default User wrote:
hugo2 wrote:
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?

You are correct, the cast is not required in C. It would be in C++, so
that could be reason for that usage. It's similar to the casts for the
returns from the *alloc() family, not necessary but a LOT of code
examples out there do it anyway. I don't know anything about that book
you mention.


I think some compilers give a warning without the explicit cast.
Maybe the author wanted to write 'warning-free' code.

Stephan
Nov 15 '05 #9
Stephan Hoffmann wrote:
Default User wrote:
hugo2 wrote:
Obrhy/hugo July 12, 2004

Take a look at this memcpy() definition.
Is there a good reason the void pointer
args are cast to byte just to assign their
addresses to byte pointers?

/*from Steve Maguire's 'Writing Soild Code'*/

void *memcpy(void *pvTo,void *pvFrom,size_t size)
{
byte *pbTo = (byte *)pvTo;
byte *pbFrom = (byte *)pvFrom;
while(size-- >0)
*pbTo++ = *pbFrom++;

return (pvTo);
}

The addresses are all unsigned int. Why not
simply byte *pbTo = pvTo; to initialize?


You are correct, the cast is not required in C. It would be in
C++, so that could be reason for that usage. It's similar to the
casts for the returns from the *alloc() family, not necessary but
a LOT of code examples out there do it anyway. I don't know
anything about that book you mention.


I think some compilers give a warning without the explicit cast.
Maybe the author wanted to write 'warning-free' code.


If a warning appears it is because it is needed. Useless casts
serve only to preserve programming errors.

--
"If you want to post a followup via groups.google.c om, 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
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4518
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
3
510
by: dgaucher | last post by:
Hi, I want to consume a Web Service that returns a choice, but my C++ client always receives the same returned type. On the other hand, when I am using a Java client, it is working fine (of course, the generated proxy is not the same). When I am looking at the C++ generated code, it seems fine, but when I am executing the code, I always get the first choice type.
2
3298
by: Jason Cartwright | last post by:
I have an abstract base class and two derived classes that I want to serialize and deserialize with schema validation. When I serialize instances of the derived classes the XmlSerializer adds the xsi:type="DerivedClass" attribute and the Instance Namespace. When I attempt to validate the xml upon deserialization the XmlValidatingReader chokes on this attribute value. I can't seem to find a way around this. Any suggestions?
100
5237
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
29
5448
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
0
17774
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico Mariani, performance architect for the Microsoft® .NET runtime and longtime Microsoft developer, mentioned to Dr. GUI in an e-mail conversation recently that a fairly common practice (and one that's, unfortunately, described in some of our...
0
1773
by: Chris Fink | last post by:
When I am consuming a webservice, an object has an undefined value (inq3Type.Call3Data). I do not completely understand why this is happening and apologize for the vague question. My assumption is that the WSDL is defined incorrectly and .NET cannot parse the types. Any help is greatly appreciated! CustDDGSvc ws = new CustDDGSvc(); ws.Url = "http://dmapfra003.decisionone.com:8080/JISOAP/CustDDGSvc"; // don't understand why the...
1
8698
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections Framework are said to have an element type. http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
669
25860
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
5
3166
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a instance is created by "calling" a class object.
0
8438
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7376
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4186
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.