"define" and "malloc" more

C

Chen Shusheng

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?
 
M

monnand

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).
 
C

Chen Shusheng

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.
 
T

Thomas J. Gritzan

Chen said:
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.
 
J

jaysome

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/platform/server/PAE/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
 
A

Ancient_Hacker

Chen said:
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;
}
 
J

jaysome

Chen said:
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
 
K

Keith Thompson

Chen Shusheng said:
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.
 
T

tedu

Chen said:
#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.
 
K

Keith Thompson

tedu said:
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().
 
H

Herbert Rosenau

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!
 
A

Andrew Poelstra

Herbert Rosenau said:
Who says that this is a legal int?

The maximum guaranteed legal int is 32767. Just so the OP knows.
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).
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.)

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.)
 
C

csshine

printf("temp1 = %p\n", (void*)temp1);

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

Keith Thompson

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.)
 
A

Ancient_Hacker

jaysome said:
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

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.
 
P

Philip Potter

Ancient_Hacker said:
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
 
L

lovecreatesbeauty

Ancient_Hacker said:
Chen said:
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;
}
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top