Saturday, June 20, 2020

Digest for comp.lang.c++@googlegroups.com - 25 updates in 10 topics

Bonita Montero <Bonita.Montero@gmail.com>: Jun 16 08:50PM +0200

> Is there some way to combine all of these methods
> (and the other data types) into a single method ?
 
Try std::variant<...>.
But that's less performant.
Bonita Montero <Bonita.Montero@gmail.com>: Jun 16 09:21PM +0200

This more exactly maps to what you want:
 
#include <variant>
#include <vector>
#include <string>
 
using namespace std;
 
struct ObjPtr
{
// ...
};
 
struct DataItem : public ObjPtr
{
enum
{
INT,
DOUBLE,
VEC_INT,
VEC_STR
};
using var_t = variant<int, double, vector<int>, vector<string>>;
ObjPtr *store( var_t &var );
 
int aAvalueInt;
double aValueDouble;
vector<int> aValueIntArray;
vector<string> aValueStringArray;
};
 
 
ObjPtr *DataItem::store( var_t &var )
{
switch( var.index() )
{
case INT:
aAvalueInt = get<INT>( var );
break;
case DOUBLE:
aValueDouble = get<DOUBLE>( var );
break;
case VEC_INT:
aValueIntArray = get<VEC_INT>( var );
break;
case VEC_STR:
aValueStringArray = get<VEC_STR>( var );
break;
}
return this;
}
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>: Jun 14 12:25AM -0400

Keith Thompson wrote:
> should be updated to state that <errno.h> shall provide a macro
> definition for errno.
 
> (POSIX apparently goes back to 1998.
more like 1988 if you believe to Wikipedia. I recall first learning about it
circa 1993. Back then, any reference to UNIX would elicit a quip "which UNIX?"
from one shrewd friend of mine.
 
-Pavel
I wonder if that wording was
Bo Persson <bo@bo-persson.se>: Jun 16 04:12PM +0200

On 2020-06-16 at 14:26, Manfred wrote:
> Cast operators are explicitly meant to tell the compiler: look, this
> pointer, irrespective of where it comes from, is a pointer to T, deal
> with it.
 
Sorry, no. The compiler has seen all your code and can tell that nowhere
does it create a T object. So how could there be a pointer to T, when
there are no Ts?
 
 
Bo Persson
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jun 16 03:39PM -0700

On 6/16/2020 3:33 PM, Chris M. Thomasson wrote:
>   return p;
> }
 
> Would would one need to cast the return value of malloc to (X*)?
 
Why would one need to cast the return value? void* can do it as is,
right? In pure C...
Scott Newman <scott69@gmail.com>: Jun 17 08:42AM +0200

>> it will work with any compiler that will ever exist.
 
> The problem is that if you trigger UB, the compiler is allowed to
> do whatever it wants with it. Including not doing what you want.
 
Not in this case.
boltar@nowhere.co.uk: Jun 17 03:46PM

On Wed, 17 Jun 2020 17:41:33 +0200
 
>> A static_cast is just a more verbose C cast and the two are interchangable.
 
>No, they are not.
 
>https://en.cppreference.com/w/cpp/language/expressions#Conversions
 
Its close enough as substitute in 99% of circumstances. Though personally
I prefer a C cast, C++ has enough verbiage as it is.
Manfred <noname@add.invalid>: Jun 16 05:53PM +0200

On 6/16/2020 5:29 PM, Paavo Helde wrote:
> trigger implicit object creation."
 
> I gather all this fuss is about allowing type-based alias analysis.
> Reinterpret_cast works directly against this idea.
 
That's why I find the proposal going in the direction of causing more
trouble than help.
True, reinterpret_cast breaks type-based alias analysis. However, it has
been part of the standard since day one, and since you can't do type
analysis on a buffer returned by malloc anyway, what's the problem with
that?
 
By the way, I managed to look into Bjarne's book (p. 1260):
"Note that malloc() etc. does not invoke constructors and free() doesn't
invoke destructors. Do not use these functions for types with
constructors and destructors. Aldo, memset() should never be used for
any type with a constructor".
 
That makes perfectly sense.
I wonder why the standard managed to come up with something that denies
/any/ object allocation with malloc (at least according to the first
example in the proposal).
Paavo Helde <eesnimi@osa.pri.ee>: Jun 15 08:28PM +0300

15.06.2020 20:15 Scott Newman kirjutas:
 
> LOL, only esoteric idiots around here.
> If that would make any difference than the code shown before ...
> On which machine ? Never !
 
We are talking about formal correctness here, the original code would
likely work fine with all currently existing implementations. And I
admit I'm not sure if there is a formal difference at all.
 
I hope you are aware there is a difference between
 
&vector[0] + vector.size()
 
and
 
&vector[vector.size()]
 
(assuming a non-empty vector).
 
