Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is the behaviour defined

Reply
Thread Tools

Is the behaviour defined

 
 
pete
Guest
Posts: n/a
 
      10-07-2005
Marc Boyer wrote:

> Here is a well defined program:
>
> #include <stdio.h>
> #include <string.h>
>
> int main(){
> void* p= &p;
> if ( strlen("abc") < sizeof(p) ){
> memcpy(p, "abc", strlen("abc")+1 );
> puts( (char*) p );


It's not defined and these issues have
already been gone over in this thread.

p is an indeterminate pointer after the memcpy call.

Accessing the value of p at that point,
as the cast operator does, is undefined.

#include <stdio.h>
#include <string.h>

#include <stdio.h>
#include <string.h>

int main(void)
{
void *p = &p;

if (strlen("abc") < sizeof(p)) {
strcpy(p, "abc");
puts((char *)&p);
} else {
puts("Pointers too small");
}
return 0;
}

--
pete
 
Reply With Quote
 
 
 
 
Marc Boyer
Guest
Posts: n/a
 
      10-07-2005
Le 07-10-2005, pete <> a écrit*:
> Marc Boyer wrote:
>
>> Here is a well defined program:
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main(){
>> void* p= &p;
>> if ( strlen("abc") < sizeof(p) ){
>> memcpy(p, "abc", strlen("abc")+1 );
>> puts( (char*) p );

>
> It's not defined and these issues have
> already been gone over in this thread.
>
> p is an indeterminate pointer after the memcpy call.


Yes, sorry. I ve not posted the right version.

> Accessing the value of p at that point,
> as the cast operator does, is undefined.


Of course.

> #include <stdio.h>
> #include <string.h>
>
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> void *p = &p;
>
> if (strlen("abc") < sizeof(p)) {
> strcpy(p, "abc");
> puts((char *)&p);
> } else {
> puts("Pointers too small");
> }
> return 0;
> }


Yes


--
À vélo, prendre une rue à contre-sens est moins dangeureux
que prendre un boulevard dans le sens légal. À qui la faute ?
 
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
Re: __STDC_IEC_559__ (defined or !defined ?) Keith Thompson C Programming 0 08-17-2010 04:36 PM
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
Using parenthesis with defined (#if defined(...)) Angel Tsankov C++ 1 04-05-2006 10:00 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 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