This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Implement name change in POD example; Chris Waggoner++.
[perl5.git] / pod / perlclib.pod
index 80e6194..ef0b6b0 100644 (file)
@@ -97,15 +97,17 @@ There is no equivalent to C<fgets>; one should use C<sv_gets> instead:
 
 =head2 Memory Management and String Handling
 
-    Instead Of:                 Use:
-
-    t* p = malloc(n)            New(id, p, n, t)
-    t* p = calloc(n, s)         Newz(id, p, n, t)
-    p = realloc(p, n)           Renew(p, n, t)
-    memcpy(dst, src, n)         Copy(src, dst, n, t)
-    memmove(dst, src, n)        Move(src, dst, n, t)
-    memcpy/*(struct foo *)      StructCopy(src, dst, t)
-    free(p)                     Safefree(p)
+    Instead Of:                        Use:
+
+    t* p = malloc(n)                   Newx(p, n, t)
+    t* p = calloc(n, s)                Newxz(p, n, t)
+    p = realloc(p, n)                  Renew(p, n, t)
+    memcpy(dst, src, n)                Copy(src, dst, n, t)
+    memmove(dst, src, n)               Move(src, dst, n, t)
+    memcpy(dst, src, sizeof(t))                StructCopy(src, dst, t)
+    memset(dst, 0, n * sizeof(t))      Zero(dst, n, t)
+    memzero(dst, 0)                    Zero(dst, n, char)
+    free(p)                            Safefree(p)
 
     strdup(p)                   savepv(p)
     strndup(p, n)               savepvn(p, n) (Hey, strndup doesn't exist!)
@@ -130,6 +132,19 @@ instead of raw C<char *> strings:
 Note also the existence of C<sv_catpvf> and C<sv_vcatpvfn>, combining
 concatenation with formatting.
 
+Sometimes instead of zeroing the allocated heap by using Newxz() you
+should consider "poisoning" the data.  This means writing a bit
+pattern into it that should be illegal as pointers (and floating point
+numbers), and also hopefully surprising enough as integers, so that
+any code attempting to use the data without forethought will break
+sooner rather than later.  Poisoning can be done using the Poison()
+macros, which have similar arguments to Zero():
+
+    PoisonWith(dst, n, t, b)    scribble memory with byte b
+    PoisonNew(dst, n, t)        equal to PoisonWith(dst, n, t, 0xAB)
+    PoisonFree(dst, n, t)       equal to PoisonWith(dst, n, t, 0xEF)
+    Poison(dst, n, t)           equal to PoisonFree(dst, n, t)
+
 =head2 Character Class Tests
 
 There are two types of character class tests that Perl implements: one
@@ -161,9 +176,9 @@ table, C<c> is a C<char>, and C<u> is a Unicode codepoint.
 
     atof(s)                     Atof(s)
     atol(s)                     Atol(s)
-    strtod(s, *p)               Nothing.  Just don't use it.
-    strtol(s, *p, n)            Strtol(s, *p, n)
-    strtoul(s, *p, n)           Strtoul(s, *p, n)
+    strtod(s, &p)               Nothing.  Just don't use it.
+    strtol(s, &p, n)            Strtol(s, &p, n)
+    strtoul(s, &p, n)           Strtoul(s, &p, n)
 
 Notice also the C<grok_bin>, C<grok_hex>, and C<grok_oct> functions in
 F<numeric.c> for converting strings representing numbers in the respective
@@ -193,5 +208,5 @@ For C<signal>/C<sigaction>, use C<rsignal(signo, handler)>.
 
 =head1 SEE ALSO
 
-C<perlapi>, C<perlapio>, C<perlguts>
+L<perlapi>, L<perlapio>, L<perlguts>