From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 114487 invoked by alias); 27 Sep 2016 17:49:49 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 114390 invoked by uid 89); 27 Sep 2016 17:49:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.6 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=dee, doo, mistook, UD:Test.c X-HELO: mail-yw0-f179.google.com Received: from mail-yw0-f179.google.com (HELO mail-yw0-f179.google.com) (209.85.161.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 27 Sep 2016 17:49:46 +0000 Received: by mail-yw0-f179.google.com with SMTP id i129so13285268ywb.0 for ; Tue, 27 Sep 2016 10:49:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=IITSXbnq7piRu+CkwdJMjHuGGMUT7S/bEQMs8smrbRQ=; b=j078tVHGd8Q4p1Gvn1TeBq+g1YZOnXi+YZFIiQoodNc0ih6VehRAuCQpvW63H7EAL9 RAnm6uDNU1GjcabHdmuRMfmzlNm6mKRbGbHaBfDHi23WjhbjjjAOBeUbYMKfS1KfjxZS CsVwg2BAbBgtN9xz0/KfJzAAqhb9wPvdLMsLx0vcE2jRG4BSZaylCdAd3hwjDp/srnFM UjyDQhVzWrjNKqNgYNG42uP7G7hcohSsCglgCAV1BUSToA6UFQcJLgdctt14dl3cXqzC vY2pAlvCMs1euwF92Mn8l1tdMwfFVQiIJlNzRwrmsgviNsKNJvmyLaYfgUdtuyVOdxKF Mrwg== X-Gm-Message-State: AA6/9RlP0FF0n+0bsAqLpP0t42rJmTvFnOHfHQZR5teK4KjZx3JQLiRxoL3AfGnBErmTiq6ylO/Pfs8M+2Gdlg== X-Received: by 10.129.163.7 with SMTP id a7mr1424710ywh.174.1474998585221; Tue, 27 Sep 2016 10:49:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.37.252.34 with HTTP; Tue, 27 Sep 2016 10:49:44 -0700 (PDT) In-Reply-To: <4e4b52d1-d9fd-ef2b-6e53-99f4b68a9c4b@gmail.com> References: <87zimt5pza.fsf@mid.deneb.enyo.de> <4e4b52d1-d9fd-ef2b-6e53-99f4b68a9c4b@gmail.com> From: Jonathan Wakely Date: Tue, 27 Sep 2016 17:49:00 -0000 Message-ID: Subject: Re: Optimization question To: Nikolaus Dunn Cc: gcc-help Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2016-09/txt/msg00108.txt.bz2 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 > #include > #include > > 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.