Simple code doesn't work on two systems

S

Sourcerer

I wrote this very simple code in .NET VC++. I compiled it on my system, and
tried to run it on my friend's computer (he doesn't have the compiler). We both
have Windows XP Professional. I have .NET framework 2.0, and he had 1.0 and it
didn't work; then he installed 2.0 and it still didn't work; so he tried with
2.1 and it didn't work, then 3.0 and nothing still worked. I have Intel Centrino
Mobile (laptop computer), and he has Intel Pentium 4 (desktop computer). His
system reported an error that it cannot execute the program because of invalid
configuration. Why doesn't it work?

Here is the code I wrote, if it would mean anything:

#include <stdlib.h>

int main(void) {
int *a;
while (1) {
a = (int *) malloc(100000 * sizeof(int));
}
return 0;
}

--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
B

Ben Voigt

Sourcerer said:
I wrote this very simple code in .NET VC++. I compiled it on my system, and
tried to run it on my friend's computer (he doesn't have the compiler). We
both have Windows XP Professional. I have .NET framework 2.0, and he had
1.0 and it didn't work; then he installed 2.0 and it still didn't work; so
he tried with 2.1 and it didn't work, then 3.0 and nothing still worked. I
have Intel Centrino Mobile (laptop computer), and he has Intel Pentium 4
(desktop computer). His system reported an error that it cannot execute the
program because of invalid configuration. Why doesn't it work?

Did you do a release compile? The debug compile in C++ requires debug
libraries that are only present when the compiler is installed.
 
S

Sourcerer

Ben Voigt said:
Did you do a release compile? The debug compile in C++ requires debug
libraries that are only present when the compiler is installed.

Do you mean by going to Build->Compile in the menu (or pressing Ctrl+F7)? I did
that, but it doesn't work.

--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
B

Ben Voigt

Sourcerer said:
Do you mean by going to Build->Compile in the menu (or pressing Ctrl+F7)?
I did that, but it doesn't work.

I mean select "Release" as the current build configuration. After you
compile, what directory does the .exe appear in? If it's a Debug directory,
then it won't run on any computer without the compiler installed. Note that
the directory name isn't what counts, it's just a good indicator under the
default project options.

The build configuration appears on the "Standard" toolbar in Visual Studio
2005.
 
S

Sourcerer

Ben Voigt said:
I mean select "Release" as the current build configuration. After you
compile, what directory does the .exe appear in? If it's a Debug directory,
then it won't run on any computer without the compiler installed. Note that
the directory name isn't what counts, it's just a good indicator under the
default project options.

The build configuration appears on the "Standard" toolbar in Visual Studio
2005.

OK, that solved the problem with the error. The program now runs, but is
terminated by Windows. Windows reports being low on Virtual Memory.

Now I know for a fact that my roommate has his Virtual Memory size set to zero.
However, what I don't understand is why the program won't run, given that he has
1.7GB of RAM free most of the time (idle Windows only use about 300MB). Why
won't Windows use physical memory, but instead insist on using virtual memory
for this program? Other programs run just fine on his computer.

--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
W

Willy Denoyette [MVP]

Sourcerer said:
OK, that solved the problem with the error. The program now runs, but is terminated by
Windows. Windows reports being low on Virtual Memory.

Now I know for a fact that my roommate has his Virtual Memory size set to zero. However,
what I don't understand is why the program won't run, given that he has 1.7GB of RAM free
most of the time (idle Windows only use about 300MB). Why won't Windows use physical
memory, but instead insist on using virtual memory for this program? Other programs run
just fine on his computer.

--
"It is easy in the world to live after the world's opinion; it easy in solitude to live
after our own; but the great man is he who in the midst of the crowd keeps with perfect
sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/





while (1) {
a = (int *) malloc(100000 * sizeof(int));
}

Not surprising, this is an endless loop, you keep allocating memory until all your VAS is
exhausted. What did you expect from this code?

Willy.
 
S

Sourcerer

