Re: General method for dynamically allocating memory for a string



Frederick Gotham <fgothamNO@xxxxxxxx> wrote:

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".

So so <stdlib.h>, <string.h>, and...

#include <stdio.h> // for printf()

.... <stdio.h>.

It is not often necessary to #include <stddef.h>. It is very rarely
necessary to do so just for the declaration of size_t. All headers whose
functions need it also declare it themselves, and it's a rare program
that does not use one of those headers.

char * substr(char * string, size_t start, size_t end);

First parameter should be "pointer to const".

No, it shouldn't, because that's not how the function is defined. It may
be a wise idea to _define_ it as a pointer to const char, or even a
const pointer to const char, and then also declare it as such, but
"should" is too strong.

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.

The reasoning given in the comment is also bogus. It is a very _bad_
programming habit to start expecting that you can free pointers twice.

// 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.

Why?

Richard
.



Relevant Pages

  • declaring functions moving input pointer
    ... There is already one function processing pointer values this way. ... void consume (const char **); ... might declare `consume' like this. ...
    (comp.lang.c)
  • Re: pointers to constant characters and constant pointers to characters
    ... cdecl> declare a as pointer to char ... cdecl> declare b as const pointer to char ... cdecl> declare c as pointer to const char ...
    (comp.lang.c)
  • Re: Doubt about arrays name
    ... "const" to string array passed to the function (so to avoid any change ... of this pointer). ... void chartobyte (const char *s) ... If you wanted to declare a const pointer to char (so the pointer ...
    (comp.lang.c)
  • Re: I need some basic C++ help
    ... >> I am trying to declare a string variable as an array of char's. ... the expression you gave was trying to assign an address to array. ... Oops, sorry, I should have said "array of const char" instead of "const char ... a pointer cannot be assigned to an array... ...
    (comp.lang.cpp)
  • Re: How do I stop a TSP written in Delphi crashing on Add Provider?
    ... > You cast mod to a pointer to TCHAR but you declare it a char. ... >> Dim buffer As String ... >> I can successfully use the code above to access a functions in the Delphi ...
    (microsoft.public.win32.programmer.tapi)