473,848 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question realted to void *

Guys,

Consider a function
func(void **var)
{

/* In function I need to typecast the variable var as (int **)
I mean to say, I need to access var as (int **)

}

My question is what is the better option, should I use
func(void **var) Or
func(void *var).

I think func(void *) should be fine, as we can store any type of
"pointer variable" in "void pointer variable".
void pointer variable should be big enough to hold any type of pointer
variable.

Is that correct? Or I am missing something?

And if that is correct, should we ever use void ** ?

thanks for any help/response.


Sep 16 '08 #1
28 1831
On Sep 16, 4:54*pm, "junky_fel...@y ahoo.co.in"
<junky_fel...@y ahoo.co.inwrote :
Guys,

* *Consider a function
func(void **var)
{

/* In function I need to typecast the variable var as (int **)
* *I mean to say, *I need to access var as (int **)

}

My question is what is the better option, should I use
func(void **var) *Or
func(void *var).

I think func(void *) should be fine, as we can store any type of
"pointer variable" in "void pointer variable".
void pointer variable should be big enough to hold any type of pointer
variable.

Is that correct? Or I am missing something?

And if that is correct, should we ever use void ** ?

thanks for any help/response.
You can cast anything to anything you want, but the less confusing you
can make it, the better. Ideally, func() would take an "int** var" if
that is what it really is. However, sometimes, as appears to be true
in this case, it is necessary to use void. If you need to use void,
then I would still prefer "void**" if what you are passing really is a
pointer-to-pointer.

To me what matters more than how you cast "var" inside the function,
is what the function signature tells the client caller. If I'm going
to call a function with a signature "func(void* )" then I expect to
pass a pointer, not a pointer-to-pointer. For example:

void f1(void*);
void f2(void**);

int x = 0;
int* px = &x;

f1(px); /* pointer */
f2(&px); /* pointer to pointer */

Adam
Sep 16 '08 #2
ju**********@ya hoo.co.in wrote:
Guys,

Consider a function
func(void **var)
{

/* In function I need to typecast the variable var as (int **)
I mean to say, I need to access var as (int **)

}

My question is what is the better option, should I use
func(void **var) Or
func(void *var).

I think func(void *) should be fine, as we can store any type of
"pointer variable" in "void pointer variable".
void pointer variable should be big enough to hold any type of pointer
variable.

Is that correct? Or I am missing something?
It's not certain, because you've told us how you intend to
do something but not what you're trying to do. The answer to
"Should I use a screwdriver or a hammer?" depends greatly on
whether you're trying to drive a screw, hammer a nail, or apply
paint ...

Perhaps what you're missing is that pointers of different
types aren't interchangeable -- that is, you've not understood
question 4.9 in the FAQ. But it's hard to be sure, based on
your rather sketchy description of your goals.
And if that is correct, should we ever use void ** ?
I'll ignore everything up through the comma, because there's
not enough information to evaluate it. As for the rest, void**
crops up rather rarely, but does sometimes make sense.

--
Er*********@sun .com
Sep 16 '08 #3

<ju**********@y ahoo.co.inwrote in message
>
And if that is correct, should we ever use void ** ?
A void ** is for a list of pointers to arbitrary memory buffers.
A void * is for a pointer to an arbitary memory buffer.

Since arbitrary memory buffers can contain lists of pointers, you can always
use a void *, just as you could for any other type.

So the question is really, "should all parameters be passed as void *'s?"

The answer is no. Only occasionally does it make sense to hide the type of
pointer from the calling function. Use of actual types enables checking in
the compiler, and is easier to read.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 16 '08 #4
"ju**********@y ahoo.co.in" <ju**********@y ahoo.co.inwrite s:
[...]
And if that is correct, should we ever use void ** ?
void* can be used as a generic pointer type. What that means is that
a value of any pointer type (other than a pointer-to-function type)
can be converted to void* and back again without loss of information.

You might think that void** is therefore a generic pointer-to-pointer
type, but it isn't; in fact, there is no generic pointer-to-pointer
type.

You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 16 '08 #5
Malcolm McLean wrote:
<ju**********@y ahoo.co.inwrote in message
>And if that is correct, should we ever use void ** ?

A void ** is for a list of pointers to arbitrary memory buffers.
A void * is for a pointer to an arbitary memory buffer.
No, a void** is for a pointer to a void*. When an array of void*
is passed anywhere, the reference is made with a void**. Just the
same as any other array reference.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Sep 16 '08 #6
abu...@ieee.org wrote:
<junky_fel...@y ahoo.co.inwrote :
* *Consider a function
func(void **var)
{
/* In function I need to typecast the variable var as
(int **)
* *I mean to say, *I need to access var as (int **)
}

My question is what is the better option, should I use
func(void **var) *Or
func(void *var).

I think func(void *) should be fine, as we can store
any type of "pointer variable" in "void pointer
variable".
You can store any pointer to an object or incomplete type.
void pointer variable should be big enough to hold any
type of pointer variable.
There is no requirement for void * to be big enough to
hold function pointers.
Is that correct? Or I am missing something?

And if that is correct, should we ever use void ** ?

You can cast anything to anything you want,
You can try. But you can also fail in the attempt.
but the less confusing you can make it, the better.
Ideally, func() would take an "int** var" if that is
what it really is. However, sometimes, as appears to
be true in this case, it is necessary to use void. If
you need to use void, then I would still prefer
"void**" if what you are passing really is a
pointer-to-pointer.
Just realise that the guarantees that go with void * are
not necessarily available to void **.
To me what matters more than how you cast "var" inside
the function, is what the function signature tells the
client caller. If I'm going to call a function with a
signature "func(void* )" then I expect to pass a pointer,
not a pointer-to-pointer. For example:

* * void f1(void*);
* * void f2(void**);

* * int x = 0;
* * int* px = &x;

* * f1(px); * */* pointer */
* * f2(&px); * /* pointer to pointer */
This violates a constraint. There is no implicit
conversion to or from void ** as there is for void *.

But even if I cast &px in the call to f2, there is no
guarantee that the conversion of &px to void ** succeeds.

Whilst unlikely in practice, the standard allows void *
to have stricter alignment than int *. In that case, the
conversion of int ** to void ** can fail.

--
Peter
Sep 16 '08 #7
In article
<0c************ *************** *******@l33g200 0pri.googlegrou ps.com>,
"ju**********@y ahoo.co.in" <ju**********@y ahoo.co.inwrote :
func(void **var)
{

/* In function I need to typecast the variable var as (int **)
I mean to say, I need to access var as (int **)
}

My question is what is the better option, should I use
func(void **var) Or
func(void *var).
If you have a choice, why is

void func( int** );

not an option, if that's what you're casting the argument to anyway?
That allows better type checking.
Sep 16 '08 #8
ju**********@ya hoo.co.in said:
Guys,

Consider a function
func(void **var)
{

/* In function I need to typecast the variable var as (int **)
By "typecast" I presume you mean "cast".

If the function requires int **, pass it int ** and accept it as int **.
My question is what is the better option, should I use
func(void **var) Or
func(void *var).
void func(int **var)
I think func(void *) should be fine, as we can store any type of
"pointer variable" in "void pointer variable".
Not function pointers, but otherwise yes, provided the value is
determinate.
void pointer variable should be big enough to hold any type of pointer
variable.
With the possible exception of function pointers, yes (and broadly
speaking, this is why C doesn't guarantee that you can store function
pointers in a void *).
Is that correct? Or I am missing something?
You're missing the obvious: void func(int **var)
And if that is correct, should we ever use void ** ?
Rarely. Very rarely. It is easy to make the mistake of thinking that void
** has the same guarantees as void *, but it doesn't.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 16 '08 #9
>
With the possible exception of function pointers, yes (and broadly
speaking, this is why C doesn't guarantee that you can store function
pointers in a void *).
Is that correct? Or I am missing something?

You're missing the obvious: void func(int **var)
Actually, I want to typecast "var" with some structure type. Just to
keep it simple I used "int".
However, the conclusion the I make is as follows. Please correct me if
I am wrong.

1) There is nothing wrong in using "void *" (in case var is not a
function pointer).
2) Using "void **" is a better option as it is more readable and we
don't lose the pointer-type information.

thanks a lot guys..
Sep 17 '08 #10

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

Similar topics

4
1983
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is some typedef structure and offcourse x is a pointer to it. Needless to say my intention for writing such functions in C was to protect the accident write to x's content. So far so good. While porting to C++, I've mada DATA_TYPE as class and I want...
1
2848
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from remote server and display this info on the UI. From doing some research, I know that the way my implementation works today is not thread-safe, because essentially my worker thread updates the UI, which is BAD. So, here I am trying to figure out how...
1
5549
by: Eric | last post by:
Consider the following: class ParentClass { public: void FunctionOne( void ); protected: void FunctionTwo( void ); private: void FunctionThree( void );
2
1440
by: Bob | last post by:
Got a datagridview with records bound to tableParent. Each time I go to a new row (single select) in the datagrid view I want a series of textboxes as well as another datagridview to shwo the realted records. The textboxes are in tableChild1 and the other datagridview is of tableChild2, Both these tables have a PK-FK relationship established in the dataset being used. How can I do this? Any help appreciated. Bob
14
1350
by: streamkid | last post by:
i'm a learning newbie at c++... and i have the following question... reading some source code, i saw this: int function(const void * one, const void * two) { int var1, var2; var1 = *((int*)one); var2 = *((int*)two); /* sm other code here*/ }
29
2012
by: Amar Kumar Dubedy | last post by:
implement a c++ class such that it allows us to add data members at runtime.
12
3031
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
4
1630
by: sip.address | last post by:
Hi there, When creating interfaces and implementations, the usual thing is doing somethign like class Interface { public: virtual void f() = 0; virtual void g() = 0; };
0
9892
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
9735
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
10997
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...
1
10718
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
10347
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...
1
7889
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
7067
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
5731
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...
3
3172
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.