Problems with Heap Corruption - C

Viewed 69

I am making a "simple" print out string, append string and remove section from a string. The append and new string works sometimes, sometimes it outputs nothing. When i do:

char * temp = malloc(newSize);

It just stops outputting anything.

I have commented everything out in sections, trying to find the problem. Can't seem to find the problem, but google keeps coming up with "Heap Corruption".

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

typedef struct {
    char * data;
    int length;
} String;

int str_getLength(const char * characters)
{
    int index = 0;
    while (1)
    {
        if (characters[index] == '\0') break;
        index++;
    }
    return index;
}

String str_new(const char * characters) 
{
    String result;
    result.length = str_getLength(characters);
    result.data = malloc(result.length);
    memcpy(result.data, characters, result.length);
    return result;
}

void str_append(String * str, const char * characters) 
{
    int charsLength = str_getLength(characters);
    str->data = realloc(str->data, charsLength);
    for (int i = 0; i < charsLength; i++) {
        str->data[i + str->length] = characters[i];
    }
    str->length = str->length + charsLength;
}

void str_remove(String * str, int startIndex, int endIndex) 
{
    if (startIndex < 0 || endIndex > str->length || endIndex < startIndex) {
        return;
    }
    int chunkSize = endIndex - startIndex;
    int newSize = str->length - chunkSize;

    char * temp = malloc(newSize);
    // for (int i = 0; i < str->length; i++) 
    // {
    //  if (i < startIndex || i > endIndex) {
    //      temp[i] = str->data[i];
    //  } 
    // }

    // free(str->data);
    // str->length = newSize;
    // str->data = temp;
}
}

int main() 
{
    String str = str_new("Hello, ");
    printf("%s\n", str.data);

    str_append(&str, "this is my first C application.");
    printf("%s\n", str.data);

    str_remove(&str, 0, 3);
    printf("%s\n", str.data);

    free(str.data);

    return 0;
}

I expected it to output a modified string, it doesn't and sometimes it outputs nothing. I am a beginner, sorry if it's a quick fix.

2 Answers

There are two problems with your reallocation. First of all, you're not assigning the result of the realloc to str->data, so if the memory was reallocated to a different place, tr->data points to invalid memory afterwards. Second, you're not adding the sizes of the string and the appended part, you're just taking the size of the part that you're appending.

This here

realloc(str->data, charsLength);

Should be:

str->data = realloc(str->data, charsLength + str->length + 1);

Apart from the blaze answer. There are few more problems.

// for (int i = 0; i < str->length; i++) 
// {
//  if (i < startIndex || i > endIndex) {
//      temp[i] = str->data[i];
//  } 
// }

You will access out of bound for temp. You need to maintain separate index for temp.

char * temp = malloc(newSize+1);
int k=0;
for (int i = 0; i < str->length; i++) 
 {
  if (i < startIndex || i > endIndex) {
      temp[k++] = str->data[i];
  } 
}
 temp[k] = '\0'; 
 free(str->data);
 str->length = newSize;
 str->data = temp;

And

You are not null terminating the string after append.

str->data = realloc(str->data, str->length + charsLength +1); //current length + new length + \0
for (int i = 0; i < charsLength; i++) {
    str->data[i + str->length] = characters[i];
}
 str->data[i + str->length] = '\0'; //null terminate the new string
str->length = str->length + charsLength;
Related