473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

"define" and "malloc" more

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
17 2373
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
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
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
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

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
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
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
"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
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
"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
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
"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
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
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

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
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Chen Shusheng | last post by:
CSS white here: Simply strange, I found "define" can not work with "malloc". Together my complier will say "parse error". Could anyone tell me why? ------------------------- #define MAX 10000...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.