passing pointers [C]

From: Stephen Ramsay (sramsay_at_uga.edu)
Date: 07/08/04


Date: Thu, 08 Jul 2004 20:17:30 GMT

I've got a problem that probably stems from some fundamental
misunderstandings on my part.

My app has to concatenate strings to build up a bunch of SQL queries
(some elements of which aren't known until runtime. Now, I realize that
I could malloc some space, concatenate the first two, malloc some more,
concatenate again, but this seems less than elegant. So, I thought I
would create a separate cat_string function that would take in the
strings, do that malloc, and return a new string.

The problem with this implementation, is that while it works (compiles
and produces the expected behaviour), it's leaking memory all over the
place -- I assume because it's really creating new copies and failing to
de-allocate the old ones.

Be that as it may, it seems to me that the most efficient way to do this
would be to keep passing in the current string (the one that is being
gradually built up) and realloc-ing it's memory block. When all the
concatenations are completed, it could then just free that one block.

In my limited understanding of C, I start to think, "Hmm. Something to
do with passing addresses, um, help . . ."

So, here's some (toy) code. It's really a mess -- seg faults every time
-- and it's really just the latest iteration of "well, let's try this."
It should, though, give you an idea of what I'm trying to do. If anyone
can give me a hint, I'd really appreciate it.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static void cat_string(char **left, char *right);

int main(void)
{
        char *target = "";

        char *string1 = "Welcome ";
        char *string2 = "to ";
        char *string3 = "C ";
        char *string4 = "Programming ";
        char *string5 = "for ";
        char *string6 = "the confused.";

        cat_string(&target, string1);
        cat_string(&target, string2);
        cat_string(&target, string3);
        cat_string(&target, string4);
        cat_string(&target, string5);
        cat_string(&target, string6);

        printf("%s\n", target);

        free(target);

        return 0;

}

static void cat_string(char **left, char *right)
{

        realloc(*left, (strlen(*left) + strlen(right) + 1)
                        * sizeof(char *));
        strcat(*left, right);

}

Thanks,

Steve

-- 
Stephen Ramsay
Department of English
University of Georgia
web: http://cantor.english.uga.edu/


Relevant Pages

  • Re: passing pointers [C]
    ... >I could malloc some space, concatenate the first two, malloc some more, ... >strings, do that malloc, and return a new string. ... Since you only call realloc, there is no way for old allocations not ...
    (alt.comp.lang.learn.c-cpp)
  • Re: was: Current Uses for Fortran ??
    ... > Strange because no other programming languages do it. ... > numbers and strings. ... I don't like + for concatenate since I believe that concatenate ... no deficiencies and the other way is to make it so complicated ...
    (comp.lang.fortran)
  • Re: Best practices for efficient FP string building in CL?
    ... CONCATENATE fast: ... (defun smarter-concatenate-strings (strings) ... (defun not-so-clever-concatenate (strings) ...
    (comp.lang.lisp)
  • Re: Building HTML - String vs Array
    ... Concatenate Strings: ... Build Using Arrays: ... Use either arrays or string concatenation, whichever suits. ...
    (comp.lang.javascript)
  • Re: Console.WriteLine(s) Is Missing Line Terminator?
    ... If I don't know if those are strings or numerics, ... It's the ease with which it encourages lazy programming that puts me off ... Matius is arguing strongly that '+' should not be used to concatenate ... reasons", to me it's like saying "Say Hasta La Vista to ...
    (microsoft.public.dotnet.general)