access of memory beyond allocation, not causing segmentation fault...

C

Chris Dollin

Malcolm said:
One reason he gave was that ANSI permitted the implict cast. OK. I
surrender, _conversion_, reluctantly, and knowing that they were driving a
coach and horses through the type-checking system.

/Casts/ (of pointer types) give you a coach-and-horses -- nay, a veritable
battleship -- to drive with. Allowing an implicit conversion doesn't seem
to make a significant difference and, by removing the necessity to cast
where `void*` is involved, make it, I think, less likely that casts between
non-void-* pointers will appear as a path of less resistance.
 
P

Peter 'Shaggy' Haywood

Groovy hepcat (e-mail address removed) was jivin' on Tue, 08 May 2007 22:19:16
-0700. It's a cool scene! Dig it.
#include <stdlib.h>

int main()
{
char *p = (char*)malloc(100);
if(p==NULL)
{
printf("...null...\n");
}
else
{
printf("...success...\n");
printf("1.%d\n",p[10]);
memset(p,0,101);
printf("2.%d\n",p[10]);
free(p);
printf("3.%d\n",p[101]);

All these printf() calls cause undefined behaviour, since you have
neglected to include stdio.h.

A C90 compiler is required to issue a diagnostic for a non-void
function without a return statement. In C99 it is permissible to leave
out a return statement in main(). However, there are apparently very few,
if any, C99 compilers. So chances are you don't have one. In either case,
it's always been a good idea to put at least one return statement in
main(). Return 0 or EXIT_SUCCESS to indicate successful execution and
EXIT_FAILURE to indicate termination due to an error. Those macros are
defined in stdlib.h, which you have included.
}

I used gcc on Solaris OS and i get the following output,

[Snip.]

You needn't get any output at all. Your program needn't even
successfully compile.

--
Dig the sig!



----------- Peter 'Shaggy' Haywood ------------

Ain't I'm a dawg!!
 

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
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top