Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:51:17+00:00 2026-06-15T06:51:17+00:00

I have a mixed C/C++ program. It contains a flex/bison parser which targets C,

  • 0

I have a mixed C/C++ program. It contains a flex/bison parser which targets C, while the remainder is C++.

Being C, the generated parser and scanner manage their memory with malloc, realloc and free. They are good enough to expose hooks allowing me to submit my own implementations of these functions. As you might expect, the rest of the (C++) program “wants” to use new, delete, etc.

Doing a little research seems to show that the relevant standards do not guarantee that such mixing should work. Particularly the C “heap” is not necessarily the C++ “free area”. It seems the two schemes can trample each other.

On top of this, someday (soon) this program will probably want to integrate a customized heap implementation such as tcmalloc, used by both C and C++.

What is the “right” thing to do here?

Given the desire to integrate tcmalloc (which explains how to link with C programs) I’m tempted to find some cross-type, cross-thread, cross-everything overload/hook/whatever into C++ memory management. With that I could point all C++ allocation/release calls back to their C equivalents (which in turn land on tcmalloc.)

Does such a pan-galactic global C++ hook exist? Might it already be doing what I want, similar to how ios_base::sync_with_stdio secretly marries iostream and stdio by default?

I am not interested in talking about stdio vs. iostreams, nor about switching parser generators nor using the C++ flex/bison skeletons (they introduce independent headaches.)

EDIT: Please include the names of those sections of the C++ standard that support your answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:51:18+00:00Added an answer on June 15, 2026 at 6:51 am

    The standard does guarantee that mixing the two allocation variants will work. What it doesn’t permit is things like calling free on memory that came from new, since they may use a totally different arena for the two types.

    Providing you remember to call the correct deallocation function for a given block of memory, you will be fine. They won’t trample each other if you follow the rules and, if you don’t follow the rules then, technically, you’re doing the trampling, not them 🙂


    The controlling part of the C++11 standard is 20.6.13 C library which states, paraphrased:

    • The functions calloc, malloc, free and realloc are provided, based on the C standard.
    • The functions do not use ::operator new() or ::operator delete().
    • This allows the heritage C stuff to use a different memory arena then the normal C++ memory allocation.

    That second bullet point is interesting in light of what you’re eventually proposing, dropping in tcmalloc to replace the C heritage functions and have C++ use it as well.

    There’s a footnote in the standard which explains why they don’t use let malloc() call ::operator new():

    The intent is to have operator new() implementable by calling std::malloc() or std::calloc(). In other words, they want to avoid a circular dependency.

    However, while it allows operator new() to call malloc(), I’m not sure that the standard actually requires it. So, to be safe, you’d probably want to inject tcmalloc into both the C and C++ areas.

    You’ve indicated you already know how to do that for C. For C++, it can be done by simply providing the entire set of global operator new()/delete() functions in your code, suitably written to call tcmalloc under the covers. The C++ standard states in 3.7.4 Dynamic storage duration:

    The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable.

    A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the
    default version provided in the library.

    The following allocation and deallocation functions are implicitly declared in global scope in each translation unit of a program:

    • void* operator new(std::size_t);
    • void* operator new[](std::size_t);
    • void operator delete(void*);
    • void operator delete[](void*);
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mixed mode C++-CLI program in Visual Studio 2005 that is set
Say I have a Group data structure which contains a list of Element objects,
I have an updater program, the pulled files from server has mixed vb6 dll
I have a program which I believe to be running in .NET 4.0, but
I have an xml document that contains some html. <begin-line> <verse-num>6</verse-num>a mixed people<footnote id=f2>
I have written a game playing program for a competition, which relies on some
We have a very large program here which mixes C++ and FORTRAN (sorry). One
I have a little C program which takes a number of vectors and their
we have a mixed development environment using ASP.NET MVC and Ruby on Rails. We
i have a mixed aspx/MVC webapp project and need to rewrite incoming URL's either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.