R
raashid bhatt
Hi.
Consider the Following Code.
=========================================================================
#include <stdio.h>
#include <string.h>
/* For these Variables The Linker Only Allocates 4 Bytes each in the
memory Like
DB 77 -> w
DB 00
DB 00
DB 00
DB 61 -> a
DB 00
DB 00
DB 00
*/
char a[] = "w";
char b[] = "a";
int main(int argc, char **argv)
{
printf("%s",a);
/*
Copy 7bytes which should not cause data to be overwritten as a have
declared a
as a char[] 'welc' copied(as a has been allocated 4 bytes) to a but
'ome' Get written
to b as b is adjacent to it*
/
strcpy(a, "welcome");
printf("%s", b); /* output 'ome'
return 0;
}
============================================================================
i tried it with nearly all compilers and all produced the same output.
my point is
why for a char[] only 4bytes are allocated while as it has space for
unlimited num of bytes.
Consider the Following Code.
=========================================================================
#include <stdio.h>
#include <string.h>
/* For these Variables The Linker Only Allocates 4 Bytes each in the
memory Like
DB 77 -> w
DB 00
DB 00
DB 00
DB 61 -> a
DB 00
DB 00
DB 00
*/
char a[] = "w";
char b[] = "a";
int main(int argc, char **argv)
{
printf("%s",a);
/*
Copy 7bytes which should not cause data to be overwritten as a have
declared a
as a char[] 'welc' copied(as a has been allocated 4 bytes) to a but
'ome' Get written
to b as b is adjacent to it*
/
strcpy(a, "welcome");
printf("%s", b); /* output 'ome'
return 0;
}
============================================================================
i tried it with nearly all compilers and all produced the same output.
my point is
why for a char[] only 4bytes are allocated while as it has space for
unlimited num of bytes.