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