[MinGW-Notify] [mingw] #40686: std::string custom operator new/delete function

アーカイブの一覧に戻る
MinGW Notification List mingw****@lists*****
Wed Sep 2 22:10:39 JST 2020


#40686: std::string custom operator new/delete function

  Open Date: 2020-08-30 10:32
Last Update: 2020-09-02 14:10

URL for this Ticket:
    https://osdn.net//projects/mingw/ticket/40686
RSS feed for this Ticket:
    https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=40686

---------------------------------------------------------------------

Last Changes/Comment on this Ticket:
2020-09-02 14:10 Updated by: keith
 * Resolution Update from None to Works For Me

 * Severity Update from 5 - Medium to 1 - Lowest

 * Priority Update from 5 - Medium to 1 - Lowest


Comment:

This is an issue which really would have been better to raise on the mailing
list, where it would be visible to a broader spectrum of experienced users. My
own expertise is predominantly in FORTRAN-77 and C programming; my knowledge of
C++ is sketchy, at best. Nonetheless, I will try to answer, to the best of my
ability.

Firstly, let me make it perfectly clear: any comparison between behaviour on
Windows, and behaviour on Linux, is completely irrelevant; the two platforms
are fundamentally different, and there are many pitfalls in migrating from
either one to the other. In this case, I believe you are falling foul of one of
those pitfalls; there is no bug here, beyond a gap in your understanding.

In your test case, while you define replacements for both new and delete, I see
no explicit use of either; any use which does arise is implicit in the
instantiation, and subsequent destruction, of the std::string object, to which
you refer. Instantiation of that object, apparently, does invoke new, and
destruction thus invokes delete, but consider this: where is the implementation
of that std::string class object defined? I'll give you some time to think
about that, then I'll suggest, in follow-up comments, how the answer may
explain the behaviour you have observed.



---------------------------------------------------------------------
Ticket Status:

      Reporter: marvol
         Owner: (None)
          Type: Issues
        Status: Open
      Priority: 1 - Lowest
     MileStone: (None)
     Component: GCC
      Severity: 1 - Lowest
    Resolution: Works For Me
---------------------------------------------------------------------

Ticket details:

Hello,

I am writing a custom wrapper for new/delete allocations to get an aligned
memory. I noticed that if I use std::string, operator new/delete are not called
as it is in a linux version of g++ (operators new/delete are called). With the
default optimization level, operator new/delete is not called and with
optimization levels O1, O2, O3 only operator delete is called.

Source:

 1.  #include <string>
 2.  #include <cstdio>
 3.  #include <cstdlib>
 4.  
 5.  void* operator new(size_t size)
 6.  {
 7.          printf("Calling new: %zu", size);
 8.          void* address = malloc(size);
 9.  
10.          printf(" address: 0x%p\n", address);
11.          return address;
12.  }
13.  
14.  void* operator new[](size_t size)
15.  {
16.          printf("Calling new[]: %zu", size);
17.          void* address = malloc(size);
18.  
19.          printf(" address: 0x%p\n", address);
20.          return address;
21.  }
22.  
23.  void operator delete(void* address)
24.  {
25.          printf("Calling delete, address: 0x%p\n", address);
26.          free(address);
27.  }
28.  
29.  void operator delete[](void* address)
30.  {
31.          printf("Calling delete[], address: 0x%p\n", address);
32.          free(address);
33.  }
34.  
35.  struct Obj
36.  {
37.          std::string m_str;
38.  };
39.  
40.  void test()
41.  {
42.          Obj obj;
43.          obj.m_str = "long string ......................";
44.  }
45.  
46.  int main()
47.  {
48.          test();
49.  
50.          return 0;
51.  }

Compiling with the default optimization level:

  • g++ main.cpp -std=c++11 -o test.exe

Output:

  • NONE

Compiling with the O2 optimization level:

  • g++ main.cpp -std=c++11 -O2 -o test.exe

Output:

  • Calling delete, address: 0x00542098

I am using:

  • OS: Windows 7
  • Mingw: 5.4.1
  • Shell: cmd MINGW32_NT-6.1 MARTIN-LENOVO 1.0.18(0.48/3/2) 2012-11-21 22:34
    i686 Msys
  • Uname -a:
  • G++: g++ (MinGW.org GCC Build-2) 9.2.0
  • GCC: gcc (MinGW.org GCC Build-2) 9.2.0
  • ld: GNU ld (GNU Binutils) 2.32



-- 
Ticket information of MinGW - Minimalist GNU for Windows project
MinGW - Minimalist GNU for Windows Project is hosted on OSDN

Project URL: https://osdn.net/projects/mingw/
OSDN: https://osdn.net

URL for this Ticket:
    https://osdn.net/projects/mingw/ticket/40686
RSS feed for this Ticket:
    https://osdn.net/ticket/ticket_rss.php?group_id=3917&tid=40686



More information about the MinGW-Notify mailing list
アーカイブの一覧に戻る