Entry line left in C troubles

I wanted to create a string-filling function to use left-padding a binary representation with zeros, indented to a specific byte size. I tried printf at first, but did not allow the string to be zero-filled and was not flexible.

I came 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 a simply loose string (e.g. 0100100001). Is my arithmetic a pointer wrong, am I copying to the wrong place, or is there something else that I missed that prevents this from working?

+3
source share
3 answers

There are many misconceptions and some other problems:

  • memset() does not change padded

; memset() , padded.

reset 'padded -= ssize + 1 undefined , .

:

strcpy(padded + bits - ssize, string);

:

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

strcpy() , .

, malloc() , , . calloc().

, memset() null, .

, strncpy(), , , , , . , strcpy() .

- const char * . (static . , , .)

#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);
}

, , ssize > bits (: assert() ). , . : NOT NOT . , ; . ; , , . !

:

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;
}

, , undefined ( , X ), :

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

, "X" strncpy()! ssize + 1, strcpy()... ...

+2

, , , strpadleft ( "0100100001", "0", 4);

ssize 10

32

33 .

/ :

   ..............................................
ma 0000000000000000000000000000000000000000000000
ed xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
md 0000000000000000111111111111111222222222222222
yr 0123456789ABCDEF123456789ABCDEF123456789ABCDEF
              |                               |
              |                               padded allocation end
              padded allocation start

, padded 0x0B.

memset '0'.

   ...........00000000000000000000000000000000...
ma 0000000000000000000000000000000000000000000000
ed xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
md 0000000000000000111111111111111222222222222222
yr 0123456789ABCDEF123456789ABCDEF123456789ABCDEF
              |                               |
              |                               "padded" allocation end
              "padded" allocatoin start

11 , padded 0x00.

( , , . , , padded . .)

   0100100001n00000000000000000000000000000000... (Note: 'n' represents the null character here)
ma 0000000000000000000000000000000000000000000000
ed xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
md 0000000000000000111111111111111222222222222222
yr 0123456789ABCDEF123456789ABCDEF123456789ABCDEF
   |          |                               |
   |          |                               "padded" allocation end
   |          "padded" allocatoin start
   "padded" now points here 

, , , .

, , , , .

, , .

+3

:

padded -= ssize + 1;
strncpy(padded, string, ssize);           /* Replace for example bits 16->32 with representation*/

char *data = padded + (bits - ssize);
strncpy(data , string, ssize);           /* Replace for example bits 16->32 with representation*/
padded [bits] = '\0';

padded, , , phihag , memset padded.

+1

Source: https://habr.com/ru/post/1778536/


All Articles