473,743 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void* versus uint32*

I have never really understood the difference between the two notations
below.

I often run into code where they are passing in the address of some
location.

Some people do something like this:

foo( void* p_data ) // pass in a pointer to some data

while I see others do this.

foo( uint32* p_data) // pass in a pointer to some data

Can someone please explain to me why you would choose one over the other.

Someone once told me using the uin32 guarantees alignment on a 32 bit
boundary.

Any help is greatly appreciated.
Jul 23 '05 #1
4 3401
john smith wrote:

I have never really understood the difference between the two notations
below.

I often run into code where they are passing in the address of some
location.

Some people do something like this:

foo( void* p_data ) // pass in a pointer to some data

while I see others do this.

foo( uint32* p_data) // pass in a pointer to some data

Can someone please explain to me why you would choose one over the other.


I use void pointers only to signal to the user of some functionality:
"Here you get a pointer, but is not of your business whats behind
that pointer, leave that alone!"

Eg. I have a geometry engine (from my dark ages). On request that
engine creates an object. It gives back a pointer to that object.
If any operation on that object needs to be done, the users code
has to present that pointer again. So the pointer acts as some sort
of 'handle' to the users code. He gets one from me, he has to present
it again in operations. But he definitly should not care what data
structure is behind that handle.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #2
In message <42************ ***@gascad.at>, Karl Heinz Buchegger
<kb******@gasca d.at> writes
john smith wrote:

I have never really understood the difference between the two notations
below.

I often run into code where they are passing in the address of some
location.

Some people do something like this:

foo( void* p_data ) // pass in a pointer to some data

while I see others do this.

foo( uint32* p_data) // pass in a pointer to some data

Can someone please explain to me why you would choose one over the other.

Given the choice, use neither.

I use void pointers only to signal to the user of some functionality:
"Here you get a pointer, but is not of your business whats behind
that pointer, leave that alone!"

Eg. I have a geometry engine (from my dark ages). On request that
engine creates an object. It gives back a pointer to that object.
If any operation on that object needs to be done, the users code
has to present that pointer again. So the pointer acts as some sort
of 'handle' to the users code. He gets one from me, he has to present
it again in operations. But he definitly should not care what data
structure is behind that handle.

Ugh. Surely *you* care that what he's giving you back pointers to isn't
garbage, and if you're giving him more than one kind of object, you're
asking for trouble. You'd be far safer giving him a pointer to a
distinct but incomplete type. He still can't tell what lies behind it,
but at least he knows that different structures have different types and
the compiler helps to enforce that their pointers can't be mixed.

Unless you're stuck with a C-style third-party API that mandates it, the
only place for void * is inside library code.

Say what you mean, and mean what you say.

--
Richard Herring
Jul 23 '05 #3
Richard Herring wrote:
Eg. I have a geometry engine (from my dark ages). On request that
engine creates an object. It gives back a pointer to that object.
If any operation on that object needs to be done, the users code
has to present that pointer again. So the pointer acts as some sort
of 'handle' to the users code. He gets one from me, he has to present
it again in operations. But he definitly should not care what data
structure is behind that handle.

Ugh. Surely *you* care that what he's giving you back pointers to isn't
garbage, and if you're giving him more than one kind of object, you're
asking for trouble. You'd be far safer giving him a pointer to a
distinct but incomplete type. He still can't tell what lies behind it,
but at least he knows that different structures have different types and
the compiler helps to enforce that their pointers can't be mixed.


As said: This code originated in the 'dark ages' :-)
But of course, in the debug version the pointer is stored in a map
and checked when given back. Worked reasonable well since more then
10 years with a surprising low number of pointer type mismatches ( that
is *0* in all those >10 years).

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #4

"john smith" <pr************ **@charter.net> wrote in message
news:Ps******** *******@fe08.lg a...
I have never really understood the difference between the two notations
below.

I often run into code where they are passing in the address of some
location.

Some people do something like this:

foo( void* p_data ) // pass in a pointer to some data

