• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Something funny in number 8

Started by Paul Breen, January 17, 2010, 05:11:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Paul Breen

If I change this line:
strcpy(szBuffer, "Frederick J. Harris");

to this
strcpy(szBuffer, "Frederick J. Harris extra");

It works fine, and I can change
char szBuffer[20];

to this:
char szBuffer[15];

And nobody complains, but if I put '2' instead of '20' as the buffer size, windows gives an
application error dialog, but the output is still correct. This is the classic c problem of
writing on memory that does not belong to you, is it not?

PB

José Roca

 
Yes, the typical buffer overrun.

Frederick J. Harris

Yes, the whole intent of that Program Example #8 was to show the 'bad news'.  Up to that point in the series a programmer used to BASIC or another high level language which protects one to a great extent from such worries about allocating memory might have been lulled into thinking that C isn't too much different from BASIC.  In that program I wanted to show that it is indeed very, very different from BASIC.

One of the tricky issues about being off by only a few bytes is that when you do a memory allocation the operating system will generally give you a few extra bytes beyond what you request due to memory allocation granularity.  In other words, if you ask for 5 bytes you'll likely get 8.  These are the kinds of things that can cause really terrible intermittant bugs in programs.  When you are doing low level work like this you really have to be careful in what you are doing.   The whole reason for .NET is that its Microsoft's bet that by and large, most programmers aren't up to the task.   

Frederick J. Harris

I added a few extra lines of commentary to ex 8 to help clarify the point, because its really fundamental and important.