What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)
--
Tobias DiPasquale
Nov 14 '05
17 2656 Da*****@cern.ch (Dan Pop) writes: In <cc**********@n ntp1.jpl.nasa.g ov> "E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Keith Thompson wrote:
Artie Gold writes:
codeslinge r wrote:
>What is the point of the construct in the subject line? Why the use of >two const labels? Does that do something different than const void *x? >And what would void * const x indicate? Thanks in advance :)
It means that neither the pointer nor what it points to may be changed.
Right, but since it points to void, what it points to can't be changed anyway.
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
Are you saying that memcpy cannot change what dest points to just because dest is of type void*?
Nope, he's *obviously* saying that dest itself cannot be used for this purpose: it has to be converted to an object pointer type first.
Right. On the other hand, a pointer to const void cannot be converted
to a pointer to non-const something-else without an explicit cast, so
the const qualifier in
const void *x;
is meaningful.
--
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.
In <Xn************ *************@2 12.27.42.66> Emmanuel Delahaye <em**********@n oos.fr> writes: In 'comp.lang.c', Da*****@cern.ch (Dan Pop) wrote:
In 'comp.lang.c', Keith Thompson <ks***@mib.or g> wrote:
Artie Gold <ar*******@aust in.rr.com> writes: > codeslinger wrote: > > What is the point of the construct in the subject line? Why the use > > of two const labels? Does that do something different than const > > void *x? And what would void * const x indicate? Thanks in advance > > :) > > It means that neither the pointer nor what it points to may be > changed.
Right, but since it points to void, what it points to can't be changed anyway.
Nonsense. A pointer can be aliased by a typed one.
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^ But you're still not using the original pointer for this purpose. So, where is the nonsense?!? <quote> "but since it points to void, what it points to can't be changed anyway." </>
This is wrong. It's not because a pointer is void that the pointed data is not accessible, at least indirectly.
A pointer to void cannot, by definition, point to any data. It is not
a data pointer. It merely contains an address with no data type attached.
If you want to access any data, you need first to either convert it or
to alias it with a pointer to character type.
The point is that the alias must be const too.
If there is such a point, your example doesn't reflect it.
char s[] = "Hello"; /* R/W data */ void const *p = s; /* not dereferencable. */ char *pa = p; /* Diagnostic */ char const *pb = p; /* Correct. Read only acces */
I can't see any pointer aliasing another pointer in your example. Are you sure you know what you're talking about?
I came with the idea that 'aliasing' was the result of more than one pointer pointing to the same location. If you have a better definition, I'd be glad to ear it.
You were talking about aliasing a pointer, not about aliasing the
pointed to data. See the underlined text above. As it happens, void
pointers can be aliased with character pointers, because they have the
same representation. A concrete example is left as an exercise to the
reader.
Hence, in my example, 'pa' and 'pb' are aliasing 'p'.
Nope. All three are aliasing the pointed data, when dereferenced.
My wording might be incorrect; correctness welcome.
It's your job to check your terminology, as this is not an
English-specific issue. char *pa = (char *)p;
Evil!
Just as evil as making p a pointer to const in the first place. ? You can even use pa in write mode, because the pointed-to data is modifiable. It didn't become read-only simply because you pointed a pointer to const at it.
Far too often, using the type pointer to const means: "I promise not to use this pointer to modify the data" rather than "this pointer points to non-modifiable data". Just as in your example.
Hehe! Abusive typecast users will burn in hell ;-)
Except that there is no abuse here. The abuse was in using a pointer to
const in the first place! ;-)
Here's an example of abusive pointer casting:
const char *s = "Hello";
void const *p = s;
char *pa = (char *)p;
Can you understand the difference?
Try avoiding schematic/religious thinking and you'll become a much
better C programmer.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) writes:
[...] A pointer to void cannot, by definition, point to any data. It is not a data pointer. It merely contains an address with no data type attached. If you want to access any data, you need first to either convert it or to alias it with a pointer to character type.
Or pass it to a function that takes a void* or const void* argument,
such as memcpy().
A pointer declared as
void *x;
can be passed as the first argument of memcpy(); a pointer declared as
const void *y;
cannot.
(The memcpy() function might convert it to char* internally, or it
might not even be implemented in C.)
--
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.
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: Da*****@cern.c h (Dan Pop) writes: [...] A pointer to void cannot, by definition, point to any data. It is not a data pointer. It merely contains an address with no data type attached. If you want to access any data, you need first to either convert it or to alias it with a pointer to character type. Or pass it to a function that takes a void* or const void* argument, such as memcpy().
Which accesses the data itself, but it still doesn't allow you access to
the data.
A pointer declared as void *x; can be passed as the first argument of memcpy(); a pointer declared as const void *y; cannot.
Irrelevant, as the address of to the *existing* data is memcpy's *second*
argument. It is obvious that you cannot use the address of a read only
object as the destination of a memory copy operation, no matter how it
is performed.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) writes: In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:Da*****@cern.c h (Dan Pop) writes: [...] A pointer to void cannot, by definition, point to any data. It is not a data pointer. It merely contains an address with no data type attached. If you want to access any data, you need first to either convert it or to alias it with a pointer to character type. Or pass it to a function that takes a void* or const void* argument, such as memcpy().
Which accesses the data itself, but it still doesn't allow you access to the data.
I don't see how that distinction is relevant. A pointer declared as void *x; can be passed as the first argument of memcpy(); a pointer declared as const void *y; cannot.
Irrelevant, as the address of to the *existing* data is memcpy's *second* argument.
I wasn't talking about memcpy's second argument, I was talking about
its first argument.
It is obvious that you cannot use the address of a read only object as the destination of a memory copy operation, no matter how it is performed.
Of course. Have I implied otherwise?
The point I was making was that the "const" keyword in "const void *y"
is semantically significant. I had previously said that "since it
points to void, what it points to can't be changed anyway"; I later
realized that this isn't entirely correct, since it can be changed by
passing the pointer to memcpy().
--
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.
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: Da*****@cern.c h (Dan Pop) writes: In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: >Da*****@cern.c h (Dan Pop) writes: >[...] >> A pointer to void cannot, by definition, point to any data. It is not >> a data pointer. It merely contains an address with no data type attached. >> If you want to access any data, you need first to either convert it or >> to alias it with a pointer to character type. > >Or pass it to a function that takes a void* or const void* argument, >such as memcpy().
Which accesses the data itself, but it still doesn't allow you access to the data.
I don't see how that distinction is relevant.
Too bad. >A pointer declared as > void *x; >can be passed as the first argument of memcpy(); a pointer declared as > const void *y; >cannot.
Irrelevant, as the address of to the *existing* data is memcpy's *second* argument.
I wasn't talking about memcpy's second argument, I was talking about its first argument.
Yet, memcpy uses its second argument to access the data, so as usual,
you're talking about something irrelevant to the discussion. It is obvious that you cannot use the address of a read only object as the destination of a memory copy operation, no matter how it is performed.
Of course. Have I implied otherwise?
The point I was making was that the "const" keyword in "const void *y" is semantically significant.
Of course. Have I implied otherwise?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de Da*****@cern.ch (Dan Pop) writes: In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:Da*****@cern.c h (Dan Pop) writes: In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:
[...] >A pointer declared as > void *x; >can be passed as the first argument of memcpy(); a pointer declared as > const void *y; >cannot.
Irrelevant, as the address of to the *existing* data is memcpy's *second* argument.
I wasn't talking about memcpy's second argument, I was talking about its first argument.
Yet, memcpy uses its second argument to access the data, so as usual, you're talking about something irrelevant to the discussion.
I'm afraid I've lost track; what discussion is it irrelevant to? It
was entirely relevant to the point I was making (which was to correct
an error I made earlier). If you were having some other discussion,
perhaps you'd care to share it with the rest of us.
--
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.
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: Da*****@cern.c h (Dan Pop) writes: In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: >Da*****@cern.c h (Dan Pop) writes: >> In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes:[...] >> >A pointer declared as >> > void *x; >> >can be passed as the first argument of memcpy(); a pointer declared as >> > const void *y; >> >cannot. >> >> Irrelevant, as the address of to the *existing* data is memcpy's *second* >> argument. > >I wasn't talking about memcpy's second argument, I was talking about >its first argument.
Yet, memcpy uses its second argument to access the data, so as usual, you're talking about something irrelevant to the discussion.
I'm afraid I've lost track; what discussion is it irrelevant to? It was entirely relevant to the point I was making (which was to correct an error I made earlier). If you were having some other discussion, perhaps you'd care to share it with the rest of us.
The discussion was about accessing data through void pointers. IIRC,
you correctly pointed out that void pointers cannot be used for this
purpose. Then, you lost track of the discussion and started talking
about other things.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Virendra Verma |
last post by:
This sounds weird, but I am looking for separate behaviors for
destruction of a const and non-const object.
I am trying to develop a smart/auto pointer class for writing objects
to disk implicitly. The destructor of this class saves the object to
the disk if it is dirty. The problem comes in the following scenario
when a function returns an uncommitted pointer class because same
copies will be committed as two separate objects on disk....
|
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...
|
by: modemer |
last post by:
If I define the following codes:
void f(const MyClass & in) {cout << "f(const)\n";}
void f(MyClass in) {cout<<"f()\n";}
MyClass myclass;
f(myclass);
Compiler complain that it can't find the best match. Anyone could give a
detail explanation in theory? Which one is good?
|
by: herrcho |
last post by:
int intcmp(const void *a, const void *b)
{
return (*(int*)a - *(int*)b);
}
in the above , if i put just 'void' instead of 'const void' as a
parameter,
what's the difference ?
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have type of "const char *".
Right? But why does the compiler I am using allow s to be modified, instead
of generating compile error?
| |
by: Enrico `Trippo' Porreca |
last post by:
Given:
typedef struct Node Node;
struct Node {
void *obj;
Node *next;
};
typedef struct Stack Stack;
struct Stack {
|
by: Geoffrey S. Knauth |
last post by:
It's been a while since I programmed in C++, and the language sure has
changed. Usually I can figure out why something no longer compiles, but
this time I'm stumped. A friend has a problem he hoped I could solve,
and I couldn't. Some code he's using, written in 1999, that compiled
fine in 1999, no longer does in 2006 with g++ 4.
This little bit of code:
SimS::SimS (ostream &s)
{
|
by: hzmonte |
last post by:
Correct me if I am wrong, declaring formal parameters of functions as
const, if they should not be/is not changed, has 2 benefits;
1. It tells the program that calls this function that the parameter
will not be changed - so don't worry.
2. It tells the implementor and the maintainer of this function that
the parameter should not be changed inside the function. And it is for
this reason that some people advocate that it is a good idea to...
|
by: grizggg |
last post by:
I have searched and not found an answer to this question. I ran upon
the following statement in a *.cpp file in a member function:
static const char * const pacz_HTMLContentTypeHeader = "Content-Type:
text/html\r\n";
Why is the second const needed and what does it do?
Thanks
|
by: wellingj |
last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from.
so here is the error I'm getting:
TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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 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...
| |