2

Its memory wastage vs cpu utilization question.

If I want to merge 3 strings:

  • Approach 1: Should I take all string lengths (strlen) and then allocate.

    char *s = malloc(strlen(s1)+strlen(s2)+strlen(s3)+1);
    

OR

  • Approach 2: I should assume 1025 and allocate considering the fact that I know the strings will never go beyond 1025.

    #define MAX 1025
    char *s = malloc(MAX);
    

Please suggest.

8 Answers 8

2

Allocate memory for all 3 strings is better.

But if you are 100% (bolded because it is very important) sure that the string never exceed the fixed length, then go ahead. You have to foresee whether you may add things in the future that may exceed the max length, and you have to consider whether user input in any way can overflow the limit.

In case you may not need everything, you can also allocate fixed buffer and truncate the rest of the string if it is too long.

If the string has potential to grow very long (a hundreds of MB), then you shouldn't use this approach. In this case, the solution depends on your application.

Sign up to request clarification or add additional context in comments.

Comments

1

Your malloc line looks fine.

The problem with your other alternative, assuming a fixed length, is that you may change your mind later about the fixed length without changing all of the corresponding problems in your code.

Comments

1

Approach 2: I should assume 1025 and allocate considering the fact that I know the strings will never go beyond 1025.

Yes, this one definitely. But you must be 100% sure. Always prefer automatic-storage allocation when you can.

2 Comments

but is the strlen operation costly ? I mean just incase to be on the safe side i do approach 1. then in that case will it be costlier in any way?
@nimish A few thousand won't slow your program down too much, if you call it once only.
1

Frankly, if you're going to opt for 2, then I'd suggest stack allocation (instead of malloc):

#define MAX 1025
char s[MAX];

1 Comment

IF MAX is as small as a few KB.
1

I am assuming that sum total of all of the strings will be <= 1024?

In this case, you better allocate memory for 1024 (approach 2). This memory you can reuse over and over.

Problem with your approach 1; you have to reallocate memory newly based on the total for that particular instance. This is going to increase your CPU cycles, if you are concerned.

Comments

1

If you really really know that the total size will never go above 1025 or some similar reasonably small value, if possible by all means allocate the string on the stack instead.

OTOH, if you're going to use malloc, you might as well do the little bit of extra work to figure out how much you need to allocate up front. In this case as you go the strlen() route and are worried by efficiency, then at least store the results of the strlen() calls, and use memcpy() to build the combined result string instead of the str*() functions.

Comments

1

How often do you need to perform this operation, how many target strings do you need to hold in memory at once? Chances are it all doesn't matter. However, put it into a function char *my_strconcat3 (const char *s1, const char *s2, const char *s3); that returns the new string. Then you have exactly one location in your code where you need to change it, if the circumstances ever change.

Comments

1

In C99, if the string is not so big, I like this,

 len = strlen(s1)+strlen(s2)+strlen(s3)+1;
 char s[len];

It is called variable-length array in stack which is higher efficiency than malloc who allocates memory from heap.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.