By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,571 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,571 IT Pros & Developers. It's quick & easy.

"define" and "malloc" more

P: n/a
Hi all,

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?
Aug 17 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
every process will have 4GB virturl memroy.
and 2GB of it is user space in Windows ( other 2GB is for system )

P.S. send me an email if you have more queesion(s).

Aug 17 '06 #2

P: n/a
Seems not only because of virtural memoral.
I checked my "windows task manager" while running the code. The usage of PF
do not have any changes.
I expect an increased usage of PF.

"monnand" <mo*****@gmail.com>
??????:11**********************@i3g2000cwc.googleg roups.com...
every process will have 4GB virturl memroy.
and 2GB of it is user space in Windows ( other 2GB is for system )

P.S. send me an email if you have more queesion(s).

Aug 17 '06 #3

P: n/a
Chen Shusheng schrieb:
Hi all,

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));
Take a texteditor and write 1000000000 times:

"I won't cast malloc's return value any more",

when done, write 1000000000 times:

"I will check malloc's return value for NULL every time".
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?
I guess, malloc() returned NULL and there was no memory allocated at all.

--
Thomas
Aug 17 '06 #4

P: n/a
On 17 Aug 2006 01:48:23 -0700, "monnand" <mo*****@gmail.comwrote:
>every process will have 4GB virturl memroy.
and 2GB of it is user space in Windows ( other 2GB is for system )
It's one thing to reply to an off-topic post. It's an entirely another
matter to reply to an off-topic post with false information.

http://www.microsoft.com/whdc/system...AE/PAEmem.mspx
>P.S. send me an email if you have more queesion(s).
I guarantee that you will never receive an email from me, because I'll
never have any "queesion(s)". I assume that your citation of "you"
applied to anyone in this newsgroup, just like I assumed that "virturl
memroy" referred to "virtual memory" and "queesion(s)" referred to
"question(s)".

Best regards
--
jay
Aug 17 '06 #5

P: n/a

Chen Shusheng wrote:
Hi all,

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:
But run these codes in my IDE, I do not say anything wrong in my system. So
could anyone help?
The system allocated the memory, but most of it sat on the disk as
virtual pages in the virtual swap file.

To actually exercise the memory, try setting each element of the array,
preferably at random. Then leave for a 6-month vacation. It may
take that long for the whole array to be populated. Example:

for( i = 0; i < MAX; i++ ) {
index = rand() * MAX;
temp1[ index ] = 123.456;
temp2[ index ] = 456.789;
}

Aug 17 '06 #6

P: n/a
On 17 Aug 2006 03:22:48 -0700, "Ancient_Hacker" <gr**@comcast.net>
wrote:
>
Chen Shusheng wrote:
>Hi all,

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:
>But run these codes in my IDE, I do not say anything wrong in my system. So
could anyone help?

The system allocated the memory, but most of it sat on the disk as
virtual pages in the virtual swap file.

To actually exercise the memory, try setting each element of the array,
preferably at random. Then leave for a 6-month vacation. It may
take that long for the whole array to be populated. Example:

for( i = 0; i < MAX; i++ ) {
index = rand() * MAX;
temp1[ index ] = 123.456;
temp2[ index ] = 456.789;
}
That's not a very good example. According to the C Standard:

"The value of the RAND_MAX macro shall be at least 32767."

Three of your four lines of code could very well lead to undefined
behavior on a bunch of implementations.

Best regards
--
jay
Aug 17 '06 #7

P: n/a
Chen Shusheng wrote:
Seems not only because of virtural memoral.
See below.

Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Aug 17 '06 #8

P: n/a
"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.
Aug 17 '06 #9

P: n/a
Chen Shusheng wrote:
#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));
MAX * sizeof(long double) is probably not fitting in size_t, so you're
not trying to allocate the amount you think you are.

Aug 17 '06 #10

P: n/a
"tedu" <tu@zeitbombe.orgwrites:
Chen Shusheng wrote:
>#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));

MAX * sizeof(long double) is probably not fitting in size_t, so you're
not trying to allocate the amount you think you are.
Yes, that's another possibility. MAX is one billion; sizeof(long
double) is typically 8, 12, or 16, and any of those will exceed the
maximum value of a 32-bit size_t. Since unsigned arithmetic
doesn't overflow, the compiler won't warn about this.

As it turns out, even if size_t is 32 bits and sizeof(long double) is
either 8, 12, or 16 (none of these are guaranteed), the result of the
multiplication is still a very large number, almost certainly more
than the amount of memory a program will be able to allocate. It's
still necessary to check the result of malloc().

--
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.
Aug 17 '06 #11

P: n/a
On Thu, 17 Aug 2006 08:37:49 UTC, "Chen Shusheng"
<ly*******@hotmail.tomwrote:
Hi all,

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
Who says that this is a legal int?
int main(void)
{
long double *temp = (long double*)malloc (MAX * sizeof(long double));
long double *temp1 = (long double*)malloc (MAX * sizeof(long double));
Never ever cast the result of malloc(). At best you'll end up in the
lands of undefined behavior, at worsest you'll suppress any diagnostic
the compiler may give you.

You have no access to temp and tmp. So the compiler may decide to
throw away the whole lines or even temp and temp1 becaue you does
nothing with them, not even check the result of malloc().

malloc() accepts only int as parameter, so you may get a diagnostic
from the compiler and you tries to ignore it. Set up the warning level
of your compiler.
putchar(77);
What does that mean? Printing a decimal 77 means nothing
getchar();
getchar();

return 0;
}