The latter form will abort the program with some existing C++
implementations, so it's not only a formal difference.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jun 16 04:11PM -0700

On 6/16/2020 4:05 PM, Chris Vine wrote:
> the struct tag before the various references to X* and there is no
> typedef. The cast of the return value of malloc is not needed in C.
 
> I don't think the example is intended as a guide to writing good C.
 
Touche! So, basically, I need to use placement new and explicit dtor via
calling the dtor ~Type(). Then it becomes a "real object" in C++. I
thought that POD would be different in a sense.
Juha Nieminen <nospam@thanks.invalid>: Jun 15 04:44PM

> header* const h = reinterpret_cast<header*>(std::malloc(sizeof(*h) +
> size));
 
I can't think of any reason why it would have UB.
 
std::malloc() is guaranteed to allocate with an alignment that's
fine for even the largest elementary type (which std::size_t
usually is), so it should just work. (If it didn't, then malloc
couldn't be used to allocate anything at all safely.)
 
I can't think of any reason why "overallocating" like that would
be UB. After all, std::malloc() just takes a byte amount as a
parameter. It doesn't even know what the type you are allocating
it for is.
 
Since you are using a char* to point to the extra space, I can't
think of any reason for UB either (as long as you stay inbounds
of the allocated block, obviously). Even if you used a std::size_t*
(or any type that's an exact fraction of that) it should be fine
as well.
 
The only thing that bothers me about your code is not that it would
be incorrect, just that it's error-prone, as you are using raw
pointers, requiring you to manually free the allocated memory
(although it may well be that you deliberately extracted this code
from its RAII context to simplify it for the sake of example).
 
It's not wrong per se to use raw pointers and having to manually
free() the allocated memory. It's just that it's very error-prone
(especially if you have exceptions enabled, as they could cause
functions to be exited from pretty much anywhere).
 
For very small programs it may be completely fine, though. No need
to over-engineer it if you never intend to use it in a larger program.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jun 13 04:16PM +0100

> because you're completely flippant about the truth. You think
> you're being cute and funny and have no idea what's coming for you.
 
> Such an unnecessary waste.
 
And Satan invented fossils, yes?
 
/Flibble
 
--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin
 
"You won't burn in hell. But be nice anyway." – Ricky Gervais
 
"I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais
 
"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jun 14 11:45PM -0700

> in that module.
 
> What syntax could I create to augment that member with later known
> types? I am considering this syntax:
[...]
 
You want syntactic sugar over all of this? For your new language?
 
Here is something very basic, no sugar:
 
For some reason you are making me think of pascal strings and variable
length arrays.
 
_______________________________
#include <iostream>
#include <cstddef>
#include <cstdlib>
#include <cstring>
 
 
struct header
{
std::size_t m_size;
 
char* get_buf()
{
return reinterpret_cast<char*>(this + sizeof(*this));
}
};
 
 
header* header_alloc(std::size_t size)
{
header* const h = reinterpret_cast<header*>(std::malloc(sizeof(*h) +
size));
 
if (h)
{
h->m_size = size;
}
 
return h;
}
 
 
void header_free(header* h)
{
std::free(h);
}
 
 
int main()
{
{
header* h = header_alloc(123);
 
if (h)
{
char* buf1 = h->get_buf();
 
std::strcpy(buf1, "Hello");
 
std::cout << "buf1 = " << buf1 << "\n";
std::cout << "h->m_size = " << h->m_size << "\n\n";
 
 
char* buf2 = h->get_buf();
 
std::strcat(buf2, " World");
 
std::cout << "buf2 = " << buf2 << "\n";
std::cout << "h->m_size = " << h->m_size << "\n\n";
 
header_free(h);
 
if ((h = header_alloc(42)))
{
std::strcpy(buf1, "FortyTwo");
 
std::cout << "buf1 = " << buf1 << "\n";
std::cout << "h->m_size = " << h->m_size << "\n";
 
header_free(h);
}
}
}
 
return 0;
}
_______________________________
 
 
Help at all?
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jun 13 07:39PM +0100

>>> code.
 
>> C++ isn't a language for easy solutions.
 
> True that! :-)
 
Oh look. Two trolls getting it on.
 
/Flibble
 
--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin
 
"You won't burn in hell. But be nice anyway." – Ricky Gervais
 
"I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais
 
"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."
Juha Nieminen <nospam@thanks.invalid>: Jun 17 06:44AM


> if (cond1 && cond2 && cond3 && cond4) {
> DoSomething();
> }
 
Only possible if there isn't any other code inside those conditionals.
Like:
 
if(!cond1) return;
doSomething();
if(!cond2) return;
doSomethingElse();
if(!cond3) return;
andSomethingOther();
if(!cond4) return;
theFinalThing();
Makis <Makis@gkarvounis.eu>: Jun 16 07:05PM +0200

Am 15.06.2020 um 23:44 schrieb Frederick Gotham:
 
> if ( !cond3 ) return;
 
> if ( !cond4 ) return;
 
> Do Something();
 
Very hard if you have to do some cleanup upon leaving
 
 
> Do Something();
 
> Label_At_End:
> ;
 
No comment!
 
 
> if ( !cond4 ) break;
 
> Do Something();
> }
 
This is a misuse of switch statement
 
 
> Do Something();
> } while (false);
 
> Does anyone else use fake switches and fake loops like this just to exploit the 'break' keyword?
 
I use this a lot. Better than nested ifs. You can do a cleanup at the
end. And... it works for C too!
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jun 15 03:04PM -0700

On 6/15/2020 2:44 PM, Frederick Gotham wrote:
> }
> }
> }
[...]
 
 
Yes. Way back in looking at some adventure games written in BASIC on my
Apple IIgs.
 
Ian Collins <ian-news@hotmail.com>: Jun 16 09:21PM +1200


>> Have you learned how to count to three yet?
 
> If you think a major rebalance would only involved updating 3 nodes then
> you have no idea how balancing works.
 
I thought not.
 
--
Ian.
Juha Nieminen <nospam@thanks.invalid>: Jun 15 04:49PM

> Hmm. So in other words if you have a huge map with millions of entries you
> could suddenly find your program grinds to a halt for a while when you do an
> insert or delete. Thats less than ideal.
 
Not really, since even if your map had a hundred million entries,
the rebalancing would perform operations to about 27 nodes (or perhaps
about double that, as it probably also does something to the other
child node of each of those nodes, so it does something to something
like 54 nodes or so.)
 
Your program isn't going to "grind to a halt" because the rebalancing
does some minor adjustments to about 50 nodes in your 100-million-node
tree.
boltar@nowhere.co.uk: Jun 15 02:31PM

On Mon, 15 Jun 2020 07:00:17 -0400
>> take and is there a way to force it to happen?
 
>This is not specified in the standard, and there are no specified ways to
>force a rebalance.
 
Hmm. So in other words if you have a huge map with millions of entries you
could suddenly find your program grinds to a halt for a while when you do an
insert or delete. Thats less than ideal.
Bonita Montero <Bonita.Montero@gmail.com>: Jun 15 12:52PM +0200

> I'm finding it hard to find out if/how/when the standard map rebalances
> itself after deletions. Does it happen after every erase or only after a
> certain amount of imbalance occurs? ...
 
Try an app which simulates a red-black-tree on the web:
https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
 
I don't like red-black-trees. They've superior performance when
there are as equal inserts / deletes as find-operations, but if
the later operations are more frequent AVL-trees become superior.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jun 16 10:38PM +0100


> You are worth being saved. Call upon Jesus and escape the
> coming tribulation. Make your future sure. Teach you
> family, friends, co-workers, and others in your life.
 
And Satan invented fossils, yes?
 
/Flibble
 
--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin
 
"You won't burn in hell. But be nice anyway." – Ricky Gervais
 
"I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais
 
"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jun 15 01:59PM -0700

On 6/15/2020 8:44 AM, Bonita Montero wrote:
> the volatile methods as well as the compare-exchange-methods that accept
> only one memory-consistency-parameter.
> Here it is:
[...]
 
Actually, if the underlying system supports lock free DWCAS then C++
"should" support it on said system.
 
https://groups.google.com/d/msg/lock-free/X3fuuXknQF0/Ho0H1iJgmrQJ
 
If not, then you need to go rouge. Take careful note of the
memory_order_acq_rel membar.
 
An atomic CAS with acquire semantics, the membar would go _after_ the CAS.
 
An atomic CAS with release semantics, the membar would go _before_ the CAS.
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Jun 15 12:58AM +0100

Apparently you shouldn't prick your sausages. I've been doing it wrong all these years.
 
/Flibble
 
--
"Snakes didn't evolve, instead talking snakes with legs changed into snakes." - Rick C. Hodgin
 
"You won't burn in hell. But be nice anyway." – Ricky Gervais
 
"I see Atheists are fighting and killing each other again, over who doesn't believe in any God the most. Oh, no..wait.. that never happens." – Ricky Gervais
 
"Suppose it's all true, and you walk up to the pearly gates, and are confronted by God," Byrne asked on his show The Meaning of Life. "What will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a world that is so full of injustice and pain. That's what I would say."
ram@zedat.fu-berlin.de (Stefan Ram): Jun 15 09:58PM

> }
> }
>}
 
int main3() { if( cond4 )DoSomething(); }
int main2() { if( cond3 )main3(); }
int main1() { if( cond2 )main2(); }
int main( ) { if( cond1 )main1(); }
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: