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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:05:01+00:00 2026-05-12T19:05:01+00:00

I’ve been instructed to write a model strdup by creating a String struct on

  • 0

I’ve been instructed to write a model strdup by creating a String struct on the heap the holds a copy of the source. I think I have successfully coded the strdup, but I’m not sure if I’ve created a Struct on the heap…

typedef 
struct String {
    int length;
    int capacity;
    unsigned check;
    char ptr[0];
} String;

char* modelstrdup(char* src){
    int capacity =0, length=0, i = 0 ;
    char *string;
    while ( src[length] != '\0'){
        length++;
    }
    capacity = length;
    string = malloc(sizeof(String) + capacity + 1);
    while ( i < length ){
        string[i] = src[i];
        i++;
    }
    string[i+1] = '\0';

    return string;
}   
  • 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-12T19:05:01+00:00Added an answer on May 12, 2026 at 7:05 pm

    Yes, you’ve created a struct on the heap. You haven’t populated it correctly, and you are going to face problems deleting it – I’m not sure whether the homework covered that or not. As it stands, you’re more likely to get memory corruption or, if you’re lucky, a memory leak than to release one of these strings.

    Code that works with standard C89 and C99

    Your code, somewhat fixed up…

    typedef 
    struct String {
        int length;
        int capacity;
        char *ptr;
    } String;
    
    char* modelstrdup(char* src){
        int length = strlen(src);
        char *space = malloc(sizeof(String) + length + 1);
        //String *string = space;  // Original code - compilers are not keen on it
        String *string = (String *)space;
        assert(space != 0);
        string->ptr = space + sizeof(String);  // or sizeof(*string)
        string->length = length;
        string->capacity = length + 1;
        strcpy(string->ptr, src);
        return string->ptr;
    }
    

    This code will work in C89 as well as C99 (except for the C99/C++ comments). You can probably optimize it to work with the ‘struct hack’ (saves a pointer in the structure – but only if you have a C99 compiler). The assert is sub-optimal error handling. The code doesn’t defend itself against a null pointer for input. In this context, neither the length nor the capacity provides any benefit – there must be other functions in the suite that will be able to make use of that information.

    As already intimated, you are going to face problems deleting the string structure when the value handed back is not a pointer to the string. You have some delicate pointer adjustments to make.


    Code that works with standard C99 only

    In C99, section 6.7.2.1 paragraph 16 describes ‘flexible array members’:

    As a special case, the last element of a structure with more than one named member may
    have an incomplete array type; this is called a flexible array member. With two
    exceptions, the flexible array member is ignored. First, the size of the structure shall be
    equal to the offset of the last element of an otherwise identical structure that replaces the
    flexible array member with an array of unspecified length.106) Second, when a . (or ->)
    operator has a left operand that is (a pointer to) a structure with a flexible array member
    and the right operand names that member, it behaves as if that member were replaced
    with the longest array (with the same element type) that would not make the structure
    larger than the object being accessed; the offset of the array shall remain that of the
    flexible array member, even if this would differ from that of the replacement array. If this
    array would have no elements, it behaves as if it had one element but the behavior is
    undefined if any attempt is made to access that element or to generate a pointer one past
    it.

    106 The length is unspecified to allow for the fact that implementations may give array members different
    alignments according to their lengths.

    Using a ‘flexible array member’, your code could become:

    typedef 
    struct String {
        int length;
        int capacity;
        char ptr[];
    } String;
    
    char* modelstrdup(char* src){
        int length = strlen(src);
        String *string = malloc(sizeof(String) + length + 1);
        assert(string != 0);
        string->length = length;
        string->capacity = length + 1;
        strcpy(string->ptr, src);
        return string->ptr;
    }
    

    This code was accepted as clean by GCC 4.0.1 apart from a declaration for the function (options -Wall -Wextra). The previous code needs a cast on ‘String *string = (String *)space;’ to tell the compiler I meant what I said; I’ve now fixed that and left a comment to show the original.


    Using the ‘struct hack’

    Before C99, people often used the ‘struct hack’ to handle this. It is very similar to the code shown in the question, except the dimension of the array is 1, not 0. Standard C does not allow array dimensions of size zero.

    typedef struct String {
        size_t length;
        size_t capacity;
        char ptr[1];
    } String;
    
    char* modelstrdup(char* src)
    {
        size_t length = strlen(src);
        String *string = malloc(sizeof(String) + length + 1);
        assert(string != 0);
        string->length = length;
        string->capacity = length + 1;
        strcpy(string->ptr, src);
        return string->ptr;
    }
    

    Code that uses a GCC non-standard extension to C89 and C99

    The zero-size array notation is accepted by GCC unless you poke it hard – specify the ISO C standard and request pedantic accuracy. This code, therefore, compiles OK unless you get to use gcc -Wall -Wextra -std=c99 -pedantic:

    #include <assert.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef
    struct String {
        int length;
        int capacity;
        char ptr[0];
    } String;
    
    char* modelstrdup(char* src){
        int length = strlen(src);
        String *string = malloc(sizeof(String) + length + 1);
        assert(string != 0);
        string->length = length;
        string->capacity = length + 1;
        strcpy(string->ptr, src);
        return string->ptr;
    }
    

    However, you should not be being trained in non-standard extensions to the C language before you have a thorough grasp of the basics of standard C. That is simply unfair to you; you can’t tell whether what you’re being told to do is sensible, but your tutors should not be misguiding you by forcing you to use non-standard stuff. Even if they alerted you to the fact that it is non-standard, it is not fair to you. C is hard enough to learn without learning tricksy stuff that is somewhat compiler specific.

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

Sidebar

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your assembly variant might be faster, might be slower. What… May 12, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer you can use: mysql connector/net or you can use OleDB May 12, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer few ways with GNU find $ find . -type d… May 12, 2026 at 7:54 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.