Willy Denoyette said:
while (1) {
a = (int *) malloc(100000 * sizeof(int));
}

Not surprising, this is an endless loop, you keep allocating memory until all
your VAS is exhausted. What did you expect from this code?

No, you misunderstood. It's supposed to be an endless loop. However, the program
doesn't even begin execution. We were monitoring the performance in Task
Manager, to see how the memory gets allocated. On my system, the memory is
properly allocated until it is all gone, and then I terminate the execution by
force. On his system, nothing gets allocated. The performance meters don't show
any memory being allocated at all.

My question is, what does this have to do with Virtual Memory? I want to occupy
physical memory, not virtual.

--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
N

Nathan Mates

My question is, what does this have to do with Virtual Memory? I
want to occupy physical memory, not virtual.

So does everybody. But, when every app wants a lot of physical
memory, then something's gotta give. Virtual memory is the way of
fulfilling the promises made to each app, allowing them to share the
physical memory. Consider virtual memory the security for a loan
you're getting.

Nathan Mates
 
S

Sourcerer

Nathan Mates said:
So does everybody. But, when every app wants a lot of physical
memory, then something's gotta give. Virtual memory is the way of
fulfilling the promises made to each app, allowing them to share the
physical memory. Consider virtual memory the security for a loan
you're getting.

Something has to go wrong eventually, yes, but why is the program terminated
immediately?
Why doesn't the system allocate physical memory when it is available, and only
when it's running out go tamper with virtual memory?

--
"It is easy in the world to live after the world's opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 
B

Ben Voigt

Sourcerer said:
Something has to go wrong eventually, yes, but why is the program
terminated immediately?
Why doesn't the system allocate physical memory when it is available, and
only when it's running out go tamper with virtual memory?

Only kernel-mode code can use physical memory, all user code uses virtual
memory which can be mapped into physical memory or into the swapfile (also
called pagefile). You can't disable virtual memory, the VMM is essential to
any modern OS because it does stack allocation, process isolation, and a
host of other stuff that's not related to the swapfile. You can only
disable the swapfile. Some things can still be purged temporarily from
physical ram even without a swapfile, specifically read-only segments which
are mainly code (because the system can always get them back from disk).
That's one reason you can't delete a program that's running.

In fact, with 1.7GB of physical ram, you are as likely to run out of virtual
memory space (2 GB limit, which can be fragmented leaving holes, and even
though you haven't much fragmentation in your allocation pattern, the DLL
load addresses cause some holes in your address space) as physical.

Why the program terminates immediately I can't quite say. How do you know
it isn't running? On a fast processor it could finish before task manager
ever notices it. Perhaps if you write to a file at the beginning of the
program, you can find out if it ever starts up. You could also log after
each allocation to find out how many times the loop runs. Make sure you
flush the file after each write, otherwise the runtime library will cache
your messages in memory, and they'll be lost when the error occurs.
 
W

Willy Denoyette [MVP]

Sourcerer said:
No, you misunderstood. It's supposed to be an endless loop. However, the program doesn't
even begin execution. We were monitoring the performance in Task Manager, to see how the
memory gets allocated. On my system, the memory is properly allocated until it is all
gone, and then I terminate the execution by force. On his system, nothing gets allocated.
The performance meters don't show any memory being allocated at all.

If you run this on a system that is able to allocate all available VAS, before taskman is
showing the memory counter's value, you won't see anything *unusual*, more your program will
probably prevent Taskman from updating the UI in a timely fashion, because your program pegs
the CPU and the Memory system.
Beware that the smallest interval between the fetches is 1 second, fast systems are able to
allocate much more RAM than available on most systems within one second.
To illustrate what I'm saying, add a Sleep(10) call between each malloc and watch Taskman.
My question is, what does this have to do with Virtual Memory? I want to occupy physical
memory, not virtual.

You will occupy physical memory, but this is not under your control, neither will you
allocate physical memory only by calling malloc, you need to write to this memory before
virtual memory (reserved) turns into physical (comitted).

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top