Solved

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

Posted on 1998-09-30
8
205 Views
Last Modified: 2012-05-04
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??

Thanks,
Dan Benyamin
benyamin@ucla.edu
0
Comment
Question by:benyamin
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1174085
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;
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174086
>> 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.
0
 

Author Comment

by:benyamin
ID: 1174087
>> 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.

0
 
LVL 5

Expert Comment

by:yonat
ID: 1174088
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 5

Expert Comment

by:yonat
ID: 1174089
I said "I new there was enough memory" and meant of course "I knew there was enough memory". Freudian slip? ;-)
0
 

Expert Comment

by:iliya
ID: 1174090
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.

0
 
LVL 32

Accepted Solution

by:
jhance earned 150 total points
ID: 1174091
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.  

0
 
LVL 22

Expert Comment

by:nietod
ID: 1174092
Yonat, there is a question I think you could answer at
http://www.experts-exchange.com/topics/comp/lang/cplusplus/Q.10085032

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.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now