while I see others do this.

foo( uint32* p_data) // pass in a pointer to some data

Can someone please explain to me why you would choose one over the other.

Someone once told me using the uin32 guarantees alignment on a 32 bit
boundary.


There is no standard type uint32. Apparently, it's something defined by
your compiler's library (such as Visual C++).

I'd use uint32* if I was passing the address of a uint32 variable. It has
nothing whatsoever to do with alignment. It's a pointer to an object of
some known type, and it should point to the type of object it says it points
to! To do anything else would require a cast (and could possibly cause
problems at some point).

Perhaps that person's statement about alignment had to do with choosing the
uint32 type for a variable, such as the one whose address gets passed to
this function. But you should choose variable types based on design
requirements. If the actual size isn't that vital to know, use int, because
that is defined to be the size used by the system it's being compiled for
(e.g., 64 bits for a 64-bit system).

I rarely declare any functions that take a void* pointer. Really, the only
use I have for them is to provide a place for API callbacks to store a
pointer to one of my objects (the one that registers to receive the
callback). That lets me cast the void* pointer to a pointer of the type I
*know* it really is, so that I can call a member function of that object to
actually do the callback work. This kind of trick is needed because the API
generally has no way of knowing what type of object it's going to call back
to, so it calls a free-standing function which I write, which *does* know
the type of object to make the call to.

Jul 23 '05 #5

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

Similar topics

9
7500
by: Pushker Pradhan | last post by:
I've a function which accepts void * as the arg because the actual datatype can be char or float or anything. The fuction: void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float *h, uint32 hlen, dwt_process ROWSORCOLS) Usage in main() convolve((float*)x, xLP, tileLength, tileWidth, ld, filterlen, DWT_ROWS);
8
5803
by: Luc Le Blanc | last post by:
I have 2 APIs that store/recall a void *. Since all I need to store is a 32-bit struct, I pass the actual data (instead of a pointer to it) as a void *: typedef { UInt8 color; UInt8 index; UInt16 resID; } GadgetData;
9
6208
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
4
2639
by: LordHog | last post by:
Hello all, I am having a little problem getting my (void *) assignment to work correctly. Basically it is some code for a circular queue buffer I am trying to implement, but getting the deferrencing of the (void *) type is giving me problems... For brevity sake not all the functions are defined, but the name gives a clue to what it does...
1
1693
by: Michel Racicot | last post by:
Hi there, The following code is troubling me: UInt32 nOffset = nStringEntriesOffset + (40 * nIndex); nStringEntriesOffset is a UInt32 containing 2124 nIndex is a UInt32 containing 5213 After the execution of the above line, nIndex equals to : 14036 !!! What the hell is hapening?
9
8628
by: Chris Botha | last post by:
Hi, I have an UInt32 and want to stick the value into an Integer and get an Overflow exception, BUT using C# the same value can be casted into an int and the value is as expected. The Hex value is FFFFFFDB, which should be -37. Thanks.
21
2333
by: ashish.sadanandan | last post by:
Hi, I haven't done a lot of C++ programming (done a lot of it in C) and the reason why I'm considering switching now is also the question I'm posting here. I have some library functions that return to me a void* to data that has been specifed by the user. There are also functions that return an enum which contains information about the data type of this data. So what I've done in the past is made a switch case statement based on the...
2
4335
by: Bern McCarty | last post by:
In the old MEC++ syntax I can do this: // compile in VS 2005 shell with cl -clr:oldsyntax -LD ArrayCopyOldSyntax.cpp #using <mscorlib.dll> public __gc class CopyTest { private: System::UInt32 m_indeces __gc; public: CopyTest(unsigned int indeces __nogc, unsigned int arrayLength)
11
15393
by: Yaniv | last post by:
Hi How can I convert Uint32 variable to System.drawing.color ?? Thanks in advanced Yaniv
0
9486
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
9344
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...
0
9214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8218
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...
0
6057
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4572
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...
0
4827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3286
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2759
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.