0

Here is my ARM7 assembly snippet

.global strCopy
.text
strCopy:

strCopyloop:    
    LDRB R2, [R1], #1
    STRB R2, [R0], #1

    CMP R2, #0
    BNE strCopyloop

    Bx LR

Here is the C file that is using this function

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

extern void strCopy(char* strTo, const char* strFrom);
int main(){

        const char* str1 ="This one";
        char* str2;


        strCopy(str2,str1);


        return 0;
}

I cant for the life of me figure out why its giving me a segmentation fault.

1 Answers1

-1

You need to create space for str2

This can be done using the malloc function.

str2= char* malloc(strlen(str1)+1)

You add the 1 for null character at the end of the string. This shows the end of the string. You can now go ahead and use the strcpy function to Scipy the 2 strings.

  • It’s always important to create space for your data data when using pointer. This is required for all kind of pointers and not just strings. Especially when you are dealing with complex structures. We usually use the function sizeof if we explicitly do not know the size of the particular data type. – chintan thakrar Mar 01 '20 at 03:50
  • If you weren't just testing a `strcpy` implementation: after getting the length with `strlen`, you could use `memcpy` which is more efficient than `strcpy`. – Peter Cordes Mar 01 '20 at 03:54