Is it possible to write on memory that was moved away using memmove in C?

70 Views Asked by At

I'm sorry if my code is garbage but I wanted to experiment with string manipulation on an already dynamically allocated string without losing my original pointer so that when I do go ahead and free up the memory outside of the function I won't be freeing the wrong thing.

The algorithm is as follows:

  • Receive a dynamically allocated string containing multiple words
  • If there is an even number of words, set the amount of spaces between each word to 2. Otherwise leave it as 1 space.
    char* evenNumOfWords(char* str)
{
    int spaceCounter = 0; // count the occurrences of space
    int spaceIndices[strlen(str)]; // save the index of all space characters to avoid going through the whole array twice
    char* endPointer = str + strlen(str);
    int length;
    for (int i = 0 ; i < strlen(str) ; i++)
    {
        if(str[i] == ' ')
        {
            spaceIndices[spaceCounter] = i;
            spaceCounter++;
        }
    }
    if (spaceCounter % 2 == 1) { // if there is an odd number of spaces there's an even number of words
        for (int i = 0 ; i < spaceCounter ; i++)
        {
            length = (int)(endPointer - str);
            memmove(str + spaceIndices[i] * sizeof(char) + 1, str + spaceIndices[i] * sizeof(char), length - spaceIndices[i] + 1); // move the string 1 byte forward starting from the place where there's a space
            endPointer++;
            str[spaceIndices[i]] = ' ';
        }
    }
    return str;
}

Perhaps my logic is entirely incorrect but my main question is, can I actually write on the memory that I moved my data away from with memmove? Because I'm getting "code 138 (interrupted by signal 10: SIGBUS)" which after Googling I found it's caused by writing on non-writable memory.

Thanks in advance!

2

There are 2 best solutions below

0
JeremyP On

The only difference between memmove and memcpy is that memmove copies the bytes non destructively. That is to say, if the source and destination areas overlap, memmove will work fine but memcpy may not.

Your problem is likely due to passing a constant string into your function. If you do

foo = evenNumOfWords("the cat sat on the mat");

the string passed in is a literal and likely to be located in a read only segment.

0
Lenny_Medina On

I've made a few edits thanks to suggestions from you guys, thanks a lot.

For starters I changed

spaceIndices[spaceCounter] = i;

to

spaceIndices[spaceCounter] = i + spaceCounter;

then I went ahead and made the function void because I didn't need any return considering I'm passing the string by reference and it's altering it directly from the heap (maybe I'm using the wrong terminology don't crucify me)

And I went ahead and scanned the strings in the console using fgets instead of passing in a string that was pre-baked in the code

To answer my own question, yes you can definitely write on memory that was moved using memmove.