473,467 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3779
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.