Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <501D50E6.532@mindspring.com> (permalink) |
|---|---|
| Date | 2012-08-04 12:42 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.lang.c |
| Subject | Re: Trying to figure out how to dynamically re-allocate strings while concatenating |
| References | <eb47af15-f8ff-4737-983d-3f54307c1081@googlegroups.com> |
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 <stdio.h>
> # include <stdlib.h>
> # include <string.h>
>
> 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Trying to figure out how to dynamically re-allocate strings while concatenating Shriramana Sharma <jamadagni@gmail.com> - 2012-08-04 07:12 -0700
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-08-04 16:17 +0200
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-08-04 16:22 +0200
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Shriramana Sharma <jamadagni@gmail.com> - 2012-08-04 07:25 -0700
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-04 16:04 +0100
Re: Trying to figure out how to dynamically re-allocate strings while concatenating "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-04 16:29 +0200
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-04 10:52 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 12:27 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-04 13:56 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 17:12 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 12:42 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-04 14:00 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Keith Thompson <kst-u@mib.org> - 2012-08-04 17:00 -0700
Re: Trying to figure out how to dynamically re-allocate strings while concatenating pete <pfiland@mindspring.com> - 2012-08-04 20:48 -0400
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-05 09:22 +0200
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-05 12:49 +0100
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-05 17:12 +0200
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Keith Thompson <kst-u@mib.org> - 2012-08-05 13:17 -0700
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-05 22:53 +0100
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Shriramana Sharma <jamadagni@gmail.com> - 2012-08-04 19:20 -0700
Re: C Vs Asm i use for cat function "io_x" <a@b.c.invalid> - 2012-08-05 08:15 +0200
Re: C Vs Asm i use for cat function "BartC" <bc@freeuk.com> - 2012-08-05 10:22 +0100
Re: C Vs Asm i use for cat function "Heinrich Wolf" <invalid@invalid.invalid> - 2012-08-05 17:00 +0200
Re: C Vs Asm i use for cat function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-05 16:20 +0100
Re: C Vs Asm i use for cat function "io_x" <a@b.c.invalid> - 2012-08-05 19:51 +0200
Re: C Vs Asm i use for cat function "BartC" <bc@freeuk.com> - 2012-08-05 21:04 +0100
Re: Trying to figure out how to dynamically re-allocate strings while concatenating Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-08-10 10:45 +0300
csiph-web