free in C++?

View: New views
3 Messages — Rating Filter:   Alert me  

free in C++?

by James K. Lowden-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is malloc_options supposed to work in C++?  I have this message:

astream in free(): warning: junk pointer, too high to make sense.

My code doesn't call free().  It calls the delete operator, but this
message is not consequent to my deletes, if gdb is to be believed.  

The free manpage indicates this is a libc diagnostic.  Trying to follow
directions, I put

        extern char *malloc_options;
        malloc_options = "X";

in the main module.  That seems like it should be enough, but to make sure
I also export MALLOC_OPTIONS=X in the shell.  Yet the behavior doesn't
change.  

I'd like to use MALLOC_OPTIONS=X or similar to trap it.  

Suggestions?  

--jkl


Re: free in C++?

by Martijn van Buul :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* James K. Lowden:
> Is malloc_options supposed to work in C++?  I have this message:

Probably, operator new/delete will in all likelyhood call malloc/free
eventually, but keep in mind that it's possible to overload these.

> The free manpage indicates this is a libc diagnostic.  Trying to follow
> directions, I put
>
> extern char *malloc_options;

The manpage calls for _malloc_options. Note the leading underscore. Also,
since you're calling this from C++, you will need to explicitly tell
it to use C linkage. This is not a NetBSD issue, but a C-vs-C++ issue.

try

extern "C" char *_malloc_options;
char *_malloc_options = "X";

This works for me; at least "J" does what it's supposed to do.

--
Martijn van Buul - pino@...


Re: free in C++?

by James K. Lowden-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Martijn van Buul wrote:

> > The free manpage indicates this is a libc diagnostic.  Trying to
> > follow directions, I put
> >
> > extern char *malloc_options;
>
> The manpage calls for _malloc_options. Note the leading underscore.
> Also, since you're calling this from C++, you will need to explicitly
> tell it to use C linkage. This is not a NetBSD issue, but a C-vs-C++
> issue.
>
> try
>
> extern "C" char *_malloc_options;
> char *_malloc_options = "X";

Well, that's odd.  I suppose I should have mentioned

        $ uname -sr && c++ -v 2>&1 | grep version
        NetBSD 2.0
        gcc version 3.3.3 (NetBSD nb3 20040520)

My libc agrees with my man page and has no leading underscore:

$ nm /usr/lib/libc.a | grep malloc_options
00000004 C malloc_options

I tried your suggestion and got:

/usr/include/g++/bits/stl_deque.h:1065: undefined reference to
`_malloc_options'

AFAIK your point about external linkage applies only to functions, not
global (file-scope) variables, because only functions have mangled names.
I found using getopt(3) from a C++ function I can *not* declare e.g.
optarg as extern "C".  

I suppose this is really a gcc question; I just hoped to get lucky here.  

--jkl