Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:51:15+00:00 2026-06-04T12:51:15+00:00

Its memory wastage vs cpu utilization question. If I want to merge 3 strings:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:51:16+00:00Added an answer on June 4, 2026 at 12:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run an executable and limit its memory and time usage. Of
I'm creating a small cache daemon, and I want to limit its memory usage
due to the size of graph, Grapgviz sometimes exceeds its memory so I want
I'm trying to search for all instances of a null-terminated string the memory of
I want use task_for_pid() and attach to another process and then change its memory
Can I configure my C# application to limit its memory consumption to, say, 200MB?
From pthread_join() man page : When a joinable thread terminates, its memory resources (thread
Bubble sort is O(n) at best, O(n^2) at worst, and its memory usage is
i need to create a function in my application to set its available memory
Does anyone know if its possible to set the amount of memory available in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.