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;
}
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…
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’:
Using a ‘flexible array member’, your code could become:
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.
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: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.