LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Compile C++ in Release vs. Debug Mode (https://www.linuxquestions.org/questions/programming-9/compile-c-in-release-vs-debug-mode-331504/)

Hady 06-08-2005 07:01 AM

Compile C++ in Release vs. Debug Mode
 
Hi!

I hope someone could help me here since this is not related to Linux in any way.

I'm using Visual Studio 6.0:
My code is built with no erros and no warnings.

when I compile and run my code in Visual Studio 6.0 in Release Mode (Build -> Set Active Configuration -> Win32 Release)
my code runs perfectly fine!!

when I compile and run the SAME code in Debug Mode,
it crashes after some seconds!! I get a memory access violation!

I have a 1GB RAM on my pc, so I don't think I'm running out of memory here!!


I'd really really appreciate any help!
& Thanks in advance,

Hady

trevelluk 06-08-2005 07:35 AM

That is something that I've run into as well. Do you have some kind of optimisations turned on for the release build (Project->Settings->C/C++ tab)? If so, it's possible that a dodgy piece of code is being optimised away somehow. Can you step through the code and identify where the crash occurs?

FWIW I think the "memory access violation" message is equivalent to "segmentation fault" in Linux coding, i.e. your program has tried to access memory outside of it's address space. This is very commonly caused by NULL pointers.

Hady 06-08-2005 08:00 AM

Hi & Thanks a lot for your reply.

You're right, I went through my code, it was a NULL pointer error! Now it's works correctly in Debug mode also!
Thanks!


by why when I have optimization "Maximum Speed" for Release mode,
it ignores NULL pointers?
Could you explain to me a bit about these optimization options?!

Thanks again!

trevelluk 06-08-2005 08:06 AM

Well, I don't know any real details about how optimisation works. Basically, the compiler will alter the code in some way to make it either faster or smaller (depending on which optimisation is selected). I'd guess that what happened in this case was that the optimisations in some way changed the problem area of the code, so that it wasn't a problem. The error certainly wasn't being ignored, it was just never happening at all.

Hady 06-08-2005 09:20 AM

OK, Thanks trevelluk for your replies!

deiussum 06-09-2005 11:59 AM

I've run into the reverse problem at times. Code runs fine in debug mode, but crashes in release mode. I believe another thing that debug mode does, is to allocate extra space when creating arrays and such. So... when working in debug mode, often times accessing an array beyond its bounds will succeed, but then it will fail in release mode because that extra debug memory goes away. Anyway, not really the problem you are seeing, but it is something else to watch for... :)

smallest 06-09-2005 12:22 PM

Quote:

Originally posted by Hady
Hi & Thanks a lot for your reply.
by why when I have optimization "Maximum Speed" for Release mode,
it ignores NULL pointers?

it doesn't.

uninitialized variables contiain random (unpredictable, at least) data. whatever random data was in the pointer during the debug build caused an access violation, but the random data in the release build didn't, but it could have - wherever it was pointing wasn't a place that the OS was worried about.

you just got lucky.

ta0kira 06-13-2005 04:00 AM

According to C++ standard, dereferencing bad pointers (NULL, uninitialized, or out of scope) is "undefined". That means that your implementation can throw an exception, do what "you meant to do", do something else, or do nothing at all. I've had the same problem with BCB6. Windows sometimes lets your program dereference a bad pointer and not throw an exception (especially when accessing int data members), but Linux is very good about letting you know when you've messed up.
ta0kira


All times are GMT -5. The time now is 05:55 PM.