473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing a constant by reference

Hi all,

I want to call a function that takes a void* argument and pass it the address of
a constant. Is there a way to do this without explicitly defining the constant
and then passing the address of the constant? In other words, what I *don't*
want to do is this:

const int myInt = 5;
MyFunc(&myInt);

I want to do something more like this:

MyFunct(&(int)5);

TIA - Bob
Jun 27 '08 #1
12 1268
Bob Altman wrote:
I want to call a function that takes a void* argument and pass it the address of
a constant. Is there a way to do this without explicitly defining the constant
and then passing the address of the constant? In other words, what I *don't*
want to do is this:

const int myInt = 5;
MyFunc(&myInt);

I want to do something more like this:

MyFunct(&(int)5);
A constant expression has no address; what you're asking for is meaningless.
It's like asking where the number 5 is stored in the computer. It's not
stored anywhere, it just *is*. Storage locations produce addresses, not
constant values. Aren't you accidentally confusing this with a literal
address, i.e.

MyFunc((void*) 5);

This will convert the integer 5 to a memory location and pass that as a
pointer. This will then most likely produce an access violation upon access,
since "5" is rarely a valid address, but if the "void*" is meant to merely
hold a pointer-sized value rather than an actual pointer, this might have
some meaning.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
Bob Altman wrote:
Hi all,

I want to call a function that takes a void* argument and pass it the
address of a constant. Is there a way to do this without explicitly
defining the constant and then passing the address of the constant? In
other words, what I *don't* want to do is this:

const int myInt = 5;
MyFunc(&myInt);
That won't work. Does your function actually accept a "const void *"
instead of a "void *"?
>
I want to do something more like this:

MyFunct(&(int)5);

TIA - Bob

Jun 27 '08 #3
>Hi all,
>>
I want to call a function that takes a void* argument and pass it the
address of a constant. Is there a way to do this without explicitly
defining the constant and then passing the address of the constant? In other
words, what I *don't* want to do is this:

const int myInt = 5;
MyFunc(&myInt);

That won't work. Does your function actually accept a "const void *" instead
of a "void *"?
Yes, my function takes a "const void*"
Jun 27 '08 #4
That won't work. Does your function actually accept a "const void *" instead
of a "void *"?
Actually, it turns out that I left the "const" off of the "void *" in my
function declaration. But I don't see how that affects my original question,
which was how to avoid explicitly declaring the constant and passing its
address.
Jun 27 '08 #5
Bob Altman wrote:
>That won't work. Does your function actually accept a "const void *" instead
of a "void *"?

Actually, it turns out that I left the "const" off of the "void *" in my
function declaration. But I don't see how that affects my original question,
which was how to avoid explicitly declaring the constant and passing its
address.
By taking its address. Let's get a few things straight here. This is a constant:

const int a = 5;

This is *not* a constant, but a constant expression:

5

Expressions (whether constant or not) do not *have* addresses. No storage is
allocated to them. If you want an address, you need a storage location. As I
mentioned, you can do this:

(const void*) 5

This is "the address with numerical value 5". It's not "the address of the
constant 5" because there's no such thing.

Why not take a step back and tell us what you're trying to ultimately
achieve, instead of insisting on the impossible?

--
J.
Jun 27 '08 #6
Bob Altman wrote:
>That won't work. Does your function actually accept a "const void
*" instead of a "void *"?

Actually, it turns out that I left the "const" off of the "void *" in
my function declaration. But I don't see how that affects my
original question, which was how to avoid explicitly declaring the
constant and passing its address.
Once you add the "const" your problem should disappear:

