![]() |
Trying to figure out how to dynamically re-allocate strings while concatenating
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>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. Thanks! |
Re: Trying to figure out how to dynamically re-allocate strings whileconcatenating
Le 04/08/2012 16:12, Shriramana Sharma a écrit :
> a = ( char * ) realloc ( a, strlen ( a ) + strlen ( b ) ) ; .....................................^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ +1 for the ending \0 |
Re: Trying to figure out how to dynamically re-allocate strings whileconcatenating
Le 04/08/2012 16:12, Shriramana Sharma a écrit :
> char * name = "my name is" ; Oh, and you can't do that, too. char * name = strdup("my name is"); Otherwise the string will be a constant, not a valid malloc() pointer. (building with -Werror=write-strings might be a good idea) |
Re: Trying to figure out how to dynamically re-allocate strings while concatenating
Hey very nice thank you very much for that guidance! :-) It's now working.
|
Re: Trying to figure out how to dynamically re-allocate strings while concatenating
"Shriramana Sharma" <jamadagni@gmail.com> schrieb im Newsbeitrag news:eb47af15-f8ff-4737-983d-3f54307c1081@googlegroups.com... > 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>Code:
You are reallocating memory in strappend, but not freeing it. This short program is ok. The reallocated "name" is still in reach and automatically freed on exiting your program. But be careful in a more complex program! I doubt that realloc is allowed on the initial value of "name", since it was not malloc'ed. In fact it is a pointer aiming at constant memory segment. Maybe char * name = strdup("my name is"); works better. > 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. > > Thanks! |
Re: Trying to figure out how to dynamically re-allocate strings whileconcatenating
On 8/4/2012 10:12 AM, 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>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. The comp.lang.c Frequently Asked Questions (FAQ) page at <http://www.c-faq.com/> would make good reading. Section 4 is all about pointers -- in fact, Question 4.8 specifically addresses one of the problems in your code. The C programming languages has many strengths, but hand-holding is not among them. You need to be something of a shade-tree mechanic or tinkerer (or even gearhead) to use C well. If you're an occasional programmer and not much interested in fiddly low-level details, you may be happier with a higher-level, more coddling language. I think Mark Twain wrote that one lifetime is not long enough to master German; something similar could be said of C. -- Eric Sosman esosman@ieee-dot-org.invalid |
Re: Trying to figure out how to dynamically re-allocate strings while concatenating
Shriramana Sharma <jamadagni@gmail.com> writes:
> Hey very nice thank you very much for that guidance! :-) It's now > working. Take care. Many C programs appear to work, right up to the point where they don't! I hope you have taken all the advice, not just the advice in the message to which you replied. Speaking of which, strdup is not a standard C function, so you should check that it will available everywhere your program might run (or at least everywhere your program might be compiled and linked). -- Ben. |
Re: Trying to figure out how to dynamically re-allocate strings while concatenating
Shriramana Sharma <jamadagni@gmail.com> 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 <stdio.h> #include <stdlib.h> #include <string.h> 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 ); }} |
Re: Trying to figure out how to dynamically re-allocate strings whileconcatenating
On 8/4/2012 12:27 PM, pete wrote:
>[...] > 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. ... which is harmless, defined as a no-op. -- Eric Sosman esosman@ieee-dot-org.invalid |
Re: Trying to figure out how to dynamically re-allocate strings whileconcatenating
On 8/4/2012 12:42 PM, pete wrote:
>[...] > 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. Note that his realloc() call and value assignment were inside a function that received the pointer value as an argument. That is, the caller still owns (or can compute) a pointer to the allocated memory. Still, (1) the O.P.'s code was pretty badly messed up already and we're all in the business of imagining different possible corrections to it, and (2) your advice to assign the value of realloc(p,...) to something other than p is usually excellent. -- Eric Sosman esosman@ieee-dot-org.invalid |
| All times are GMT. The time now is 06:49 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.