473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

why it is not work?

Hi: all:

The code is like :

char *data;
data = malloc(1000);
if (data = NULL)
{
printf (" can not assign the memory for the packet\n");
exit(1);
}

memset(data,'\0',1000);
The code is failure say "Segmentation fault"
The problem is memset, why?
Thanks

Dec 7 '05 #1
5 1199
"yezi" <ye*****@hotmail.com> writes:
Hi: all: The code is like : char *data; data = malloc(1000);
if (data = NULL)
here ^^^
{
printf (" can not assign the memory for the packet\n");
exit(1);
} memset(data,'\0',1000);
The code is failure say "Segmentation fault"
The problem is memset, why?

--
Chris.
Dec 7 '05 #2
right, thanks. but one question is " if I do not write the memset(data,
'\0',1000);

The code runs fine. only append the sentence , then crash. why?

Dec 8 '05 #3

"yezi" <ye*****@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi: all:

The code is like :

char *data;
data = malloc(1000);
if (data = NULL)
{
printf (" can not assign the memory for the packet\n");
exit(1);
}

memset(data,'\0',1000);
The code is failure say "Segmentation fault"
The problem is memset, why?


Try

if(NULL = data)

hint:
= and ==
Dec 8 '05 #4
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

yezi wrote:
right, thanks. but one question is " if I do not write the memset(data,
'\0',1000);
The code example was

) char *data;
)
) data = malloc(1000);
) if (data = NULL)
) {
) printf (" can not assign the memory for the packet\n");
) exit(1);
) }
)
) memset(data,'\0',1000);
The code runs fine. only append the sentence , then crash. why?

Because, you use memset() to set memory pointed to by pointer data
If pointer data is not initialized (or is initialized incorrectly), then
memset() writes over random memory. That invokes bad behaviour in your program
and it crashes.

So, why would data not be initialized correctly?
It wouldn't be initialized if, for some reason, malloc() failed to allocate
memory. If malloc() can't allocate memory, it returns NULL, and dereferencing
NULL can cause this problem.

Your code also has an error:
) if (data = NULL)
) {
)...
) }
Here, you don't /test/ the value of data against NULL, you /change/ the value
of data /to/ NULL. And since NULL evaluates false, the body of the if
statement doesn't get executed. So, you wipe out your pointer, replace it with
NULL, bypass your abort test, and promptly memset() NULL to some data.

Bang! Down you go.

If you /correct/ the test (don't remove it, /fix/ it), you will exit(1) (an
improper value, according to the standard, BTW) rather than memset() when data
== NULL
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDl6qxagVFX4UWr64RAtqJAKCHubGWkha9uTGGx13mBR y7VNBNIQCfQ+Oa
qB+tBI9P+1olfyRmU8A1BO8=
=SeWK
-----END PGP SIGNATURE-----
Dec 8 '05 #5
yezi wrote:
Hi: all:
data = malloc(1000);
if (data = NULL)

^^
You want the test for equality (==), not the assignment operator (=).
Dec 8 '05 #6

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

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Douglas Buchanan | last post by:
Buttons don't work if form is opened on startup A2k If 'frmMain' is set to open by default at startup none of the buttons work. If 'frmMain' is opened from the database window then all the...
15
by: Brett | last post by:
I'd like to know what management and the work environment are like where you work. I need something relative to compare my current work environment. Here's a few questions I have: 1.) Is it...
2
by: Holysmoke | last post by:
Hi, I have a asp.net form with a button in it. In the onlclick event of button I want to write a function which does some a bit of work with a timespan of 15 seconds. So I want to show a image...
1
by: Jeff | last post by:
Hello, I have developed a library of controls but for the life of me cannot get ToolboxBitmapAttribute to work -- my image simply will not display in the toolbox of any project using my...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
33
by: bonk | last post by:
I have an application that needs to perform some background work, i.e. Logging, wich must not block the main thread. How would I basically design such a scenario? It is obvious that I should do...
11
Niheel
by: Niheel | last post by:
http://bytes.com/images/howtos/information_overloaded.jpgPaul Graham wrote an interesting article a few months back about how the internet is leading to information overload for information workers...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.