But run these codes in my IDE, I do not say anything wrong in my system. So
could anyone help?

Read what your documentation says about malloc() and you'll have no
need to run your program anyway.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 18 '06 #12

P: n/a
"Herbert Rosenau" <os****@pc-rosenau.dewrites:
On Thu, 17 Aug 2006 08:37:49 UTC, "Chen Shusheng"
<ly*******@hotmail.tomwrote:
>Hi all,

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

Who says that this is a legal int?
The maximum guaranteed legal int is 32767. Just so the OP knows.
>int main(void)
{
long double *temp = (long double*)malloc (MAX * sizeof(long double));
long double *temp1 = (long double*)malloc (MAX * sizeof(long double));

Never ever cast the result of malloc(). At best you'll end up in the
lands of undefined behavior, at worsest you'll suppress any diagnostic
the compiler may give you.
At best you'll have poor style; at worst you'll have undefined behavior.
Almost by definition, UB can't be a "best case".
You have no access to temp and tmp. So the compiler may decide to
throw away the whole lines or even temp and temp1 becaue you does
nothing with them, not even check the result of malloc().
If the compiler is allowed to do this, it means that those lines are
irrelevant and should be removed. Having not seen the OP's code, I'm
not sure what kind of situation this is.
malloc() accepts only int as parameter, so you may get a diagnostic
from the compiler and you tries to ignore it. Set up the warning level
of your compiler.
Correction: malloc() accepts only /size_t/ as a parameter, although any
integer type will work (provided you have an appropriate prototype in
scope).
> putchar(77);

What does that mean? Printing a decimal 77 means nothing
And in the absense of a fflush (stdout), may very well /do/ nothing, at
least not immediately. I would like to know what the OP meant by 77,
though; in ASCII that would be a 'M', I believe. Why didn't he just
write 'M' in the first place. (Or whatever he might have meant by 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?
You cannot "run these codes", Chen; you can only compile them. Whether or
not you have an IDE is irrelevant. (Though if you do, it's statistically
more likely that you aren't compiling as standard C. Check your options.)

--
Andrew Poelstra <http://www.wpsoftware.net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 18 '06 #13

P: n/a
printf("temp1 = %p\n", (void*)temp1);

Could you pls explain why you use (void *) bfor ptr temp1?

Keith Thompson wrote:
"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.
Aug 18 '06 #14

P: n/a
cs*****@gmail.com writes:
printf("temp1 = %p\n", (void*)temp1);

Could you pls explain why you use (void *) bfor ptr temp1?
Please learn how to post properly. Don't top-post; see
<http://www.caliburn.nl/topposting.html>. It's not necessary to quote
the entire article to which you're replying; take the time to trim
anything not relevant to your response. The idea is to quote enough
material so your followup makes sense on its own, not to repeat
everything that's been written so far. See most of the articles in
this newsgroups for examples of how it's done.

In answer to your question, the "%p" format requires a void* argument.
temp is of type long double*. (This is one of the few cases where a
cast is appropriate.)

--
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.
Aug 18 '06 #15

P: n/a

jaysome wrote:
That's not a very good example. According to the C Standard:

"The value of the RAND_MAX macro shall be at least 32767."

Three of your four lines of code could very well lead to undefined
behavior on a bunch of implementations.

Best regards
--
jay
You'r 100% correct. I was sloppy. It should be more like:

index = random() % MAX;

But the main point still stands, you can malloc() huge amounts of
"memory", but it isnt always useful memory. It's quite a jump from an
array element that can be accssed in 3nsec versus having to wait oh,
say 300 msec. One Hundred Million times slower by my calculation.

Aug 18 '06 #16

P: n/a
"Ancient_Hacker" <gr**@comcast.netwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>
jaysome wrote:
That's not a very good example. According to the C Standard:

"The value of the RAND_MAX macro shall be at least 32767."

Three of your four lines of code could very well lead to undefined
behavior on a bunch of implementations.

Best regards
--
jay

You'r 100% correct. I was sloppy. It should be more like:

index = random() % MAX;
That's still not very good. See FAQ question 13.16.

Philip

Aug 18 '06 #17

P: n/a

Ancient_Hacker wrote:
Chen Shusheng wrote:
Hi all,

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:
But run these codes in my IDE, I do not say anything wrong in my system. So
could anyone help?

The system allocated the memory, but most of it sat on the disk as
virtual pages in the virtual swap file.

To actually exercise the memory, try setting each element of the array,
preferably at random. Then leave for a 6-month vacation. It may
take that long for the whole array to be populated. Example:

for( i = 0; i < MAX; i++ ) {
index = rand() * MAX;
temp1[ index ] = 123.456;
temp2[ index ] = 456.789;
}
Perhaps the original poster want to experience how memory leak is. This
may help.

#include <stdlib.h>
int main(void){
for(;;) malloc(1);
return 0;
}

Aug 18 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.