Memory dump

E

erfan

Hi,comp.c:
I try to learn malloc,facing a problem like this:
--------

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>

int main(void)
{
char *some_memory;
int megabyte=1; /*Please take care here!!!
Here is 1 byte*/
int exit_code=1;
some_memory=(char *)malloc(megabyte);
if (some_memory!=NULL)
{
sprintf(some_memory,"123456789abcdef"); /* 1
byte=8 bits */
printf("%s",some_memory);
exit_code=0;
}
else printf("not enough memory");
free(some_memory);
exit(exit_code);
}
-------------------------
two questions:
1. I want to test how many bits should the memory dump. i malloc only
1 byte,and when i fill the memory with 16 characters,that will dump?
why not 8 ? i think 1 byte equals 8 bits
2.Does the memory dump really do harm to the memory? why gcc doesn`t
gave a warn before i run it?
if i change megabyte into 1024*1024*1024? but the memory`s real
capality is 512M, what willhappen?
 
R

Richard Heathfield

erfan said:
Hi,comp.c:
I try to learn malloc,facing a problem like this:

You don't need this header (which isn't part of standard C anyway).
#include<stdlib.h>
#include<stdio.h>

int main(void)
{
char *some_memory;
int megabyte=1; /*Please take care here!!!
Here is 1 byte*/
int exit_code=1;
some_memory=(char *)malloc(megabyte);

You don't need the cast. The canonical form is:

some_memory = malloc(megabyte * sizeof *some_memory);
if (some_memory!=NULL)
{
sprintf(some_memory,"123456789abcdef"); /* 1
byte=8 bits */

1 byte = CHAR_BIT bits. CHAR_BIT is defined in <limits.h>, and it is fairly
likely that, on *your* system, it's set to 8. (It must be at least 8, but
can be higher, and is higher on some systems.)

Your sprintf is unwise, because it attempts to store sixteen bytes of
information in a region of memory that is only guaranteed to have one byte
available to it. C doesn't define what will happen in this situation. On
some implementations, you might get away with it. On others, the program
might produce unexpected output, or no output at all. Or the program might
crash or lock up. It might even hang or crash the whole computer. Or it
might corrupt the disk, or it might jump to an unexpected part of memory
and start executing whatever instructions it finds there.
printf("%s",some_memory);
exit_code=0;
}
else printf("not enough memory");
free(some_memory);
exit(exit_code);
}

See above. I'm not sure what you mean by "memory dump" in this context. If
you are asking about core dumps, probably the best place to ask is
comp.unix.programmer, or perhaps a newsgroup dealing specifically with
your compiler.
why not 8 ?

Why 8?
i think 1 byte equals 8 bits

See above.
2.Does the memory dump really do harm to the memory?

See above.
why gcc doesn`t
gave a warn before i run it?

Because gcc isn't a mindreader. It is required to diagnose syntax errors
and constraint violations, and it is allowed to warn about anything else
it likes, but it can't diagnose any arbitrary programming mistake.

if i change megabyte into 1024*1024*1024? but the memory`s real
capality is 512M, what willhappen?

If malloc can meet the request, it will return a pointer to the first byte
in the allocated region. If not, it will return NULL.
 
A

Army1987

erfan said:
Hi,comp.c:
I try to learn malloc,facing a problem like this:
[snip code deliberately attempting to dump core]
two questions:
1. I want to test how many bits should the memory dump. i malloc only
1 byte,and when i fill the memory with 16 characters,that will dump?
why not 8 ? i think 1 byte equals 8 bits
So what? Look up 'bit'. A character occupies as many bits a byte does.
Btw, don't do that. On some implementations, abort() causes a core dump,
too, and it's a less obscure way to do that. (Not to mention that the
consequences of overrunning an array can be different than that, e.g. you
could overwrite other data.)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top