-1

The strcpy line in the code below is throwing this error:

C interpreter runtime error: Memory access violation received.

How can I fix this error?

int i = 1, len;
char msg[2000];

//I have 2 strings which I need to send one by one sequentially
char *txt1 = "I am first text";
char *txt2 = "I am second text";

for (i = 1: i < 3; i++)
{
    strcpy(msg, txt[i]);
    lr_output_message(msg);
    len = strlen(msg);
}
jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • 1
    What is `txt` ? What is the `:` in `for (i=1:i<3;i++)` ? Please post real code. – Jabberwocky Nov 18 '16 at 13:35
  • maybe `char *txt[]={"I am first text", "I am second text"}` and `for (i=0:i<2;i++)` – LPs Nov 18 '16 at 13:39
  • @LP you mean `i < 3` I suppose. – Jabberwocky Nov 18 '16 at 13:41
  • Sourav, in the code you have posted there is a couple of obvious errors as pointed out by Michael Walz. You can edit your question, using the "edit" link, to post code that is closer to the real code that is causing the problem. If this is the real code then you need to make those mentioned changes in your real code. – Toby Nov 18 '16 at 13:45
  • @MichaelWalz " I'm not getting you. 2 elements array so `i<2` – LPs Nov 18 '16 at 13:46
  • 1
    Please can you explain what is the "*C interpreter*"??? – Iharob Al Asimi Nov 18 '16 at 13:48
  • 1
    @LP I think _I_ wasn't getting you. What I meant is `char *txt[]={"I am first text", "I am second text"}` works with `i < 2`, but with `i < 3` it's UB. – Jabberwocky Nov 18 '16 at 13:50
  • When you answer please consider that this may be a part of a pre-hire qualification test. – James Pulley Nov 20 '16 at 21:51

1 Answers1

1

For very many reasons this cannot possibly be your code, because it would never compile

  1. There is no txt array anywhere.
  2. The for loop syntax is wrong.

these are only two, but they are a lot for such little code.

Another question that can be asked is

  • What is the C interpreter?

But the most important thing in your code is

  • There is no good reason to copy the strings!

You can simply use the original pointers and pass them to the lr_output_message() function and also note that you don't need the length of the string since probably lr_output_message() is expecting a defined as a sequence of non-null bytes followed by a null byte. Bear in mind that copying a string is an expensive operation and if you can avoid it you should.

The access violation could be for many reasons, you are reading a third element in an array of only two. You are copying a very long string to an array that doesn't have enough space for it.

But from the posted code it's not possible to know which one is the real problem, because the code you posted would not even compile.

Also, allocating an array of an arbitrary size "probably" big enough for the target string is a bad practice IMHO. You should have a clear limit for the size of the array, or compute it at runtime. It actually depends on many things but, in general you should either know that the string is guaranteed to fit the array or, check at runtime.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97