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

  • Home
  • SEARCH
  • 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 3457622
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:52:58+00:00 2026-05-18T09:52:58+00:00

I wanted to create a string padding function for the use of left-padding a

  • 0

I wanted to create a string padding function for the use of left-padding a binary representation with zeros, padding to the defined byte size. I first tried printf but that did not allow zero padding on a string and was not flexible.

I had come up with the following function:

char * strpadleft(char * string, char pad, size_t bytes) {
 size_t ssize = strlen(string);
 size_t bits = bytes * 8;                            
 char *padded = (char *) malloc(bits + 1); /* Bit size + null terminator */
 memset(padded, pad, bits);                /* Fill contents with zeros, leave last null terminator*/
 padded -= ssize + 1;                      /* Rewind back to offset*/
 strncpy(padded, string, ssize);           /* Replace for example bits 16->32 with representation*/
 return padded;
}

/*Example: strpadleft("0100100001", '0', 4); */

Now unfortunately this returns simply the unpadded string (ex. 0100100001). Is my pointer arithmetic wrong, am I copying to the wrong location, or is there something else I missed that does not let this work?

  • 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-18T09:52:58+00:00Added an answer on May 18, 2026 at 9:52 am

    There is major misconception and some other problems:

    • memset() does not change padded

    That is, the variable in your function is not changed; memset() just sets the data that padded points to.

    The purported ‘reset’ operation padded -= ssize + 1 therefore invokes undefined behaviour by accessing memory that you did not allocate.

    Use:

    strcpy(padded + bits - ssize, string);
    

    instead of the two lines:

    padded -= ssize + 1;
    strncpy(padded, string, ssize);
    

    Using strcpy() is safe because you know all the sizes.

    Note that malloc() does not return initialized data, you cannot guarantee that the last allocated byte will be zero. You would have to use calloc() for that.

    Note that the memset() operation does NOT null terminate your string.

    Note that using strncpy(), paradoxically, also does not guarantee null termination and indeed does not null terminate your string even if you got the start position correct. By contrast, using strcpy() does guarantee null termination.

    Working code

    Note the revised interface – using const char * for the first argument. (The static just gets the code to compile under my default compilation flags without a complaint of no prior declaration of the function. You would not use that for a library function declared in a header, of course.)

    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char *strpadleft(const char * string, char pad, size_t bytes)
    {
        size_t ssize = strlen(string);
        size_t bits = bytes * 8;
        char *padded = (char *) malloc(bits + 1);
        assert(ssize < bits);
        memset(padded, pad, bits - ssize);
        strcpy(padded + bits - ssize, string);
        return padded;
    }
    
    int main(void)
    {
        const char *data = "0100100001";
        char *pad = strpadleft(data, '0', 4);
        printf("Data: <<%s>> padded <<%s>> (%d)\n", data, pad, (int)strlen(pad));
        free(pad);
        return(0);
    }
    

    Commentary

    You really need to decide what would be appropriate behaviour if ssize > bits (hint: assert() is not correct). Most probably, though, you would simply duplicate the original string. Note: it would absolutely NOT be acceptable to return a pointer to the original string. The function returns a pointer to a string that must be freed by the application; you must therefore always return an allocated string. Otherwise, your function becomes unusable; the code has to check whether the return value is the same as the argument and not release the return value if it is the same. Yuck!

    Quasi-fixed code

    Demonstrating the lack of null termination in the original code:

    static char * strpadleft(const char * string, char pad, size_t bytes)
    {
        size_t ssize = strlen(string);
        size_t bits = bytes * 8;
        char *padded = (char *) malloc(bits + 1);
        padded[bits] = 'X';  // Overwrite last allocated byte
        memset(padded, pad, bits);
        strncpy(padded + bits - ssize, string, ssize);
        return padded;
    }
    

    With the same test program as before, and relying on undefined behaviour (there was no guarantee that the byte after the X would be a null byte), I got:

    Data: <<0100100001>> padded <<00000000000000000000000100100001X>> (33)
    

    Note that the ‘X’ was not overwritten by the strncpy()! You could fix that with ssize + 1, but why not just use strcpy()…as already stated…

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

Sidebar

Related Questions

No related questions found

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.