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 6928145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:08:47+00:00 2026-05-27T11:08:47+00:00

I am facing certain problems with strlen right now(there are many cases where I

  • 0

I am facing certain problems with strlen right now(there are many cases where I read files and the string is not zero terminated). So I was thinking of making an assembly routine to calculate the length of my strings. What I would do is just go backwards from the end of the string until I encounter my first character and then calculate the length of the string. In fact I already have one that I wrote some time ago when I was writing assembly programs.

Now, I would like to know, is there any reason why I shouldn’t do this? Any particular advantages that I would be losing out on?

Another alternate would be just make each member of my character array to null. I could do this in assembly 4 bytes at a time, or even through a simple for loop.

Keep in mind that I am talking about considerable size arrays[64k]. Considerable in the length that the processing has to be really quick, since I need to display the file as soon as the user selects it.

EDIT:
To clarify, by saying that I know that I know the length of the string, I mean:

char* buffer = new char[length];

I know length. But when I fill this buffer, I do not know the exact length up till which it has ascii characters. When I use strlen, it does not give me the current length. Basically the length can be 500, but there can be only 5 valid characters inside it and the rest 495 could be garbage values.

  • 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-05-27T11:08:48+00:00Added an answer on May 27, 2026 at 11:08 am

    Now, I would like to know, is there any reason why I shouldn’t do this?

    Yes. If you already have the end byte of the string and its beginning, then you know it’s length:

    const char *begin = //beginning.
    const char *end = //last byte of string.
    ptrdiff_t stringLength = (end - begin) + 1;
    

    The +1 is because end points to the last byte of the string. If end were one-past the end, you wouldn’t need the +1. There’s no need for any routine to calculate something you already know.

    Note that this assumes that the string is ASCII or some other single-byte-per-character encoding. If you’re using a Unicode encoding of some kind (UTF-8, UTF-16, etc), then you’ll have to do scan the string to figure out how many codepoints it is.

    Of course, if it is a Unicode encoding, then the question of what you mean exactly by “length” needs to be addressed. The “length” could be “number of codepoints,” “number of distinct graphemes,” or even “number of code units in the encoding.”

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

Sidebar

Related Questions

i'm facing a problem right now, i need to count the number of time
I am facing a problem to match and replace certain words, not contained in
I'm facing a problem with an Internet application I'm working on right now (programming
just had a general question about how to approach a certain problem I'm facing.
Im facing some problems, I looked around in the forum and didnt find any
When facing the problem of validating a property in a JSF2 application there are
I am developing a binary search tree in java. But i am facing certain
I have an ArrayList of type String that contain certain values. I want to
Im facing a problem where i want to schedule a certain java application to
I am facing the very strange problem in HTML/CSS.I am using chrome 12.0.742.122. Now

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.