Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!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:27:34 -0500 Message-ID: <501D4D74.F6C@mindspring.com> Date: Sat, 04 Aug 2012 12:27:32 -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=iso-8859-1 Content-Transfer-Encoding: 8bit Lines: 37 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.87 X-Trace: sv3-g4QV4n3lPIPXOnkzwmSVg4so6H+r5qyIC4KkwLR0P9x7jKEt29TybGYK2EZ+dI2TtYUncbLaxlRJP4u!qdirHOoTDcDzPpDapyifWTYm+w9nTmuAHrOGcAdDPlf8GOtiMAw98nEPI8672yyuVF/fpFt8g2L9!Udasn63h 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: 2290 X-Received-Bytes: 2431 Xref: csiph.com comp.lang.c:24213 Stefan Ram wrote: > > Shriramana Sharma writes: > >Can anyone kindly point me to what I am doing wrong with realloc? > > The result of an attempt should always be checked according > to me. The »free« below is not really required for this > small program, but would be helpful in many cases, where the > code is repeatedly executed in a program. > > #include > #include > #include > > char * strapp( char * * const a, char const * const b ) > { *a = realloc( *a, 1 + strlen( *a )+ strlen( b )); > if( *a )strcat( *a, b ); > return *a; } > > int main( void ) > { char const * const name_ = "my name is"; > char * name = malloc( strlen( name_ )+ 1 ); > if( name ) > { strcpy( name, name_ ); > if( strapp( &name, " this or " )) > if( strapp( &name, "that." )) > printf( "%s\n", name ); > free( name ); }} I see from your call to the free function, that you intend to free the allocated memory within main. If either call to strapp returns a null pointer, then your call to free, will free a null pointer instead. -- pete