Advertising (This ad goes away for registered users. You can Login or Register)

Concat undefined number of strings

Programming on your favorite platform, for your favorite platform? Post here
dj51_Shura
Posts: 36
Joined: Thu Jun 16, 2011 7:35 pm
Location: Granada, Spain
Contact:

Re: Concat undefined number of strings

Post by dj51_Shura » Mon Nov 07, 2011 8:01 pm

Thanks for your replyis, but, I always want a simply and fast function. Not buffers and not pointers. Just the strings you want to concatenate, that's all.

For example:

Code: Select all

FILE *tempfd;
tempfd = fopen(concat(path, ".temp", NULL), "w");
fclose(tempfd);
If the function needs buffers, yes, it can be more clear, but, not useful.

I really need to free the allocated memory before returning, because, if I don't do that, then a lot of memory will be wasted each time I call concat().

If somebody creates a function that detects the number of arguments in variable-arity-functions (something like va_num()...), all would be easier.
Codestation wrote:
(take strdup by example)
I have seen strdup()... it uses malloc or something like that? As I readed here, I also need to use free if I call strdup().

Well, thank you a lot for your suggestions and quick answers. I'm still thinking at it... :geek: xD
Advertising
PSP 6.20 PRO-B7 and TN HEN E Fix firmware

http://www.mhypnok.blogspot.com

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Concat undefined number of strings

Post by m0skit0 » Wed Nov 16, 2011 7:38 pm

Sorry for lateness... :roll:
codestation wrote:It depends, one will need to calculate the lenght of all those strings beforehand everytime you need to call the function to allocate the required buffer or use one big enough to hold all of it. Also i din't see a problem with a function that does allocations by itself if it states clearly that the returned buffer must be freed (take strdup by example).
You're right. I guess it depends on taste then. But strdup is an exception: most C string manipulation function do need a buffer and they don't malloc() inside it. There'd be a reason why this is like this, don't you think? IMHO doing the malloc() and the free() on the same code entity avoid bugs and make the code clearer (e.g. you don't see a free() and go searching which function does the malloc() inside...). Also one should not be afraid of using malloc(), it's C coding style after all (that's why C is sometimes called high-level assembly xD).
codestation wrote:Just calling snprintf(buffer, BUFFER_SIZE, "%s%s%s%s%s", str1, str2, str3, str4, str5); for doing multiple concats is more simpler when you think of it ;)
And you do need a buffer, don't you? ;)
dj51_Shura wrote:I always want a simply and fast function. Not buffers and not pointers.
Pointers are actually faster and maybe they look complicated for you, but definitely they're not complicated, they're quite simple in fact.
dj51_Shura wrote:If the function needs buffers, yes, it can be more clear, but, not useful.
That function need no buffers since you're just opening a file. Also the value returned is length-fixed. But fread() for example does need a buffer because you have to read an unknown size of data. Also a function can only return one value.
dj51_Shura wrote:I really need to free the allocated memory before returning, because, if I don't do that, then a lot of memory will be wasted each time I call concat().
You cannot free the memory before returning because that would make you lose the string data. That's why I'm telling you you have to pass a buffer you allocated outside the function ;)
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming”