1

I wonder, is there another solution to modify to string literals and Is the solution really valid and optimal?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *strdup(const char *src) {
char *dst = malloc(strlen (src) + 1);   
Space for length plus nul
if (dst == NULL) return NULL;         
No memory
strcpy(dst, src);                      // Copy the characters
return dst;                            // Return the new string
}

int main( void )

{

    const char* s1= " hello ";  // A constant character pointer pointing to the string " serhat".
    char* s2= strdup(s1);
    s2[1]= 'b';
    printf("%s", s2);

    
}
6
  • 4
    Other than not freeing the memory allocated and the lack of proper comments this looks fine. Best to pick a name for the function that won't clash with any library provided function of the same name. Not sure what sort of answer you are looking for. Commented Nov 18, 2023 at 17:51
  • 2
    I'd suggest using memcpy() instead of strcpy() because you already know the length (or would if you captured the return value of strlen() in a variable) Commented Nov 18, 2023 at 17:58
  • Actually I heard that modern compilers add automatically free that allocated memory. So I didn't add but you are right. Commented Nov 18, 2023 at 18:26
  • @Paroz The operating system will generally reclaim all memory when the program exits but it isn't anything to do with the compiler and does nothing to keep you from leaking or running out of memory while the program is running, Commented Nov 18, 2023 at 19:39
  • There's already a library function in string.h called strdup(). See en.cppreference.com/w/c/experimental/dynamic/strdup. If you don't mean to replace it with your own custom function, you should use a different name. Commented Nov 18, 2023 at 20:29

1 Answer 1

1

is there another solution to modify to string literals...

The code does not modify a string literal, it merely modifies an allocated copy. You could use a simpler approach with an initialized array of char:

#include <stdio.h>

int main() {
    char s1[] = " hello ";
    s1[1] = 'b';
    printf("%s\n", s1);
    return 0;
}

Is the solution really valid and optimal?

The code posted is not valid: it does not compile and it is not optimal as you compute the string length twice: once in strlen and another time in strcpy.

Also note that strdup is a reserved name for a POSIX standard function that is semantically equivalent to your function and this strdup() function will finally be included in the C Standard in its upcoming version known as C23.

Finally s2 should be freed after use for good style.

Here is a corrected version.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *my_strdup(const char *src) {
    size_t size = strlen(src) + 1;
    char *dst = malloc(size);
    if (dst == NULL)
        return NULL;
    else
        return memcpy(dst, src, size);
}

int main(void) {
    const char *s1= " hello ";
    char *s2 = my_strdup(s1);
    if (s2) {
        s2[1] = 'b';
        printf("%s\n", s2);
        free(s2);
    ]
    return 0;
}
Sign up to request clarification or add additional context in comments.

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.