By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

why it is not work?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a
-----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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.