"Chen Shusheng" <ly*******@hotmail.tomwrites:
In fact, I want to let my memory run out. And see what will happen. My
system is windowsXp. Memory is 256M.I think my cdes will apply more memory
than I have. Codes are below:
#include <stdlib.h>
#include<stdio.h>
#define MAX 1000000000
int main(void)
{
long double *temp = (long double*)malloc (MAX * sizeof(long double));
long double *temp1 = (long double*)malloc (MAX * sizeof(long double));
putchar(77);
getchar();
getchar();
return 0;
}
But run these codes in my IDE, I do not say anything wrong in my system. So
could anyone help?
And what did you expect it to do?
malloc() attempts to allocate the requested amount of memory. If it
succeeds, it returns a pointer to it. If it fails, it returns a null
pointer. Since you didn't check the value returned by malloc(), you
have no way of knowing whether it succeeded.
If malloc() fails, it doesn't print an error message or crash your
program, it just tells you that it failed by returning a null pointer
value. You have to decide for yourself how to respond to a failure.
Never cast the result of malloc(). Always check the value returned by
malloc(). The comp.lang.c FAQ is at <http://www.c-faq.com/>; read
questions 7.6, 7.7, 7.7a, and 7.7b. (Then read the rest of the FAQ.)
The "putchar(77);" statement prints a single character with the value
77. (This is 'M' if you happen to have an ASCII-based system. I
think it's a left parenthesis in EBCDIC.)
I suppose the intent is to print some output to let you know that the
malloc() calls have completed, but since there's no newline, it's not
guaranteed that the output will appear. I can't imagine why you chose
to print character 77. If you really want to print an 'M', just use
"putchar('M');". It would have been much clearer to use something
like
printf("mallocs done\n");
but I'm going to do something else; see below.
You have two calls to getchar(). One call would be understandable; on
some systems, particularly Windows, some methods of executing programs
will close the output window immediately after the program finishes.
A better way to handle this is to use a different method to execute
the program. You can run it from a command window, or you can execute
it from your IDE with an option to keep the window open. (I don't
know how to do this; consult your system's documentation.) I have no
idea why you need two calls to getchar().
Here's a modified version of your program:
==================================================
#include <stdlib.h>
#include<stdio.h>
#define MAX 1000000000
int main(void)
{
long double *temp = (long double*)malloc (MAX * sizeof(long double));
long double *temp1 = (long double*)malloc (MAX * sizeof(long double));
if (temp == NULL) {
printf("temp is a null pointer\n");
}
else {
printf("temp = %p\n", (void*)temp);
}
if (temp1 == NULL) {
printf("temp1 is a null pointer\n");
}
else {
printf("temp1 = %p\n", (void*)temp1);
}
return 0;
}
==================================================
I could have just used
printf("temp = %p\n", (void*)temp);
to show the value of temp whether it's a null pointer or not, but the
text representation of a null pointer can vary from one system to
another. By explicitly checking whether it's equal to NULL, we make
the output more obvious. (And, if you were actually going to use the
allocated space, you'd need to do this check anyway.)
And here's the output I got:
temp is a null pointer
temp1 is a null pointer
There are some good things in your program that I didn't have to
change. You have the #include directives for the required headers,
you properly declared "int main(void)", and you have a "return 0;" at
the end of main(). Too many people leave these out.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.