Does the application freeze when I try to get data from the string structure?

advertisements

i just recently asked a question and got help but i have another problem with my custom c string. everything compiles fine but when i try to get something it freezes, or it could be when i delete it. what am i doing wrong? heres my code:

main.c

#include <stdio.h>
#include "String.h"

int main(int argc, char *argv[]) {
  String *some_string = String_New("hello");
  printf("%s\n", String_CharPtr(some_string));
  String_Delete(some_string);
  return 0;
}

String.h

#ifndef _STRING_H
#define _STRING_H

typedef struct String String;

extern String *String_New(char */*basic_string*/);
extern void String_Delete(String */*string*/);
extern char *String_CharPtr(String */*string*/);
extern unsigned int String_GetLength(String */*string*/);
extern unsigned int String_GetSize(String */*string*/);

#endif

and finally:

String.c

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

#include "String.h"

struct String {
  char *basic_string;
  unsigned int length;
};

String *String_New(char *basic_string) {
  String *temp = NULL;
  temp->length = strlen(basic_string);
  temp->basic_string = (char *) malloc(temp->length + 1);
  strcpy(temp->basic_string, basic_string);
  return temp;
}

void String_Delete(String *string) {
  free(string->basic_string);
  string->basic_string = NULL;
}

char *String_CharPtr(String *string) {
  return string->basic_string;
}

unsigned int String_GetLength(String *string) {
  return string->length;
}

unsigned int String_GetSize(String *string) {
  return string->length + 1;
}


String *temp = NULL;
temp->length = strlen(basic_string);

You haven't allocated memory for temp.