Solved

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

Posted on 1998-09-30
8
207 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.

919 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

14 Experts available now in Live!

Get 1:1 Help Now