Hi all, I'm currently rewriting many of the standard functions in C++. No i'm not wierd, i'm in the process of learning and figured this would be a good exercise. Anyway, my strlen() function is bringing up a warning. Although I understand what the warning mean, I don't understand why it is a warning. Can someone please quickly point out the technicalities. The main point I sometimes trip up with is pointers and references (as I suspect most people learning C++ encounter) int *MyStrlen(const char *source) { int i = 0; while (*source) { *source++; i++; } return &i; } Thanks ----------------------------------------- Email provided by http://www.ntlhome.com/
Am Freitag, 28. November 2003 17:39 schrieb ged.suse@ntlworld.com:
I'm currently rewriting many of the standard functions in C++. No i'm not wierd, i'm in the process of learning and figured this would be a good exercise.
Anyway, my strlen() function is bringing up a warning. Although I understand what the warning mean, I don't understand why it is a warning. Can someone please quickly point out the technicalities. The main point I sometimes trip up with is pointers and references (as I suspect most people learning C++ encounter)
int *MyStrlen(const char *source) { int i = 0; while (*source) { *source++; i++; } return &i; }
i is a var which is allocated in the context of this function. If you leave the function i is being deleted. But you return the adress of this space from your function this will crash... Better should be: int MyStrlen(....) { ... return i; } -- Andreas
Am Freitag, 28. November 2003 23:40 schrieb Andreas Winkelmann:
Am Freitag, 28. November 2003 17:39 schrieb ged.suse@ntlworld.com:
I'm currently rewriting many of the standard functions in C++. No i'm not wierd, i'm in the process of learning and figured this would be a good exercise.
Anyway, my strlen() function is bringing up a warning. Although I understand what the warning mean, I don't understand why it is a warning. Can someone please quickly point out the technicalities. The main point I sometimes trip up with is pointers and references (as I suspect most people learning C++ encounter)
int *MyStrlen(const char *source) { int i = 0; while (*source) { *source++; i++; } return &i; }
i is a var which is allocated in the context of this function. If you leave the function i is being deleted. But you return the adress of this space from your function this will crash...
Better should be:
int MyStrlen(....) { ... return i; }
-- Andreas
You don't need the '*' before source, when you want to increment the pointer source. The function should look like: int MyStrlen(const char *source) { int i = 0; while(*source) { source++; i++; } return i; } -- Stefan Lang
On 29 Nov 2003 at 16:17, Stefan Lang wrote: From: Stefan Lang <langstefan@gmx.at> To: suse-programming-e@suse.com Date sent: Sat, 29 Nov 2003 16:17:49 +0100 Subject: Re: [suse-programming-e] c++ warning ??
Am Freitag, 28. November 2003 23:40 schrieb Andreas Winkelmann:
Am Freitag, 28. November 2003 17:39 schrieb ged.suse@ntlworld.com:
I'm currently rewriting many of the standard functions in C++. No i'm not wierd, i'm in the process of learning and figured this would be a good exercise.
In that case you might like to see if you can find a copy of 'The Standard C Library' by PJ Plauger, which is about implimenting the library :) It's published by Prentice Hall and the ISBN is 0-13-131509-9. Regards, alan -- http://www.ibgames.net/alan Registered Linux user #6822 http://counter.li.org Winding Down - Weekly Tech Newsletter - subscribe at http://www.ibgames.net/alan/winding/mailing.html
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sun, 30 Nov 2003 07:38:35 -0000 alan@ibgames.com wrote:
On 29 Nov 2003 at 16:17, Stefan Lang wrote:
Am Freitag, 28. November 2003 23:40 schrieb Andreas Winkelmann:
Am Freitag, 28. November 2003 17:39 schrieb ged.suse@ntlworld.com:
I'm currently rewriting many of the standard functions in C++. No i'm not wierd, i'm in the process of learning and figured this would be a good exercise.
In that case you might like to see if you can find a copy of 'The Standard C Library' by PJ Plauger, which is about implimenting the library :)
It's published by Prentice Hall and the ISBN is 0-13-131509-9. Back in 1988-1989 I had the opportunity to implement the C RTL. That was probably one of the most fun assignments I had ever had. Most of the standard C library functions are pretty straightforward. But, there are a few that become a challenge, such as the printf and scanf functions. In my case, many of the smaller functions were to be implemented in assembler for performance reasons.
- -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2-rc1-SuSE (GNU/Linux) iD8DBQE/yfbG+wA+1cUGHqkRAk/IAKCEne9qSx3jR0ZIC1YJPgWCcqjmfgCeOtL9 2xhYizardscmKxlDR+7Bg5k= =oLVM -----END PGP SIGNATURE-----
participants (5)
-
alan@ibgames.com
-
Andreas Winkelmann
-
ged.suse@ntlworld.com
-
Jerry Feldman
-
Stefan Lang