Duplicate a string
char *SLmake_string (char *s)
The SLmake_string
function creates a new copy of the string
s
, via malloc
, and returns it. Upon failure it returns
NULL
. Since the resulting string is malloced, it should be
freed when nolonger needed via a call to either free
or
SLfree
.
SLmake_string
should not be confused with the function
SLang_create_slstring
, which performs a similar function.
SLmake_nstring, SLfree, SLmalloc, SLang_create_slstring
Duplicate a substring
char *SLmake_nstring (char *s, unsigned int n)
This function is like SLmake_string
except that it creates a
null terminated string formed from the first n
characters of
s
. Upon failure, it returns NULL
, otherwise it returns
the new string. When nolonger needed, the returned string should be
freed with SLfree
.
SLmake_string, SLfree, SLang_create_nslstring
Created a hashed substring
char *SLang_create_nslstring (char *s, unsigned int n)
SLang_create_nslstring
is like SLang_create_slstring
except that only the first n
characters of s
are used to
create the hashed string. Upon error, it returns NULL
, otherwise it
returns the hashed substring. Such a string must be freed by the
function SLang_free_slstring
.
Do not use free
or SLfree
to free the string returned by
SLang_create_slstring
or SLang_create_nslstring
. Also
it is important that no attempt is made to modify the hashed string
returned by either of these functions. If one needs to modify a
string, the functions SLmake_string
or SLmake_nstring
should be used instead.
SLang_free_slstring, SLang_create_slstring, SLmake_nstring
Create a hashed string
char *SLang_create_slstring (char *s)
The SLang_create_slstring
creates a copy of s
and
returns it as a hashed string. Upon error, the function returns
NULL
, otherwise it returns the hashed string. Such a string
must only be freed via the SLang_free_slstring
function.
Do not use free
or SLfree
to free the string returned by
SLang_create_slstring
or SLang_create_nslstring
. Also
it is important that no attempt is made to modify the hashed string
returned by either of these functions. If one needs to modify a
string, the functions SLmake_string
or SLmake_nstring
should be used instead.
SLang_free_slstring, SLang_create_nslstring, SLmake_string
Free a hashed string
void SLang_free_slstring (char *s)
The SLang_free_slstring
function is used to free a hashed
string such as one returned by SLang_create_slstring
,
SLang_create_nslstring
, or SLang_create_static_slstring
.
If s
is NULL
, the routine does nothing.
SLang_create_slstring, SLang_create_nslstring, SLang_create_static_slstring
Concatenate two strings to produce a hashed string
char *SLang_concat_slstrings (char *a, char *b)
The SLang_concat_slstrings
function concatenates two strings,
a
and b
, and returns the result as a hashed string.
Upon failure, NULL
is returned.
A hashed string can only be freed using SLang_free_slstring
.
Never use free
or SLfree
to free a hashed string,
otherwise memory corruption will result.
SLang_free_slstring, SLang_create_slstring
Create a hashed string
char *SLang_create_static_slstring (char *s_literal)
The SLang_create_static_slstring
creates a hashed string from
the string literal s_literal
and returns the result. Upon
failure it returns NULL
.
char *create_hello (void)
{
return SLang_create_static_slstring ("hello");
}
This function should only be used with string literals.
SLang_create_slstring, SLang_create_nslstring
Allocate some memory
char *SLmalloc (unsigned int nbytes)
This function uses malloc
to allocate nbytes
of memory.
Upon error it returns NULL
; otherwise it returns a pointer to
the allocated memory. One should use SLfree
to free the
memory after use.
SLfree, SLrealloc, SLcalloc
Allocate some memory
char *SLcalloc (unsigned int num_elem, unsigned int elem_size)
This function uses calloc
to allocate memory for
num_elem
objects with each of size elem_size
and returns
the result. In addition, the newly allocated memory is zeroed.
Upon error it returns NULL
; otherwise it returns a pointer to
the allocated memory. One should use SLfree
to free the
memory after use.
SLmalloc, SLrealloc, SLfree
Free some allocated memory
void SLfree (char *ptr)
The SLfree
function deallocates the memory specified by
ptr
, which may be NULL
in which case the function does
nothing.
Never use this function to free a hashed string returned by one of
the family of slstring
functions, e.g.,
SLang_pop_slstring
.
SLmalloc, SLcalloc, SLrealloc, SLmake_string
Resize a dynamic memory block
char *SLrealloc (char *ptr, unsigned int new_size)
The SLrealloc
uses the realloc
function to resize the
memory block specified by ptr
to the new size new_size
.
If ptr
is NULL
, the function call is equivalent to
SLmalloc(new_size)
. Similarly, if new_size
is zero,
the function call is equivalent to SLfree(ptr)
.
If the function fails, or if new_size
is zero, NULL
is
returned. Otherwise a pointer is returned to the (possibly moved)
new block of memory.
SLfree, SLmalloc, SLcalloc