Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting access violation while trying to copy string

I wrote very simple function that should copy n characters from source buffer to destination buffer. However, program crashes with write access violation exception. I am not sure where is the problem.

int main()
{
    char* str1 = "onefive";
    char* str2 = "two";

    char* dest = memcpy(str1, str2, strlen(str2));
    // expected output is "twofive"
}
void* memcpy(void* dest, const void* src, size_t n)
{
    char* cdest = (char*)dest;
    const char* csrc = (char*)src;

    for (int i = 0; i < n; i++)
        cdest[i] = csrc[i]; // write access violation (cdest)
    return cdest;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Literals and mutability

It is an artifact of the language (due to its age) that

char * s = "Hello world";

is valid. The "Hello world" is a string literal, which is immutable. In order to not break old code, the C Standard still allows this syntax instead of requiring the more correct:

const char * s = "Hello world";

In order to get a mutable array of characters (aka string), you need to signal it with the following syntax:

char s[] = "Hello world";

This causes you to get a copy of the immutable literal string in your own local variable s, which is a mutable (non-const) array of twelve characters (a string of 12 characters — the eleven for “hello”, space, and “world” plus the terminating nul character).

String Size

You can modify the content of the local string, but again you only have twelve characters to do it with. To concatenate strings you need an array (string) large enough to contain the results. Hence, you should specify a string size constraint at declaration:

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

int main(void)
{
  char s[100] = {0};  // the target string, non-const/mutable, 100 characters max!

  const char * a = "Hello";  // reference to immutable string
  char b[] = "World";        // mutable copy of a string

  strcpy( s, a );            // copy to our large local string s
  strcat( s, " " );          // (append a space to separate words)
  b[0] = 'w';                // changes b’s “World” to “world” (’cause b is a copy!)
  strcat( s, b );            // append a copy of b to the end of s
  strcat( s, "!" );          // some umph

  printf( "%s\n", s );       // ta-da!

  return 0;
}

Remember, arrays are just a whole bunch of values in a row. The values can be either const or not, and you must have enough room to keep all the values you wish to use.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading