strcpy trivia

A

ashok.anbalan

I have a simple strcpy implementation below. I have not malloc'd any
memory for "dest" in the function my_strcpy. I also keep doing dest++
in the loop. I would like to know why does the program *not* core dump?


/-----------------------------------------------------------------/

#include <stdio.h>

void my_strcpy(char *dest, const char *src)
{
while (*src != '\0')
{
*dest = *src;
dest++;
src++;

}
*dest = '\0';
}

int main()
{
const char *src = "hello world";
char *dest;
my_strcpy(dest, src);
printf("dest is %s\n", dest);
return 0;
}

/-----------------------------------------------------------------/

Thanks,
Ashok
 
A

Al Bowers

I have a simple strcpy implementation below. I have not malloc'd any
memory for "dest" in the function my_strcpy. I also keep doing dest++
in the loop. I would like to know why does the program *not* core dump?


/-----------------------------------------------------------------/

#include <stdio.h>

void my_strcpy(char *dest, const char *src)
{
while (*src != '\0')
{
*dest = *src;
dest++;
src++;

}
*dest = '\0';
}

int main()
{
const char *src = "hello world";
char *dest;
my_strcpy(dest, src);
printf("dest is %s\n", dest);
return 0;
}

/-----------------------------------------------------------------/

It crashes on my implementation.
You should read the faq, especially the questions on
undefined behavior. See question 11.35 at:
http://www.eskimo.com/~scs/C-faq/q11.35.html
 
A

ashok.anbalan

Actually I am using gcc on cygwin & I am a curious that the program is
not crashing.
 
J

Jens.Toerring

Actually I am using gcc on cygwin & I am a curious that the program is
not crashing.

You invoke undefined behaviour. And undefined means "undefined", i.e.
everything can happen. That includes the possibility that your program
seems to be working correctly. In your case 'dst' seems to point to
some memory you can write to and the program seems to be short enough
that you don't manage to overwrite something that's going to be used
later. But don't count on that. It could already stop to work when you
invoke the program again.
Regards, Jens
 
C

Chris Croughton

I have a simple strcpy implementation below. I have not malloc'd any
memory for "dest" in the function my_strcpy. I also keep doing dest++
in the loop. I would like to know why does the program *not* core dump?

It is Undefined Behaviour (UB). That means that anything at all may
happen, including (but not limited to) your computer catching fire, your
cat dying, demons flying out of your nose, atomic war -- or nothing at
all. Probably it just happens that the value in 'dest' points to an
area of memory which the system thinks is actually there (so doesn't
trap it and core dump) but which could cause you problems later or with
some other program.

Variables declared as 'automatic' (that is within a function and not
declared as static) do not have any particular value until they are
initialised, they may just have whatever value happened to be at that
location in storage. Indeed, until they are initialised they may not
even have the same value each time they are read (the storage could be
re-used by the OS for other programs until it is marked as 'dirty' by
writing to it).

(On some systems, like the ones for which I program professionally,
there is no trapping at all and no "core dump", all memory accesses are
valid as far as the hardware is concerned...)

HTH,

Chris C
 
M

Mac

Actually I am using gcc on cygwin & I am a curious that the program is
not crashing.

You already have your answer. There is nothing in the c standard which
requires any program to dump core or crash.

But if you WANT a crash, try this slightly modified version of your
original program:

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

void my_strcpy(char *dest, const char *src)
{
while (*src != '\0')
{ *dest = *src;
dest++;
src++;
}
*dest = '\0';
}

int main()
{
char src[1024 * 1024];
char *dest;

memset(src, 'h', sizeof src - 1);
src[sizeof src - 1] = 0;
my_strcpy(dest, src);
printf("dest is %s\n", dest);
return 0;
}

HTH.

--Mac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top