Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Undefined behavior?

Reply
Thread Tools

Undefined behavior?

 
 
Andrew Clark
Guest
Posts: n/a
 
      12-22-2003
*** post for FREE via your newsreader at post.newsfeed.com ***

Hi all,

Someone brought this up in a group I read on Yahoo!

char *p, *q;
p = malloc ( 16 );
if ( p ) {
q = p;
p = NULL;
free ( q );
}

Will this invoke UB?

Andrew


-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      12-22-2003
Andrew Clark <> writes:

> char *p, *q;
> p = malloc ( 16 );
> if ( p ) {
> q = p;
> p = NULL;
> free ( q );
> }
>
> Will this invoke UB?


No. There is no reason that it should.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-22-2003
Andrew Clark wrote:
>
> *** post for FREE via your newsreader at post.newsfeed.com ***
>
> Hi all,
>
> Someone brought this up in a group I read on Yahoo!
>
> char *p, *q;
> p = malloc ( 16 );
> if ( p ) {
> q = p;
> p = NULL;
> free ( q );
> }
>
> Will this invoke UB?


No.
The pointer value returned by malloc,
is the pointer value which is freed.

--
pete
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      12-22-2003
Andrew Clark wrote:
> *** post for FREE via your newsreader at post.newsfeed.com ***
>
> Hi all,
>
> Someone brought this up in a group I read on Yahoo!
>
> char *p, *q;
> p = malloc ( 16 );
> if ( p ) {
> q = p;
> p = NULL;
> free ( q );
> }
>
> Will this invoke UB?


Well yes, but only because of your failure to:

#include <stdlib.h>

(or, at least we can't see it (-: )

Other than that, all is well.

HTH,
--ag


--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      12-22-2003
Andrew Clark wrote:

> Someone brought this up in a group I read on Yahoo!
>
> char* p = (char*)malloc(16);
> if (NULL != p) {
> char* q = p;
> p = NULL;
> free(q);
> }
>
> Will this invoke undefined behavior?


No. Neither will this

char* p = (char*)malloc(16);
char* q = p;
p = NULL;
free(q);

 
Reply With Quote
 
Andrew Clark
Guest
Posts: n/a
 
      12-22-2003
*** post for FREE via your newsreader at post.newsfeed.com ***

Artie Gold <> wrote in news:3FE64536.8090902
@austin.rr.com:

> Andrew Clark wrote:
>> *** post for FREE via your newsreader at post.newsfeed.com ***
>>
>> Hi all,
>>
>> Someone brought this up in a group I read on Yahoo!
>>
>> char *p, *q;
>> p = malloc ( 16 );
>> if ( p ) {
>> q = p;
>> p = NULL;
>> free ( q );
>> }
>>
>> Will this invoke UB?

>
> Well yes, but only because of your failure to:
>
> #include <stdlib.h>
>
> (or, at least we can't see it (-: )
>
> Other than that, all is well.
>
> HTH,
> --ag
>
>


Well, for my snippet I assumed that

Andrew


-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----

 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      12-22-2003
E. Robert Tisdale wrote:

> Andrew Clark wrote:
>
>> Someone brought this up in a group I read on Yahoo!
>>
>> char* p = (char*)malloc(16);


>> if (NULL != p) {
>> char* q = p;
>> p = NULL;
>> free(q);
>> }
>>
>> Will this invoke undefined behavior?


This is downright malicious. You're pretending to quote someone else, but
have gratuitously added a cast where none exists in the original, apart
from making other unnecessary and unmarked changes. Please don't attribute
your words to someone else. In real life it would be called forgery, and
is a felony.

-nrk.

>
> No. Neither will this
>
> char* p = (char*)malloc(16);
> char* q = p;
> p = NULL;
> free(q);



 
Reply With Quote
 
The Real OS/2 Guy
Guest
Posts: n/a
 
      12-22-2003
On Mon, 22 Dec 2003 01:38:32 UTC, "E. Robert Tisdale"
<> wrote:

> Andrew Clark wrote:
>
> > Someone brought this up in a group I read on Yahoo!
> >
> > char* p = (char*)malloc(16);
> > if (NULL != p) {
> > char* q = p;
> > p = NULL;
> > free(q);
> > }
> >
> > Will this invoke undefined behavior?

>
> No. Neither will this


Wrong. It occures undefined behavior when you forget to include
stdlib.h. You hides the diagnostic saying that you tries to convert
int to a pointer throu casting the value the compiler things it were
int to a pointer.

> char* p = (char*)malloc(16);


The same bug as above - resulting in undefined behavior.

> char* q = p;
> p = NULL;
> free(q);
>


That's fine - but lets q hold an indetermined value (the pointer in it
points to somewhere but not to an address guilty for your program)
after free.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-22-2003
nrk <> spoke thus:

> E. Robert Tisdale wrote:
>>>
>>> char* p = (char*)malloc(16);


> This is downright malicious.


From Tisdale?? Never! I propose, actually, that a section of the FAQ
be devoted to identifying known trolls and such (21.1 - "Who is E.
Robert Tisdale?") for the benefit of those who might otherwise be
seduced by their wiles... 21.2 should be "What is a killfile?"

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-22-2003
nrk wrote:
>

.... snip Trollsdale stuff ...
>
> This is downright malicious. You're pretending to quote someone
> else, but have gratuitously added a cast where none exists in the
> original, apart from making other unnecessary and unmarked changes.
> Please don't attribute your words to someone else. In real life it
> would be called forgery, and is a felony.


This is just standard procedure for ERTisdale, better known as
Trollsdale. He has made himself almost as obnoxious as possible
in his inimitably ignorant way.

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeof x == 'undefined' or x == undefined? -Lost Javascript 13 01-31-2007 12:04 AM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
pass an undefined number of datasets Andreas VHDL 5 05-31-2005 06:06 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
NET::NNTP Undefined value error Gene Mat Perl 0 07-07-2003 11:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57