473,588 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code Review...

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/
Nov 14 '05 #1
12 1744
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #2
"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
Nov 14 '05 #3
"Ronny Mandal" <ro*****@math.u io.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.
Nov 14 '05 #4
nrk
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.
Nov 14 '05 #5
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
Nov 14 '05 #6
On Sun, 28 Dec 2003 04:06:32 +0000, striker <st*****@strike rnet.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>>
Nov 14 '05 #7
Barry Schwarz wrote:
striker <st*****@strike rnet.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #8

On Sun, 28 Dec 2003, CBFalconer wrote:

Barry Schwarz wrote:
striker <st*****@strike rnet.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

Nov 14 '05 #9
On Sun, 28 Dec 2003 22:41:13 GMT, CBFalconer <cb********@yah oo.com>
wrote:
Barry Schwarz wrote:
striker <st*****@strike rnet.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>>
Nov 14 '05 #10

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

Similar topics

3
5213
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 proposals, what should I be asking /looking out for? I realise that running the applications through a migration tool will help but I am looking for a more through analysis of the actual codebase as well as assessment of the architecture Thanks
0
1432
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 members-only list
18
2135
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 http://www.codeproject.com/cpp/notepadre.asp I'm trying to use best practice in my C++ programming and would appreciate any advice anyone can give. As code is far more than just a one page sample, just a review of one of the source files (or even just a function or two) is...
19
2254
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 personal server. Are there any code samples or books that someone can recommend so that I can implement this. I would prefer VB.NET code, but I am willing to convert from C# if necessary.
3
1669
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 or software to optimizate this task? We actually use .Net 2003 and source safe 6.0d. Thanks in advance.
239
10152
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 users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling suite to work properly. My assumption is that this is due to the author/s creating the applications...
3
1595
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.
10
1772
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 to look up stuff. maybe someone can suggest a good book on good practices for c#? Cheers Jo
4
1906
maxx233
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 within our organization. Should I have a class called Review, with properties for things like "review date", "employee being reviewed", etc?.. And methods for things like submit(), generateReport(), delete(), etc? I don't really think I *need* an...
0
1639
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 great news with its associates that Secure Auditor has been branded as a Five Star product by SC Magazine August 2008 edition. SC Magazine is among the world’s most prestigious Information Security magazines. In the comparative review with...
0
7927
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7857
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8220
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8352
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6632
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5723
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3846
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1457
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1194
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.