int(5); // creates a temporary of type int
MyFunct(&(int(5))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
Jun 27 '08 #7
Ben Voigt [C++ MVP] wrote:
Bob Altman wrote:
>>That won't work. Does your function actually accept a "const void
*" instead of a "void *"?
Actually, it turns out that I left the "const" off of the "void *" in
my function declaration. But I don't see how that affects my
original question, which was how to avoid explicitly declaring the
constant and passing its address.

Once you add the "const" your problem should disappear:

int(5); // creates a temporary of type int
MyFunct(&(int(5))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
This does not compile. We're talking pointers here, not references, and
there's no such thing as a void&.

--
J.
Jun 27 '08 #8
That's just not in the language(s).

Thanks. That's the answer I was afraid I'd get. I'm stuck with passing
pointers 'cause I need to expose stuff using C bindings from a library that's
callable from other languages (Ada, FORTRAN, etc.).
Jun 27 '08 #9
>Once you add the "const" your problem should disappear:
>>
int(5); // creates a temporary of type int
MyFunct(&(int(5))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
This does not compile. We're talking pointers here, not references, and
there's no such thing as a void&.
Here's one that compiles (with VC2008):

void fn1( void * p )
{
int x = *static_cast<int*>(p);
}

fn1( const_cast<int *>( &static_cast<const int &>(5) ) );

.... but I think it stinks! :)

Dave
Jun 27 '08 #10
David Lowndes wrote:
>>Once you add the "const" your problem should disappear:

int(5); // creates a temporary of type int
MyFunct(&(int(5))); // takes the address of this temporary, references
can be bound to temporaries only if the reference is const
This does not compile. We're talking pointers here, not references, and
there's no such thing as a void&.

Here's one that compiles (with VC2008):

void fn1( void * p )
{
int x = *static_cast<int*>(p);
}

fn1( const_cast<int *>( &static_cast<const int &>(5) ) );

... but I think it stinks! :)
Oh, right... Ben's solution actually *does* work, with a slight modification
-- a cast to a reference forces generation of a bound temporary. The trick
is that the reference has to be const. Recall that the OP actually did want
to use a const void* after all, so no further casting is necessary:

MyFunct(&static_cast<const int&>(5));

Of course, this is still unsafe. MyFunct() better not store that pointer
anywhere for later use. It does meet the original requirement of not needing
to declare a separate variable, for whatever that's worth.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #11
>void fn1( void * p )
>{
int x = *static_cast<int*>(p);
}

fn1( const_cast<int *>( &static_cast<const int &>(5) ) );

... but I think it stinks! :)
Oh, right... Ben's solution actually *does* work, with a slight modification
-- a cast to a reference forces generation of a bound temporary. The trick
is that the reference has to be const. Recall that the OP actually did want
to use a const void* after all, so no further casting is necessary:
Ah, I'd forgot he'd mentioned const later on in the thread.
>Of course, this is still unsafe. MyFunct() better not store that pointer
anywhere for later use.
I think that statement would apply to any local variable though.
>It does meet the original requirement of not needing
to declare a separate variable, for whatever that's worth.
Yes, it's just a way of achieving the same as the more clear way of
defining a local variable - the generated code will be pretty much
identical.

Dave
Jun 27 '08 #12
David Lowndes wrote:
>>void fn1( void * p )
{
int x = *static_cast<int*>(p);
}

fn1( const_cast<int *>( &static_cast<const int &>(5) ) );

... but I think it stinks! :)
Oh, right... Ben's solution actually *does* work, with a slight modification
-- a cast to a reference forces generation of a bound temporary. The trick
is that the reference has to be const. Recall that the OP actually did want
to use a const void* after all, so no further casting is necessary:

Ah, I'd forgot he'd mentioned const later on in the thread.
>Of course, this is still unsafe. MyFunct() better not store that pointer
anywhere for later use.

I think that statement would apply to any local variable though.
True, but the scope of a temporary is even smaller than that of a local
variable, and taking the address of one is even less common (so it might not
throw up a warning flag when it really should).

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #13

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

Similar topics

18
by: Johnd | last post by:
I am running into an issue when I try to write more than 1024 characters to a memo field. Apparantly the odbc connection I am using does not permit literals to be larger that 1024 characters. ...
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
2
by: conrad | last post by:
In C, we have pass by value only. This results from values being the product of 'the value of the expression'. Now, I imagine that the C++ standard has the same concept of the 'value of the...
3
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing...
20
by: MartinRinehart | last post by:
Is the following correct? x = "some string" x is a reference to "some string" foo(x) Reference is passed to function.
4
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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,...

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.