1
#include<string.h>
int main()
{
     char *s;
     strcpy(s,"asdqw");
     strcpy(s,s+2);
     return 0;
}

This program is not showing up any error when run in linux system, it works fine. But when on running in mac osx, it is showing abort trap : 6. Why is this so happening so?

Aswin Prasad
  • 407
  • 6
  • 14

2 Answers2

2

You have to allocate memory to s. Like this:

char *s = malloc(100);

Otherwise, undefined behaviour is caused. Since the behaviour is undefined, it working on Linux and not working on OS X are both reasonable.

Also, as @Florian Zwoch wisely points out, the second strcpy() operates on overlapping memory areas, which invokes undefined behaviour again. This is because strcpy() doesn't allow memory areas overlapping. You may want to use memmove(s, s + 2, sizeof (s + 2));, which allows the destination and source to overlap.

nalzok
  • 14,965
  • 21
  • 72
  • 139
0

You can run "Valgrind" on Linux and see if is there any error about memory.

Valgrind easy Tutorial This is an easy tutorial how to use it.

It's because it could be a memory problem.