Hi,
I invite reviews for the following code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main ( void )
{
char *p;
p = (char*) &p;
strcpy ( p, "Hi" );
printf ( "%s\n", p );
return EXIT_SUCCESS;
}
Thanks.
--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/ 12 1735
On Wed, 24 Dec 2003 11:45:50 +0530, "Vijay Kumar R Zanvar"
<vi*****@hotpop.com> wrote in comp.lang.c: Hi,
I invite reviews for the following code:
Your code invokes undefined behavior. #include <stdio.h> #include <string.h> #include <stdlib.h>
int main ( void ) { char *p;
p is an uninitialized pointer to char.
p = (char*) &p;
p now contains the its own address.
strcpy ( p, "Hi" );
Now you overwrite p's contents with three characters, 'H', 'i', and
'\0'. Immediate undefined behavior if sizeof (char *) is < 3, which
is true on many 16-bit implementations.
printf ( "%s\n", p );
Undefined behavior for sure, you have modified the value of p via an
lvalue of character type. Accessing it as a pointer, or indeed as
anything other than an array of character type, is now undefined
behavior.
Undefined behavior also because printf() will attempt to dereference
p, which almost certainly no longer points to a string your program
has the right to access.
return EXIT_SUCCESS; }
Thanks.
What did you actually think this silly nonsense would be good for?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
"Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message
news:bs************@ID-203837.news.uni-berlin.de... Hi,
I invite reviews for the following code:
#include <stdio.h> #include <string.h> #include <stdlib.h>
Includes ok. int main ( void ) { char *p;
p = (char*) &p;
Why cast the pointers address to the pointer? WHen you operate on pointers,
*p will give you accessto what is stored at the pointers address.
Similar to ordinary variables:
int p=5
printf ( "%d\n", p ); will yield 5
eq
int *p = 5;
printf ( "%d\n", *p ); will yield 5 also
printf ( "%d\n", p ); will yield the address in memory where p is stored.
Doing this cast will as always compile correctly, but yield a seg. fault.
strcpy ( p, "Hi" ); printf ( "%s\n", p ); return EXIT_SUCCESS;
Assuming that EXIT_SUCCESS is 0 (simply put in a 'define EXIT_SUCCESS 0') }
Thanks.
-- Vijay Kumar R Zanvar My Home Page - http://www.geocities.com/vijoeyz/
--
I hope that this was nearby the answer you wished for.
Ronny Mandal
"Ronny Mandal" <ro*****@math.uio.no> wrote: When you operate on pointers, *p will give you access to what is stored at the pointers address. Similar to ordinary variables:
int p=5
;
printf ( "%d\n", p ); will yield 5
It'll output the digit 5 and a newline character, yeah.
eq
int *p = 5;
This wrongly attempts to initialise a pointer type with an integer. It
is a constraint violation, so the compiler must emit a diagnostic
message. Perhaps you actually meant:
int i = 5;
int *p = &i;
Now i has the value 5, and p has the value of the address of i.
printf ( "%d\n", *p ); will yield 5 also
True, given my correction.
printf ( "%d\n", p ); will yield the address in memory where p is stored.
This is undefined behaviour, as the %d conversion requires an int as its
argument. The correct way to output a representation of the value of a
pointer is:
printf("%p\n", (void *)p);
This converts the value of type 'pointer to int' into a value of type
'pointer to void' as required by the %p conversion specifier.
Assuming that EXIT_SUCCESS is 0 (simply put in a 'define EXIT_SUCCESS 0')
No! EXIT_SUCCESS is a macro defined in <stdlib.h>, which the OP Vijay
correctly included. It has the same meaning as returning 0, but need
not actually have the value 0. You are not allowed to define this
macro yourself, that would be undefined behaviour.
--
Simon.
Vijay Kumar R Zanvar wrote: Hi,
I invite reviews for the following code:
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main ( void ) { char *p;
p = (char*) &p; strcpy ( p, "Hi" ); printf ( "%s\n", p ); return EXIT_SUCCESS; }
Thanks.
Crap.
-nrk.
On Wed, 24 Dec 2003 11:45:50 +0530, Vijay Kumar R Zanvar wrote: Hi,
hey I invite reviews for the following code:
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main ( void ) {
3 lines for a function definition? Well, guess it's ok... char *p;
p = (char*) &p;
casting a (char **) to a (char *). Not very healthy. strcpy ( p, "Hi" );
now copying literal string "Hi" to *p. Fsck, Segfault! printf ( "%s\n", p );
If your O/S managed not to segfault then you'll see lots of crap in your
terminal. return EXIT_SUCCESS;
Yeah, no errors at all. } } }
Those last braces are lost in the source. Thanks.
You're welcome
On Sun, 28 Dec 2003 04:06:32 +0000, striker <st*****@strikernet.org>
wrote: On Wed, 24 Dec 2003 11:45:50 +0530, Vijay Kumar R Zanvar wrote:
Hi, hey I invite reviews for the following code:
#include <stdio.h> #include <string.h> #include <stdlib.h>
int main ( void ) {
3 lines for a function definition? Well, guess it's ok... char *p;
p = (char*) &p; casting a (char **) to a (char *). Not very healthy.
Since char* and void* are required to have the same representation,
why do you think this is a problem? strcpy ( p, "Hi" ); now copying literal string "Hi" to *p. Fsck, Segfault!
Unless p happens to occupy less than three bytes (possibly true on
some 16 bit systems), why do you think overlaying the bytes of p
causes a segfault. By the way, lots of systems don't have segments
and therefore cannot have segfaults. printf ( "%s\n", p ); If your O/S managed not to segfault then you'll see lots of crap in your terminal.
This one is more likely to cause a memory access failure than anything
previous. return EXIT_SUCCESS; Yeah, no errors at all. } } } Those last braces are lost in the source. Thanks. You're welcome
<<Remove the del for email>>
Barry Schwarz wrote: striker <st*****@strikernet.org> wrote: Vijay Kumar R Zanvar wrote:
.... snip ... char *p;
p = (char*) &p;
casting a (char **) to a (char *). Not very healthy.
Since char* and void* are required to have the same representation, why do you think this is a problem?
I see no void*. Why do you think a pointer to a pointer to a char
necessarily has any similarity?
--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
On Sun, 28 Dec 2003, CBFalconer wrote: Barry Schwarz wrote: striker <st*****@strikernet.org> wrote: Vijay Kumar R Zanvar wrote: > > char *p; > > p = (char*) &p;
casting a (char **) to a (char *). Not very healthy.
Since char* and void* are required to have the same representation, why do you think this is a problem?
I see no void*. Why do you think a pointer to a pointer to a char necessarily has any similarity?
I think Barry was trying to point out that the assignment,
while "not very healthy," was in fact perfectly *legal* C code,
via the similarity between
void *foo = (void *) &p; /* obviously correct */
and
char *bar = (char *) &p; /* also correct */
A (char *), AFAIK, is guaranteed to be able to point anywhere a
(void *) can -- because a 'char' is the smallest addressable
unit of memory in C.
Now, I don't wish to beat Barry with a dead horse, but I have
pointed out ad nauseam that just because (void *) must have the
same representation as "a pointer to a character type," doesn't
mean it must have the same representation as a pointer to 'char'
*in particular*! So his statement, while well-intentioned, was
a little off-target [unless that passage from N869 has been
clarified when I wasn't paying attention].
-Arthur
On Sun, 28 Dec 2003 22:41:13 GMT, CBFalconer <cb********@yahoo.com>
wrote: Barry Schwarz wrote: striker <st*****@strikernet.org> wrote: > Vijay Kumar R Zanvar wrote: >... snip ... >> >> char *p; >> >> p = (char*) &p; > >casting a (char **) to a (char *). Not very healthy.
Since char* and void* are required to have the same representation, why do you think this is a problem?
I see no void*. Why do you think a pointer to a pointer to a char necessarily has any similarity?
&p has type pointer to pointer to char. Let's call this pointer to T.
Any pointer can be converted (explicitly or implicitly) to type void*
without problem. char* is required to have the same representation as
void *. Therefore my question: Why did striker believe that
explicitly casting a pointer to T to a char* would cause a problem?
What kind of problem could it possibly cause?
<<Remove the del for email>>
"Ronny Mandal" <ro*****@math.uio.no> wrote in message
news:bs**********@readme.uio.no... "Vijay Kumar R Zanvar" <vi*****@hotpop.com> wrote in message news:bs************@ID-203837.news.uni-berlin.de...
<snip> char *p;
p = (char*) &p; Why cast the pointers address to the pointer?
What about, "because it wouldn't compile otherwise?" (Hint: think types!)
Simon Biber has already corrected your other errors and other posters
corrected the OP, so I won't bother.
Peter
On 28 Dec 2003 19:45:41 GMT, in comp.lang.c , Barry Schwarz
<sc******@deloz.net> wrote: On Sun, 28 Dec 2003 04:06:32 +0000, striker <st*****@strikernet.org> wrote:
On Wed, 24 Dec 2003 11:45:50 +0530, Vijay Kumar R Zanvar wrote: strcpy ( p, "Hi" );now copying literal string "Hi" to *p. Fsck, Segfault!
Unless p happens to occupy less than three bytes (possibly true on some 16 bit systems), why do you think overlaying the bytes of p causes a segfault.
The size of p is not really relevant. It is uninitialised, copying
anything into wherever it points is UB, and might well segfault ....
By the way, lots of systems don't have segments and therefore cannot have segfaults.
..... even on a machine which doesn't have segfaults. Its UB. It can do
anything it jolly well pleases.
More pragmatically, its quite possible that the particular arch used
by the OP points all uninitialised pointers at readonly memory, or at
an invalid address.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Mark McIntyre <ma**********@spamcop.net> wrote in message news:<f2********************************@4ax.com>. .. On 28 Dec 2003 19:45:41 GMT, in comp.lang.c , Barry Schwarz <sc******@deloz.net> wrote:
On Sun, 28 Dec 2003 04:06:32 +0000, striker <st*****@strikernet.org> wrote:
On Wed, 24 Dec 2003 11:45:50 +0530, Vijay Kumar R Zanvar wrote: strcpy ( p, "Hi" ); now copying literal string "Hi" to *p. Fsck, Segfault! Unless p happens to occupy less than three bytes (possibly true on some 16 bit systems), why do you think overlaying the bytes of p causes a segfault.
The size of p is not really relevant. It is uninitialised, copying anything into wherever it points is UB, and might well segfault ....
If you go back to my message which you responded to and go up 9 lines
from the "Unless" line, you will see that the OP initialized p with
the statement
p = (char*)&p;
so that p points to an area of memory exactly sizeof p bytes long. As
long as p occupies at least three bytes, there is no undefined
behavior associated with the call to strcpy. By the way, lots of systems don't have segments and therefore cannot have segfaults. .... even on a machine which doesn't have segfaults. Its UB. It can do anything it jolly well pleases.
It is not UB unless sizeof p < 3. More pragmatically, its quite possible that the particular arch used by the OP points all uninitialised pointers at readonly memory, or at an invalid address.
Not relevant since the pointer is initialized. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Arvie |
last post by:
I need some advice guys.. I am proposing that we get someone to do a
complete audit/review of our Java application codebase, about 1000
JSPs/Servlets and 100 EJBs. If I get firms to submit...
|
by: gs-code-review-bounces |
last post by:
Your mail to 'gs-code-review' with the subject
Re: Application
Is being held until the list moderator can review it for approval.
The reason it is being held:
Post by non-member to a...
|
by: Ben Hanson |
last post by:
I have created an open source Notepad program for Windows in C++ that allows
search and replace using regular expressions (and a few other extras). It
is located at...
|
by: Swaregirl |
last post by:
Hello,
I would like to build a website using ASP.NET. I would like website
visitors to be able to download code that I would like to make available to
them and that would be residing on my...
|
by: Filippo |
last post by:
Hi,
In my organization we would like to activate a code review system, in
wich a developer have to pass a review from a reviewer before check in
the modified files in source safe. Is there any way...
| |
by: Eigenvector |
last post by:
My question is more generic, but it involves what I consider ANSI standard C
and portability.
I happen to be a system admin for multiple platforms and as such a lot of
the applications that my...
|
by: JeanDean |
last post by:
I am looking for freeware tool which can review the c++ code(compiled
on g++).
Please share your experiences and details obout the usage of the tool.
|
by: Jo |
last post by:
Hi there:
I m wondering what can I do to improve my code, everytime I am coding
I feel like it could be done better. I started on c# a good months ago
and feel conformtable but sometimes I Need...
|
by: maxx233 |
last post by:
Hello all,
I'm new to OO design and have a question regarding where I should place some code. Here's a simplified situation:
I'm making an app to do create, submit, and track employee reviews...
|
by: corey |
last post by:
Secure Bytes audit and vulnerability assessment software Secure
Auditor named “Versatile tool” and earn “Five Star Ratings” in SC
Magazine Group Test
Secure Bytes is really pleased to share this...
|
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,...
| |
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |