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 ?
i can't get the meaning of const when used in parameter..
Thanks in advance ~ 13 2330
"herrcho" <he*********@ko rnet.net> wrote: 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 ?
Nothing, since you're not trying to change anything anyway.
i can't get the meaning of const when used in parameter..
If a parameter is declared const, the function cannot change it. This
sounds like it's no change, since a function cannot change a the value
of a parameter in the caller anyway. However, if the parameter is const,
the function cannot even change its value _within the function_.
If a parameter is a pointer to a const type, the function cannot change
the object the pointer points at.
If you don't modify anything in the function to begin with, this doesn't
matter, of course. However, the above looks like a comparison function
for qsort() (or bsearch()). In that case, the consts are there to
prevent you from changing the objects behind qsort()'s back, since doing
so could mess up the sorting algorithm.
Richard
"herrcho" <he*********@ko rnet.net> wrote: 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 ?
If you're intending to pass this function to qsort or something
that expects a parameter of type int(*)(const void*,const void*)
then you must not change from const void to void, this will be
a constraint violation (wrong type of argument) or else undefined
behaviour (if you say cast it back to the right type).
i can't get the meaning of const when used in parameter..
When used as 'const type *a' it means that you are not allowed to
modify the thing that the pointer is pointing to. Unfortunately
due to the bad way you wrote the function this protection is lost
when you cast the argument to (int*). You can in fact write any
qsort compare function in a completely typesafe manner with no
casts, which is much cleaner code:
int intcmp(const void *va, const void *vb)
{
const int *a = va, *b = vb;
return *a - *b;
}
That way you will get a diagnostic message from the compiler if
you accidentally attempt to modify the contents of the memory.
--
Simon.
Simon Biber wrote:
.... snip ... When used as 'const type *a' it means that you are not allowed to modify the thing that the pointer is pointing to. Unfortunately due to the bad way you wrote the function this protection is lost when you cast the argument to (int*). You can in fact write any qsort compare function in a completely typesafe manner with no casts, which is much cleaner code:
int intcmp(const void *va, const void *vb) { const int *a = va, *b = vb; return *a - *b; }
That way you will get a diagnostic message from the compiler if you accidentally attempt to modify the contents of the memory.
Totally agree, EXCEPT for the actual comparison in your example,
which is liable to problems from integer overflow. This is a good
place for:
return (*a > *b) - (*a < *b);
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
"CBFalconer " <cb********@yah oo.com> wrote: Simon Biber wrote: int intcmp(const void *va, const void *vb) { const int *a = va, *b = vb; return *a - *b; }
That way you will get a diagnostic message from the compiler if you accidentally attempt to modify the contents of the memory.
Totally agree, EXCEPT for the actual comparison in your example, which is liable to problems from integer overflow. This is a good place for:
return (*a > *b) - (*a < *b);
Yes, my mistake. In fact I usually write it like the equivalent:
return (*a < *b) ? -1 : (*a > *b);
But I prefer your subtraction form, because in the rather arbitrary
and unjustified model of code microefficiency in my head, a subtract
is faster than a compare. This may or may not be true on any of the
platforms I compile for, I wouldn't know.
--
Simon.
"Simon Biber" <ne**@ralminNOS PAM.cc> wrote in message
news:3f******** *************** @news.optusnet. com.au... You can in fact write any qsort compare function in a completely typesafe manner
How? The function itself can never guarantee that it won't be applied to a
type that differs from the intended one.
with no casts, which is much cleaner code:
Cleaner perhaps, but an absense of casts is not typesafety.
--
Peter
Simon Biber wrote: "CBFalconer " <cb********@yah oo.com> wrote: Simon Biber wrote: int intcmp(const void *va, const void *vb) { const int *a = va, *b = vb; return *a - *b; }
That way you will get a diagnostic message from the compiler if you accidentally attempt to modify the contents of the memory.
Totally agree, EXCEPT for the actual comparison in your example, which is liable to problems from integer overflow. This is a good place for:
return (*a > *b) - (*a < *b);
Yes, my mistake. In fact I usually write it like the equivalent: return (*a < *b) ? -1 : (*a > *b);
But I prefer your subtraction form, because in the rather arbitrary and unjustified model of code microefficiency in my head, a subtract is faster than a compare. This may or may not be true on any of the platforms I compile for, I wouldn't know.
I see two unconditional comparisons and a subtraction
in the first,
but only two comparisons, with one of them being conditional,
in the second.
--
pete
pete wrote: Simon Biber wrote: "CBFalconer " <cb********@yah oo.com> wrote: Simon Biber wrote:
> int intcmp(const void *va, const void *vb) > { > const int *a = va, *b = vb; > return *a - *b; > } > > That way you will get a diagnostic message from the compiler if > you accidentally attempt to modify the contents of the memory.
Totally agree, EXCEPT for the actual comparison in your example, which is liable to problems from integer overflow. This is a good place for:
return (*a > *b) - (*a < *b);
Yes, my mistake. In fact I usually write it like the equivalent: return (*a < *b) ? -1 : (*a > *b);
But I prefer your subtraction form, because in the rather arbitrary and unjustified model of code microefficiency in my head, a subtract is faster than a compare. This may or may not be true on any of the platforms I compile for, I wouldn't know.
I see two unconditional comparisons and a subtraction in the first, but only two comparisons, with one of them being conditional, in the second.
However the emitted code will often not have any jumps in it (for
the first), which avoids flushing any instruction queues. I
learned it from someone, and consider it a trick worth knowing.
The point is that they are both safe, and have a chance to
simultaneously be efficient and clear. However there may be
considerable code involved in converting a machine comparison into
the 0 or 1 representation, in which case the ?: coding will be
superior.
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
CBFalconer wrote: pete wrote: Simon Biber wrote: "CBFalconer " <cb********@yah oo.com> wrote: > Simon Biber wrote:
> > int intcmp(const void *va, const void *vb) > > { > > const int *a = va, *b = vb; > > return *a - *b; > > } > > > > That way you will get a diagnostic message from the compiler if > > you accidentally attempt to modify the contents of the memory. > > Totally agree, EXCEPT for the actual comparison in your example, > which is liable to problems from integer overflow. This is a good > place for: > > return (*a > *b) - (*a < *b);
Yes, my mistake. In fact I usually write it like the equivalent: return (*a < *b) ? -1 : (*a > *b);
But I prefer your subtraction form, because in the rather arbitrary and unjustified model of code microefficiency in my head, a subtract is faster than a compare. This may or may not be true on any of the platforms I compile for, I wouldn't know.
I see two unconditional comparisons and a subtraction in the first, but only two comparisons, with one of them being conditional, in the second.
However the emitted code will often not have any jumps in it (for the first), which avoids flushing any instruction queues. I learned it from someone, and consider it a trick worth knowing.
The point is that they are both safe, and have a chance to simultaneously be efficient and clear. However there may be considerable code involved in converting a machine comparison into the 0 or 1 representation, in which case the ?: coding will be superior.
Thank you.
--
pete
"Simon Biber" <ne**@ralminNOS PAM.cc> wrote in message news:<3f******* *************** *@news.optusnet .com.au>... "herrcho" <he*********@ko rnet.net> wrote: 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 ? If you're intending to pass this function to qsort or something that expects a parameter of type int(*)(const void*,const void*) then you must not change from const void to void, this will be a constraint violation (wrong type of argument) or else undefined behaviour (if you say cast it back to the right type).
i can't get the meaning of const when used in parameter..
When used as 'const type *a' it means that you are not allowed to modify the thing that the pointer is pointing to. Unfortunately due to the bad way you wrote the function this protection is lost when you cast the argument to (int*). You can in fact write any qsort compare function in a completely typesafe manner with no casts, which is much cleaner code:
int intcmp(const void *va, const void *vb) { const int *a = va, *b = vb; return *a - *b; }
Friends,
Don't we have to cast a void* so that the void points to a valid
value (as per 6.3.2.3 of the standard). (Correct me if I am wrong):
int fun(const void *a, const void *b)
{
unsigned int *p , *q;
p =(unsigned int*) a, q =(unsigned int*) b;
/*return (*p > *q ) - (*p < *q); */
return *p - *q;
}
int main(void)
{
int a=2,b=2,c=8,d=6 ;
printf("%d %d\n",fun(&a,&b ),fun(&c,&d));
return 0;
}
OUTPUT:
0 2
And for the second one:
int fun(const void *a, const void *b)
{
unsigned int *p , *q;
p =(unsigned int*) a, q =(unsigned int*) b;
return (*p > *q ) - (*p < *q);
/*return *p - *q; */
}
int main(void)
{
int a=2,b=2,c=8,d=6 ;
printf("%d %d\n",fun(&a,&b ),fun(&c,&d));
return 0;
}
OUTPUT:
0 1
Is there any UB?
That way you will get a diagnostic message from the compiler if you accidentally attempt to modify the contents of the memory. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bolin |
last post by:
Hi all,
A question about smart pointers of constant objects. The problem is to
convert from Ptr<T> to Ptr<const T>. I have look up and seen some
answers to this question, but I guess I am too stupid to understand
and make them work.
E.g. I have read that boost's smart pointers are able to do this
convertion, but the following code doesn't compile (VC++6.0):
|
by: Pavel |
last post by:
I am writing software for an embedded application and here is the
question.
GCC would emit data declared like
const char text = "abc";
to .rodata (i.e. "read only data") section. I can put this section to
flash memory and that would be OK.
I have a structure with one member of it being
const char** array;
|
by: x-pander |
last post by:
given the code:
<file: c.c>
typedef int quad_t;
void w0(int *r, const quad_t *p)
{
*r = (*p);
}
|
by: bob_jenkins |
last post by:
{
const void *p;
(void)memset((void *)p, ' ', (size_t)10);
}
Should this call to memset() be legal? Memset is of type
void *memset(void *, unsigned char, size_t)
Also, (void *) is the generic pointer type.
My real question is, is (void *) such a generic pointer type that it
|
by: vashwath |
last post by:
Hi all,
Recently I attended an interview in which the question "Is there any
difference between "const T var" and "T const var"? was asked.I
answered "NO"(I guessed it:-( ).Did I answered correctly?Please shed
some light on this.I searched the FAQ, but in vain.
Thanks
| |
by: Michael Safyan |
last post by:
Dear members of comp.lang.c++,
I am a little bit confused about the differences between constant
references and values. I understand that it is faster to use a constant
reference ("const T&") than a value ("T") since the former does not
require copying whereas the latter does, is that correct? Also, I un
derstand that "const T&" allows for polymorphism whereas "T" will
generate code cuttting.
On the former points, I am fairly confident......
|
by: junky_fellow |
last post by:
Hi guys,
Consider the following statements.
const int *ptr; /* ie the contents pointed to by ptr cannot be
changed */
My question is how/who prevents the contents from being modified ?
Is it the C compiler that would give compile time error while trying to
change the contents ? Or is it the implementation that would somehow
|
by: Tim H |
last post by:
I understand the semantics of why this works the way it does. But I
wonder if there's a reason for the behaviore at the line marked
"QUESTION". I figured if there is an answer, someone here knows it.
Specifically, part 1 is obvious to most anyone. Part 2 is isomorphic
to part 1, yet behaves differently. If a shared_ptr is const, should
it really allow non-const dereferences?
Thanks,
|
by: Stephen Howe |
last post by:
Hi
Just going over some grey areas in my knowledge in C++:
1) If I have
const int SomeConst = 1;
in a header file, it is global, and it is included in multiple translations
units, but it is unused, I know that it does not take up storage in the
|
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: 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,...
| |
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: 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();...
|
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: 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...
| |