What's wrong with my code? I just want to append a single space to the beginning of my string :( http://ideone.com/3EXbA
but I want to get rid of the original string, or at least reuse it
I'm sure there is a much better way to do this...
I think your error is on line 21: unsigned int len_engs = strlen(engs) + 1; you are not taking into account the null character ('\0') that appears at the end of every C string. So you need to allocate "strlen(engs)+2" characters.
nah I took care of that in the malloc part
len_engs is the length of the characters, plus 1 for the nul character
so in malloc, I also add +1 to the len_engs to make space for the space
but you are padding it with an extra space, so you need another character
which is why I've added +1 to the len_engs when calling malloc
1 more byte for the space
oh - sorry - I didn't read ahead to the malloc part. let me re-read the code.
yeah I think the real trouble in my code is at the very end :)
why are you "free"ing at line 29?
I want to delete the original string
but the original string is allocated on the stack
right: the original string may not be malloc()ed so there's my problem :(
what do I do?
you need to define the responsibility of your function. if it is expected to be called only with malloc'ed strings then you cannot call it with a string allocated on the stack. it is generally not good practice to write code like this - which is why I don't use C anymore and instead use C++ where you can make use of constructors/destructors, smart pointers, etc to manage memory in a much cleaner way.
:( right... this would be easier and cleaner if I used some other way of implementing strings.
but I wrote my program with this function in mind for accepting user input as strings: http://ideone.com/SUnx0
but I could rewrite padstring in a much cleaner way :-D
yes - strncpy makes it cleaner, but I still don't like writing functions that malloc. the problem you run into is who is responsible for freeing this memory? do you HAVE to code in C or can you use some other language like C++ or Java?
maybe you can rewrite it much better than I could :-D
I want to eliminate free()ing responsibility from everybody except the calling function which will be a big procedure that just translates an integer into an english sentence. Also I want to use C :-D
last time I coded in C was more than 20 years ago :-D
the only thing I could suggest here is that line 29 could be simplified further to: strcpy(tempengs + 1, engs); since "engs" is null terminated and you have allocated enough space to receive it in tempengs.
sorry line 28 not 29
but I think I should change fprintf(stdout.... ) to fprintf(stderr)
what else can I do to improve my stuff?
I still don't think its a good idea to free the original string in this method - it feels like an unexpected "side-effect". I think it might be better to leave the responsibility of freeing memory to the caller.
good idea
ok - good luck - but I would strongly advise you to move to a higher level language like C++, Java, Scala, etc
or how about I just realloc the original string, and then make a new buffer, write a space to the beginning of the buffer, copy the string to the buffer, rewrite the string with the buffer, and just free the buffer without worrying about the original string?.
but that won't change a thing... :-P
I would just be doing the same thing!
hmmm - again feels like an unexpected side-effect and relies on the original string being malloc'ed
isn't there an easier way to append a space to a string in C? without having to bust my butt implementing strings as something other than ordinary C strings?
I'll just use Java
:-) there probably is a better way of doing this but I would expect it to still involve allocating memory somewhere.
In Java that whole thing is just something like " " + source
but probably behind the scenes in Java it's doing the same thing :-D
exactly - you should use a language that makes it easy for you to express your ideas rather than it getting in the way of what you want to do.
creating a new string object and returning it :-D
all languages eventually get compiled down to the same bits. you need (should) pick a language that makes it as natural as possible for you to express your ideas.
right
You might find this article useful: http://wilsonmar.com/1strings.htm the first section is on "C String Safety"
thanks!
Join our real-time social learning platform and learn together with your friends!