3

I'm on amd64 architecture. The following code gets rejected by the g++ compiler at the "if" statement:

void * newmem=malloc(n);
if(newmem==0xefbeaddeefbeadde){

with the error message:

error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

I can't seem to find the magic incantation needed to get it going (and I don't want to use -fpermissive). Any help appreciated.

Background: I'm hunting an ugly bug which crashes my program while requesting memory in some STL new operation (at least gdb told me that). Thinking it could be some memory overrun of one of the allocated memory chunks being, by bad luck, adjacent to memory used by the OS to manage memory lists of my program, I quickly overrode new(), new plus their delete counterparts with own routines that added memory fencing; and while the application still crashes (all fences intact (sigh)), gdb now told me this:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000604f5e in construct (__val=..., __p=0xefbeaddeefbeadde, this=<optimized out>)
at /usr/include/c++/4.6/ext/new_allocator.h:108
108           { ::new((void *)__p) _Tp(__val); }

I noted that the pointer __p has as value the pointer representation of the value I used for one of my memory fences (0xdeadbeef), hence my wish to catch this earlier in my new() to try and dump out some more complex values in my program.

Additional note: the function where it crashes runs flawlessly a couple of million times before it crashes (intermixed with dozens of other routines which all also run a couple of thousand to million times), using valgrind does not seem like an option atm because it takes 6hrs and 11 Gb before my program crashes.

3 Answers 3

2

One of them is an integer, and the other a pointer. Perhaps an ugly cast would do

if(newmem==(void*)0xefbeaddeefbeadde){
Sign up to request clarification or add additional context in comments.

3 Comments

Add an 'L' suffix to the constant
Possibly, or ULL, it depends on the system.
Gaaaaah, a dumb cast and I did not think of it. I tried static_cast but that failed. Thinking of it, that would be reintepret_cast. I need vacation.
1

For your question: you have 2 options - cast the pointer to 64bit number or cast the number to void*


For the background: as this takes too long, it could be a memory leak. So, try valgrind - no need to wait for crash, just run your program through valgrind and use the appropriate options. valgrind could help you to catch undefined behavior, too.

Another thing - compile your program with -O0 optimization level and with max level of debug symbols - -ggdb3. Also, if your executable does not generate a core dump, when this error occurs, make it generate. Then leave it working, while you're trying different approaches to catch the error.

In case you don't succeed, you'll at least have a (hopefully) nice core dump, that you can examine with gdb your_exe the_core_dump. Then, watch the core frame by frame, watch the variables, etc.

If it's not a memory leak, it could be a memory corruption somewhere, undefined behavior or something like this. If a good core dump is generated, you'll probably catch the error. Or, at least, it could give you useful information about the case.

Another thing, that could be useful, if you generate a core dump (no matter if it's a good or a bad one) - the size of the dump. If it's too large (the definition of "large" this depends on your program), then you most probably have a memory leak (it could be some kind of cross-reference issue, if you use smart pointers and if so - valgrind will not catch it)

1 Comment

Thanks for -ggdb3, didn't know that one. Most probably not a memory leak, I think I don't have any "new" construct remaining in my program as everything is (should be, I'll check again) managed by the STL.
0

That probably won't work that way. Next time when your program runs, malloc may never return that value. There is something else you must do, rather than just checking on the return value of new!

1 Comment

Fortunately the behaviour is reproducible, on two machines :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.