passing pointers [C]
From: Stephen Ramsay (sramsay_at_uga.edu)
Date: 07/08/04
- Next message: Robert W Hand: "Re: Default constructors, passing argument"
- Previous message: Chad J McQuinn: "Re: Default constructors, passing argument"
- Next in thread: Robert W Hand: "Re: passing pointers [C]"
- Reply: Robert W Hand: "Re: passing pointers [C]"
- Reply: Barry Schwarz: "Re: passing pointers [C]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: Robert W Hand: "Re: Default constructors, passing argument"
- Previous message: Chad J McQuinn: "Re: Default constructors, passing argument"
- Next in thread: Robert W Hand: "Re: passing pointers [C]"
- Reply: Robert W Hand: "Re: passing pointers [C]"
- Reply: Barry Schwarz: "Re: passing pointers [C]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|