Re: General method for dynamically allocating memory for a string
- From: Frederick Gotham <fgothamNO@xxxxxxxx>
- Date: Thu, 31 Aug 2006 12:41:57 GMT
Randall posted:
#include <stdlib.h> // for malloc() and free()
#include <string.h> // for strncpy()
#include <sys/types.h> // for size_t
Non-standard header. "stddef.h" contains "size_t".
#include <stdio.h> // for printf()
char * substr(char * string, size_t start, size_t end);
First parameter should be "pointer to const".
int main( void ) {
char *str1 = substr("abcdefghijklmnop",2,7);
char *str2 = substr("abcdefghijklmnop",4,15);
Both variables should be const. (You don't want the address to change
before you pass it to "free".)
if( str1 != NULL ) {
printf( "str1: %s\n", str1);
} else {
// Setting a null pointer to zero ensures you
// can delete it more than once (free) without
// undefined behavior. This is a good
// programming habit.
str1 = 0;
}
if( str2 != NULL ) {
printf( "str2: %s\n", str2 );
} else {
// Setting a null pointer to zero ensures you
// can delete it more than once (free) without
// undefined behavior. This is a good
// programming habit.
str2 = 0;
}
As Richard Heathfield pointed out, both "else" clauses are redundant.
free( str1 );
free( str2 );
return 0;
}
/**
* Note: this function will return a newly allocated string. It
* is your responsibility to delete this memory to prevent a leak.
*
* param "string" - the string you want to extract a substring from.
* param "start" - the array index to begin your substring.
* param "start" - the array index to begin your substring.
* param "end" - the array index to terminate your substring.
*
* On Error: this function returns null;
*/
char * substr( char * string, size_t start, size_t end) {
First parameter should be "pointer to const".
// pointer to the substring on the heap
char *subString;
// calculate the total amount of memory needed
// to hold the substring.
// Algo: end - start + null terminator
size_t subStringSize = end - start + 1;
Perhaps you should have written:
size_t len = end - start;
And then added the +1 only when malloc'ing.
// request enough bytes to store the entire
// substring and null terminator.
subString = malloc( subStringSize );
subString = malloc(len+1);
// test to make sure we got the memory
// from malloc
if( subString != NULL ) {
// Note this copies one extra byte (the
// null terminator's spot) which is garbage.
// We have to terminate this string.
strncpy( subString, string + start, subStringSize );
"memcpy" would be more efficient if you were strict about the length of
the string, e.g.:
assert( strlen(string) - istart >= len );
subString[subStringSize] = '\0';
Here you invoke undefine behaviour by writing past the end of the buffer.
subString[len] = 0;
You should _always_ check array indexes.
}
// This will either be NULL if we didn't get the
// memory from malloc or it will have our substring.
return subString;
} // end function substr
--
Frederick Gotham
.
- Follow-Ups:
- Re: General method for dynamically allocating memory for a string
- From: Richard Bos
- Re: General method for dynamically allocating memory for a string
- References:
- Re: General method for dynamically allocating memory for a string
- From: Frederick Gotham
- Re: General method for dynamically allocating memory for a string
- From: Randall
- Re: General method for dynamically allocating memory for a string
- Prev by Date: Re: Holidays are over
- Next by Date: Re: General method for dynamically allocating memory for a string
- Previous by thread: Re: General method for dynamically allocating memory for a string
- Next by thread: Re: General method for dynamically allocating memory for a string
- Index(es):
Relevant Pages
|