Re: General method for dynamically allocating memory for a string




smnoff wrote:
I have searched the internet for malloc and dynamic malloc; however, I still
don't know or readily see what is general way to allocate memory to char *
variable that I want to assign the substring that I found inside of a
string.

Any ideas?

It would help if you could tell us a bit more about the strings:

(1) Are we allowed to mangle the source string?

(2) Can we assume the source string is going to exist for as long as
you need the substring?

(3) How many substrings do you need at any one instant? How long are
they on average?

-----
You see most malloc()'s have considerable overhead. If we can mangle
the source string, we can return the substring as a pointer into the
source string, with a zero byte at the end of the substring. Which is
economical, unless you need the original string or maybe another
substring might match at substring end + 1?

If the lifetime of the substrings can be determined, it might be
simpler faster and easier to just have a small array of substrings
rather than allocate each darn one on the heap.

.