472,354 Members | 1,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Memory dump

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?

Jan 18 '08 #1
2 3664
erfan said:
Hi,comp.c:
I try to learn malloc,facing a problem like this:
--------

#include<unistd.h>
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);
}
-------------------------
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?
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 18 '08 #2
erfan wrote:
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.)

--
Army1987 (Replace "NOSPAM" with "email")
Jan 18 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yuriy | last post by:
Hi, Can anybody explain the following? Say I have the following source XML and XSLT (see below). No matter what this XSLT does. It is just a sample to show a problem. the idea is that XSLT...
7
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app...
9
by: Microsoft News Server | last post by:
Hi, I am currently having a problem with random, intermittent lock ups in my ASP.net application on our production server (99% CPU usage by 3 threads, indefinately). I currently use IIS Debug...
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
1
by: Nick Craig-Wood | last post by:
I've been dumping a database in a python code format (for use with Python on S60 mobile phone actually) and I've noticed that it uses absolutely tons of memory as compared to how much the data...
9
by: Michael Sparks | last post by:
Hi, I'm interested in writing a simple, minimalistic, non persistent (at this stage) software transactional memory (STM) module. The idea being it should be possible to write such a beast in a...
1
by: Nagu | last post by:
I didn't have the problem with dumping as a string. When I tried to save this object to a file, memory error pops up. I am sorry for the mention of size for a dictionary. What I meant by...
1
by: =?Utf-8?B?d29vZiE=?= | last post by:
My laptop is generating a memory dump and crashing. With some investigation It led me to believe there was a memory problem (used memtest86+). Changed hard drive, swapped the two 512mb RAMS...
2
by: ruqiang826 | last post by:
hi I have written a service running backgroud to do something in linux. unfortunately$B!$(BI deleted the source code by mistake, and I can still see the process running background using "ps aux"...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.