Win98/NT and Visual C ver. 4/5 incompatibility?

I have C++ code that compiles and executes correctly on a Windows 98 machine, using either MS Visual C++ version 4 or 5 as the compiler.

I copied the project to a Win NT 4.0 machine, and recompiled the code using MSVC version 4.  As expected, the compile was successful.  However, executing the code gave a run time error: error writing to location 0x00000000 (i.e. NULL).  When I recompile the project with MSVC version 5, it compiles and runs correctly.

Debugging the version 4 project shows the error to be at the last line of the following code:

int *my_array;
my_array = new int[some_variable];
my_array[0] = my_data;

which implies, since debugging indicates that my_array points to NULL, that the memory was not allocated during runtime.  Interestingly enough, when I step through the code in Visual C 4, it exectues fine.

Normally, I would just use Visual C++ 5, but I need to link a few libraries which were compiled for 4.

The code is compiled as a Win32 "command-line" executable, and consists of ANSI C/++, nothing Windows-specific.  

Any ideas??

Dan Benyamin
Who is Participating?

Improve company productivity with a Business Account.Sign Up

jhanceConnect With a Mentor Commented:
nietod, unless you have supplied your own exception handler for new, you will not get an exception but rather it will just return NULL.  Check your VC++ documentation.

benyamin, since you claim that the input to new is a valid number at run time (but have you actually verified this???), but it still returns NULL from new, then I'd suspect you are somehow trashing the runtime library's new allocator before this in your code.  Since you've only posted 3 lines of code we have no idea of what has happened before but I'd suggest looking at the code leading up to this problem and see what you're doing.  

You are failing to check the pointer returned by new, which is really bad programming practice.  It seems unlikely that you really are out of memory but other things can cause new to fail.  What is the value of "some_variable" when this executes?  Is it 0?  Is it negative?  Is it some HUGE value that causes new to fail?

int *my_array;
my_array = new int[some_variable];
if(my_array == NULL){
  // Do some error recovery....

my_array[0] = my_data;
>> You are failing to check the pointer returned by new
According to the current standard, new should not return NULL when it fails.  It throws an exception instead.  VC 5 does not yet have this behavior as the default, however.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

benyaminAuthor Commented:
>> Is it 0?  Is it negative?  Is it some HUGE value that causes new to fail?
No, it is on the order of 10 to 100, and since MSVC 5 for NT and both MSVC 4 & 5 for Win 98 can allocate the memory, I don't think it is the size of the allocation that is causing the error.

I realize that it's good to check the returned pointer, but having my code exit gracefully doesn't solve the problem either.

Not sure this will help, but I once had a similar problem: I new there was enough memory (if you include virtual memory) yet new returned a bad value.

My solution was to allocate a smaller block, and then allocate the bigger block again. Magically, it worked.
I said "I new there was enough memory" and meant of course "I knew there was enough memory". Freudian slip? ;-)
You said: "Interestingly enough, when I step through the code in Visual C 4, it exectues fine"

This happends with me always when I have some bug which writes somewhere in the memory. Try to find the bug.

Yonat, there is a question I think you could answer at

Jhance, I know that and even said that, but my point is that the change is being made.  There is no point in littering a program with checks that will be useless.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.