?
Solved

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

Posted on 1998-09-30
8
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
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.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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