Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 16th, 2006, 12:17 PM   #21
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by Jessehk
I personally think that DaWei, Narue, and Grumpy are some of, if not the most helpful members on this forum. Thanks to all of you!
++
And in the case of DaWei, possibly the most entertaining as well
Jimbo is offline   Reply With Quote
Old Jul 17th, 2006, 12:14 PM   #22
kyoryu
Newbie
 
Join Date: Jun 2006
Posts: 20
Rep Power: 0 kyoryu is on a distinguished road
Quote:
Originally Posted by Sane
By the way, Narue, I'm not sure if you were calling my approach to it incorrect. Because all I had to do was add the word static before the declaration of "new_text". Which is still returning a pointer, but it's not as if I had to do much.
Question: What is the output of the following program? Why?

// insert proper .h files, omitted for brevity
using namespace std;
const char * doSomethingWithAString( const char * inString )
{
	// please ignore the lack of length checking.
	static char internal_string[50];
	memcpy( internal_string, inString, strlen( inString ) );
	return internal_string;
}
int main()
{
	const char * first_string = doSomethingWithAString( "Hello, " );
	const char * second_string = doSomethingWithAString( "world!\n" );
	cout << first_string << " " << second_string;
	return 0;

}
kyoryu is offline   Reply With Quote
Old Aug 22nd, 2006, 3:44 AM   #23
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Hmmm ..... I don't normally resurrect old threads, but someone sent me a PM suggesting I look at the last post in this thread.

The code given by kyoryu yields undefined behaviour, as doSomethingWithAString() does not append a zero byte on the end of internal_string.

In practice, what will usually be seen will be typically be the word "world!" on a line of it's own, possibly followed by a lot of random junk, then a space, then the text "world!" on a line of it's own again, again possibly following by a lot of random junk. On some systems, the program will crash, hang, or otherwise die miserably somewhere in the process of doing all this.

If kyoru was to change the memset() line to
memcpy( internal_string, inString, strlen( inString ) +1);
then the output would be two lines of text saying "world!"

Either way, this example illustrates quite nicely that simply making a local variable static does not ensure the program gives the behaviour the programmer might intend - a lot of people would expect this code to output the line "Hello world!".
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
changing size of an array Eric the Red Java 3 Apr 3rd, 2006 8:19 PM
2 dimension array of characters brad sue C 8 Mar 15th, 2006 9:40 AM
Returning an Array Kilo C# 3 Dec 4th, 2005 10:31 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM
Returning An Array From a Function ViZioN C++ 5 Feb 21st, 2005 6:45 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC