public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Nikolaus Dunn <nikdunn1979@gmail.com>
Cc: gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Optimization question
Date: Tue, 27 Sep 2016 17:49:00 -0000	[thread overview]
Message-ID: <CAH6eHdTV7avw9GTZSBPAmOV3Zc6o6ji-14TnzV2V28Hg4NGvBg@mail.gmail.com> (raw)
In-Reply-To: <4e4b52d1-d9fd-ef2b-6e53-99f4b68a9c4b@gmail.com>

On 27 September 2016 at 18:39, Nikolaus Dunn wrote:
> Maybe I'm using the wrong terminology then or am possibly confused. I am
> under the impression that std::allocator points to std::new_allocator by
> default. In fact, in debugging with gdb, I've gotten into new_allocator.h.
> At any rate, in trying to contrive the smallest possible test program, I
> learned some new things that in my original pass, I mistook.

Sorry, I completely misread your original mail, I thought you meant
new and delete operators for specific classes, not the global ones.

You're right that std::allocator uses the global new and delete operators.

> It turns out that std::vector seems to work fine with -O2 or without it.
>
> std::ostringstream however does not call my new OR delete with no
> optimization. With -O2, it calls only my delete.
>
> If I do not attempt to wrap malloc and free, I get the same result.
> std::vector calls my new and delete, ostringstream calls neither.

I can't reproduce this, I see stringstream calling both.


Creating new vector

pushing an element

new: 4 bytes
wrap_malloc: 4 bytes
wrap_malloc address: 0x1528c20
new address: 0x1528c20
Creating new ostringstream

pushing once

new: 513 bytes
wrap_malloc: 513 bytes
wrap_malloc address: 0x1528c40
new address: 0x1528c40
pushing twice

printing it out

new: 28 bytes
wrap_malloc: 28 bytes
wrap_malloc address: 0x1528e50
new address: 0x1528e50
Blah blah blah boo dee doo
delete: 0x1528e50
wrap_free: 0x1528e50
delete: 0x1528c40
wrap_free: 0x1528c40
delete: 0x1528c20
wrap_free: 0x1528c20



> The command line I used to compile it is:
> g++ -g -O2 --std=c++14 -Wl,-wrap,malloc -Wl,-wrap,free Test.c -o test.exe
>
> Test.c:
>
> #include <sstream>
> #include <iostream>
> #include <vector>
>
> extern "C" void *__real_malloc(size_t);
> extern "C" void __real_free(void *);
>
> extern "C" void *__wrap_malloc(size_t nbytes) {
>     printf("wrap_malloc: %ld bytes\n", nbytes);
>     void * foo = __real_malloc(nbytes);
>     printf("wrap_malloc address: %ld\n", foo);

N.B. compiling your code produces loads of warnings, the correct
format specifiers for printf are %zu and %p.

  reply	other threads:[~2016-09-27 17:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <c8204c33-7085-5dde-96f3-92d7745a0018@gmail.com>
2016-09-27 15:20 ` Florian Weimer
2016-09-27 17:10   ` Jonathan Wakely
2016-09-27 17:39     ` Nikolaus Dunn
2016-09-27 17:49       ` Jonathan Wakely [this message]
2016-09-27 18:32       ` Florian Weimer
2016-09-28 10:13         ` Jonathan Wakely
2000-01-07 11:39 optimization question Igor Schein
2000-04-01  0:00 ` Igor Schein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAH6eHdTV7avw9GTZSBPAmOV3Zc6o6ji-14TnzV2V28Hg4NGvBg@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=nikdunn1979@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).