Tuesday, November 22, 2016

Digest for comp.lang.c++@googlegroups.com - 17 updates in 5 topics

Juha Nieminen <nospam@thanks.invalid>: Nov 22 09:36AM

> code in question is not conformant. That may be what you had in mind.
> But where the static member is not ODR-used, you should be safe from
> that problem.
 
Is this similar to what may happen if you make an extern function/method
implementation inline? The optimizer may optimize it away, causing a
linker error if it's being called from somewhere else than the
compilation unit where it's implemented. (Or at least I remember some
version of gcc doing that.)
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Nov 22 11:12PM

On Tue, 22 Nov 2016 09:36:06 +0000 (UTC)
> away, causing a linker error if it's being called from somewhere else
> than the compilation unit where it's implemented. (Or at least I
> remember some version of gcc doing that.)
 
I'm not really sure about that one. In C, inline functions have
internal linkage by default, which seems logical. In C++ they have
external linkage by default, which on the face of it seems illogical
but I am sure there is a reason for it. If you declare a C++ inline
function extern I should have thought it would have no effect at all,
since functions don't have storage - they just have linkage/visibility.
So the extern declaration seems redundant.
 
Chris
"Christopher J. Pisz" <cpisz@austin.rr.com>: Nov 22 03:06PM -0600

So, I got asked on an interview today if I am have ever done a custom
memory manager and if I am familiar with placement new.
 
Nope. I think I am accustomed to more high level programming then what
they are looking for. None the less, I'd like to learn about the concept.
 
Even after Googling placement new, I cannot fathom why I would use it.
Perhaps I have some old legacy hardware that does not actually have any
kind of programming interface, and the only way to communicate is to
plug things in at a specific location? Does that even happen these days?
 
What kind of scenarios are there with modern hardware and software where
you are using this kind of thing?
 
Also, why would one need a custom memory manager and what kinds of
custom things would their manager provide? I understand that a person
might have a limited amount of memory, but I don't see how taking over
what new and delete normally do is going to save you. Maybe you have to
only use specific locations, but similar to the previous question, why
would that happen?
Mark Blair <mblair@quantumdata.com>: Nov 22 03:13PM -0600

On 11/22/2016 3:06 PM, Christopher J. Pisz wrote:
> plug things in at a specific location? Does that even happen these days?
 
> What kind of scenarios are there with modern hardware and software where
> you are using this kind of thing?
 
It's very useful when used with shared memory.
 
"Chris M. Thomasson" <invalid@invalid.invalid>: Nov 22 01:30PM -0800

On 11/22/2016 1:06 PM, Christopher J. Pisz wrote:
> what new and delete normally do is going to save you. Maybe you have to
> only use specific locations, but similar to the previous question, why
> would that happen?
 
Lock-free allocators are nice because they tend to outperform
malloc/free under load wrt multiple threads and/or processes
concurrently allocating and freeing blocks of memory. The Intel TBB is
pretty nice. Placement new is great for being able to integrate with
custom memory management solutions. Shared memory is a very nice example
where placement new comes into play.
"Christopher J. Pisz" <cpisz@austin.rr.com>: Nov 22 03:35PM -0600

On 11/22/2016 3:13 PM, Mark Blair wrote:
>> what new and delete normally do is going to save you. Maybe you have to
>> only use specific locations, but similar to the previous question, why
>> would that happen?
 
What does one use shared memory for? Faster IPC? What are some examples?
where you've used it?
 
From what I am reading, there is no shared memory per-say on Windows.
Only virtual addresses that map to regions of a file. If that is
correct, perhaps, this is the reason I have not come across this, being
a Windows guy.
Mark Blair <mblair@quantumdata.com>: Nov 22 03:39PM -0600

On 11/22/2016 3:35 PM, Christopher J. Pisz wrote:
>>> would that happen?
 
> What does one use shared memory for? Faster IPC? What are some examples?
> where you've used it?
 
On Linux, between separate processes that share structured data. In one
case we have a Java GUI that uses JNI to interface to data populated
from a C++ daemon.
 
> From what I am reading, there is no shared memory per-say on Windows.
 
There *is* shared memory on Windows, although it's been a while since
I've used it.
 
> Only virtual addresses that map to regions of a file. If that is
> correct, perhaps, this is the reason I have not come across this, being
> a Windows guy.
 
Most every OS uses virtual addresses, even for shared memory.
 
--
Mark
"Chris M. Thomasson" <invalid@invalid.invalid>: Nov 22 01:41PM -0800

On 11/22/2016 1:35 PM, Christopher J. Pisz wrote:
> Only virtual addresses that map to regions of a file. If that is
> correct, perhaps, this is the reason I have not come across this, being
> a Windows guy.
 
Take a look at memory mapped files:
 
https://msdn.microsoft.com/en-us/library/ms810613.aspx
 
That is from 1993.
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Nov 22 02:52PM -0700

On Tue, 22 Nov 2016 15:06:33 -0600, "Christopher J. Pisz"
>what new and delete normally do is going to save you. Maybe you have to
>only use specific locations, but similar to the previous question, why
>would that happen?
 
"Placement new" decouples memory allocation from invocation of the
constructor. If you already have memory that you want to use for an
object, whether it's static or automatic or allocated by something
other than malloc(), you can use placement new to run the object's
constructor with calling malloc().
 
As Chris said, there are alternatives to malloc() and free(), which
are general-purpose routines. When you're allocating and deallocating
memory for multiple objects of the same type (and therefore of the
same size), it sometimes makes sense to preallocate memory for a bunch
of contiguous objects and then keep track of which ones are in use.
When it's time to initialize a new object of that type, pick an unused
object and call placement new to call the constructor with the
object's address.
 
This page looks useful:
 
https://isocpp.org/wiki/faq/dtors#placement-new
 
Louis
Paavo Helde <myfirstname@osa.pri.ee>: Nov 23 12:24AM +0200

On 22.11.2016 23:06, Christopher J. Pisz wrote:
> plug things in at a specific location? Does that even happen these days?
 
> What kind of scenarios are there with modern hardware and software where
> you are using this kind of thing?
 
STL implementations make use of placement new quite a lot, in places
like std::vector or std::make_shared. How do you construct objects
inside the contiguous buffer of a std::vector? Placement new is the only
option for non-POD-s. Whenever you implement something like that itself,
placement new will be needed.
 
In general, any time when you allocate and release many items together,
then it is more efficient to allocate a single large block and place the
objects inside it. Technically this could appear either as using
placement new or using a custom pooled allocator, depending on exact
implementation. By deep parsing large XML files for example such
techniques might give 100x speedup and 10x better memory usage, compared
to dynamically allocating each XML node individually.
 
 
> what new and delete normally do is going to save you. Maybe you have to
> only use specific locations, but similar to the previous question, why
> would that happen?
 
One might want to use a custom memory manager for debugging leaks or
other bugs, for example. One might also want to use a faster
multithreaded memory manager like Intel TBB instead of your standard
one, this may make a world of difference in heavily multithreaded programs.
 
Cheers
Paavo
Paavo Helde <myfirstname@osa.pri.ee>: Nov 23 12:32AM +0200

On 22.11.2016 23:35, Christopher J. Pisz wrote:
> From what I am reading, there is no shared memory per-say on Windows.
> Only virtual addresses that map to regions of a file.
 
Technically yes, but this file can be the pagefile, meaning basically
that it is just a region of memory.
woodbrian77@gmail.com: Nov 22 10:38AM -0800

On Tuesday, November 22, 2016 at 6:25:22 AM UTC-6, David Brown wrote:
> things like "Please step back for the car". Of course, the car was
> covered in dents and scratches made by students who wanted to hear what
> it would say next!
 
 
 
Perhaps you would benefit from reading about the war on
science:
 
http://www.city-journal.org/html/real-war-science-14782.html
 
"The Left saw scientists as the new high priests, offering them
prestige, money, and power. The power too often corrupted. Over
and over, scientists yielded to the temptation to exaggerate
their expertise and moral authority, sometimes for horrendous
purposes."
 
"the modern Left has become obsessed with silencing heretics. "
 
David and others are obsessed with silencing voices that
stand up to them. They accuse me of "fussing," but I'm not
the one trying to hide the elephant in the room -- the C++
Middleware Writer.
 
 
Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Nov 22 09:01PM

> stand up to them. They accuse me of "fussing," but I'm not
> the one trying to hide the elephant in the room -- the C++
> Middleware Writer.
 
Elephant in the room? Have you stopped taking your medication again Brian?
 
Sausages.
 
/Flibble
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Nov 22 10:41AM -0700

On Mon, 21 Nov 2016 07:58:32 +0000 (UTC), Juha Nieminen
 
>'tmp' is a pointer to something. 'tmp2' is another pointer of the same type.
>It's assigned the pointer 'tmp'. Thus it will just point to the same object.
>Only one instance of 'parent' is being created here (using 'new').
 
It took me a while to figure out what you're getting at.
 
You're finding disagreement where there is none. "tmp2 is a copy of
tmp" means "tmp2 has the same value as tmp." Nobody said anything
about copies of "parent"; there's only one.
 
Louis
woodbrian77@gmail.com: Nov 22 12:03PM -0800

On Tuesday, November 22, 2016 at 7:58:29 AM UTC-6, Alf P. Steinbach wrote:
 
Alf, please don't swear here.
 
Have you seen this one?
http://www.city-journal.org/html/real-war-science-14782.html
 
 
Brian
Ebenezer Enterprises
http://webEbenezer.net
red floyd <dont.bother@its.invalid>: Nov 22 01:01PM -0800

> On Tuesday, November 22, 2016 at 7:58:29 AM UTC-6, Alf P. Steinbach wrote:
 
> Alf, please don't swear here.
 
Fuck off. Haven't you figured it out yet?
Gareth Owen <gwowen@gmail.com>: Nov 22 07:26PM


> It does, for slightly different reasons. But it's easy to remedy
> that problem:
 
> if(str1.length() < str2.length()+12) { ... }
 
Well, yes. But it'd be nice if those two thing were functionally the
same. (At least for values of .length() that commonly occur in programs,
i.e. less than SIZE_MAX / 2)
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: