🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

5 total messages Started by David Fox Fri, 07 Sep 1990 17:02
Proposed addition to string library: strnew
#490
Author: David Fox
Date: Fri, 07 Sep 1990 17:02
7 lines
526 bytes
I propose the following function be added to the standard string.h:

inline char *
strnew(const char *s) 
{
  return s ? strcpy(new char[strlen(s)+1], s) : 0;
}
Re: Proposed addition to string library: strnew
#497
Author: Walter Bright
Date: Mon, 10 Sep 1990 18:22
11 lines
803 bytes
In article <FOX.90Sep7130228@devo.research.att.com> fox@research.att.com (David Fox) writes:
<I propose the following function be added to the standard string.h:
<
<inline char *
<strnew(const char *s) 
<{
<  return s ? strcpy(new char[strlen(s)+1], s) : 0;
<}

What's the matter with strdup()? It's not ANSI C, but it's in common
usage among MS-DOS and OS/2 compilers.
Re: Proposed addition to string library: strnew
#499
Author: Shankar Unni
Date: Mon, 10 Sep 1990 23:39
9 lines
600 bytes
> inline char *
> strnew(const char *s) 
> {
>   return s ? strcpy(new char[strlen(s)+1], s) : 0;
> }

Check out "strdup()".
---
Shankar.
Re: Proposed addition to string library: strnew
#503
Author: David Fox
Date: Wed, 12 Sep 1990 18:15
5 lines
876 bytes
I am aware of strdup(), but it uses malloc rather than new and
it bombs if you pass it a zero pointer, rather than passing
back a zero pointer as strnew does.  I wasn't really that
serious about strnew, but I've received many replies saying
that strdup is the same as strnew, but its not.
Re: Proposed addition to string library: strnew
#508
Author: Tony L. Hansen
Date: Thu, 13 Sep 1990 16:19
29 lines
2018 bytes
In article <2699@dataio.Data-IO.COM>, bright@Data-IO.COM (Walter Bright) writes:
< In article <FOX.90Sep7130228@devo.research.att.com> fox@research.att.com (David Fox) writes:
<< I propose the following function be added to the standard string.h:
<< inline char * strnew(const char *s) {
<<   return s ? strcpy(new char[strlen(s)+1], s) : 0;
<< }
< 
< What's the matter with strdup()? It's not ANSI C, but it's in common
< usage among MS-DOS and OS/2 compilers.

strdup() is defined as using malloc() underneath. However, there's no
guarantee in any of the C++ books that new[] and malloc() must be
compatible in any way. In other words, you cannot assume that you can
create a value using new[] and then use free() on it later, or create a
value using malloc() and pass it to delete later.

It's guaranteed that new[] is NOT compatible for types with a
constructor or destructor, as new[] is required to keep track of the
number of members within the array. Some implementations of new[] may
use malloc(), but allocate a larger amount of memory, and then place
the number of members in front of the memory whose pointer is returned.
Such an implementation is DEFINITELY NOT compatible with malloc().
Whether this is done for all invocations of new[], or just those for
types without constructors/destructors, is implementation dependent.
Consequently, you cannot guarantee anything for any invocations of new[].

					Tony Hansen
				att!pegasus!hansen, attmail!tony
				    hansen@pegasus.att.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads