?
Solved

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

Posted on 1998-09-30
8
Medium Priority
?
234 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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 300 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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

600 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