Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 04 Aug 2012 11:42:16 -0500 Message-ID: <501D50E6.532@mindspring.com> Date: Sat, 04 Aug 2012 12:42:14 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Trying to figure out how to dynamically re-allocate strings while concatenating References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 114 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.87 X-Trace: sv3-1xaQVXCpCmHdGDclyvK7/b2DBkPOHbUj1aOQgUB+HzOmF4hfNES6fKiG9hQ2SkF6fqVt/oybcfp8bXK!L+nMuz6kvEK/iJkk+4XJ/rC0dcybD/5ZTq4qE1jcZ9BkjXIUfpuomXmFRlKXcrHCYpjUCZXXZCeh!P2I6cJ6o X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3937 Xref: csiph.com comp.lang.c:24214 Shriramana Sharma wrote: > > Hello. I am not a professional programmer > and only program now and then. > So please forgive me if my question sounds silly or naive. > > I am trying to find out how to dynamically lengthen a string > while concatenating in C, > as it is easily possible to do so in C++ or Python > (two other languages I have used). > > I patched up the following code: > > [code] > # include > # include > # include > > void strappend ( char * a, char * b ) > { > a = ( char * ) realloc ( a, strlen ( a ) + strlen ( b ) ) ; > strcat ( a, b ) ; > } > > void main ( void ) > { > char * name = "my name is" ; > strappend ( name, " this or " ) ; > strappend ( name, "that." ) ; > printf ( "%s\n", name ) ; > } > [/code] > > But it aborts with an error saying: > "./strappend: realloc(): invalid pointer: 0x000000000040073c ***". > No matter how many times I execute it > that hex number remains the same. > > Can anyone kindly point me to what I am doing wrong with realloc? > I have never really used malloc/realloc stuff > so I am probably botching things up somewhere but I'm not sure where. One thing that nobody else has mentioned, is that it is good programming practice to assign the return value of realloc to a temporary pointer first, and only to assign that value to your working pointer if the temporay pointer has been tested and found to be not a null pointer. Otherwise you lose the value of the pointer to the allocated memory, which can cause a memory leak. A memory leak causes allocated memory to not be able to be deallocted while the program is running. This is especially bad in a program which does a lot of allocating and freeing. It is entirely inconsequential in a small program like yours, but it is good to learn to write using good programming practices. Freeing allocated memory explicitly with a call to free, is a good habit to learn. /* BEGIN new.c */ #include #include #include char * str_append(char *a, char *b) { a = realloc(a, 1 + strlen(a) + strlen(b)); if (a != NULL) { strcat(a, b); } return a; } int main (void) { char *temp = NULL; char *name = malloc(1); if (name != NULL) { *name = '\0'; temp = str_append(name, "my name is") ; } if (temp != NULL) { name = temp; temp = str_append(name, " this or ") ; } if (temp != NULL) { name = temp; temp = str_append(name, "that.") ; } if (temp != NULL) { name = temp; } if (name != NULL) { puts(name); } free(name); return 0; } /* END new.c */ -- pete