TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Darin McBride
from: Peter Schuller
date: 1996-07-21 14:58:52
subject: VERY strange violation acception!

PS>> I have a problem witch I will explain, but first, this 
 PS>> is the file the "messed up" part of the program are 
 PS>> suppose
 PS>> to read (the lines by the probbs, there are several 
 PS>> line before that, wich are commented out with "//", and a
 PS>> few line after to...):

 DM> Damn, this one was hard to track down.  VERY annoying.  Not an
 DM> annoying bug, but annoying source code.  I'm going to recommend a few
 DM> things to clean this up.

:-D Yes, I know about the pointers... Everybody complained about that :-D

 PS>> When a was debugging, a made the program print every 
 PS>> character it read, on the first line not commented out,
 PS>> and when it had gotten to "Cls=" it craches (more on
this in the
 PS>> code).

 PS>> Here is the important part of the code:

 DM> Question: have you tried running this more than once?  Does it ALWAYS
 DM> crash in the same place?
Yeps...

 PS>> {
 PS>>    ofstream error ("Errors.Log", ios::app);
 PS>>    int *errorcode = new int;
 PS>>    *errorcode = 0;
 PS>>    char *ch = new char;
 PS>>    *ch = ' ';
 PS>>    int *state = new int;
 PS>>    *state = 0;

 DM> What the HECK are you doing all this for?  You are using new to
 DM> allocate a single character... and the ch pointer is taking up 4
 DM> bytes just to point at a single character!  Try:

 DM> int errorcode;
 DM> char ch;
 DM> int state;

 DM> Then don't dereference this.

 PS>>    char *str = new char[200];

 DM> You're writing for OS/2.  Just put this on the stack.  I'll get back
 DM> to the reason later.

 DM> char str[200];

 PS>>    ifstream ctl ("MaxNews.Ctl");
 PS>>    if (ctl.fail())

 DM> Let's say that opening this fails.  (no such file, say)

 PS>>    {
 PS>>       ofstream error ("Errors.Log", ios::app);
 PS>>       error << "Error: Unable to open MaxNews.Ctl
(Errorlevel 1)\n";      
 PS>> error.close();       ctl.close();       return 1;     // Unable to
 PS>> open MaxNews.Ctl

 DM> You're returning 1, but NOT cleaning up the rest of your memory!  You
 DM> aren't deleting errorcode, ch, state, or str!  However, if they are
 DM> on the stack, as I proposed above, you don't have to clean it up -
 DM> that's automatic.

 PS>>       ctl.get (*ch);

 DM> This would become ctl.get(ch);, for example.

 PS>>       *state++;

 DM> Here's your bug.  *state++ is more explicitly parsed as:
 DM>    *(state++);

 DM> As you can see, you are incrementing the POINTER, not the value it
 DM> points at.  If you use state on the stack, it will simply increment
 DM> this directly:

Yes, but I assume *state++ would work, since *state++ is the va”ue, and
state++ would be the pointer, and a think it worked in Borland C++ 4.5 for
DOS.

 DM>    state++; // what you want if you aren't using state as a pointer

 PS>>       str[*state] = *ch;    // here is the bug, when 

 DM> Now that state is pointing somewhere other than where you want, it
 DM> could be pointing at a rather large number.

 DM>         str[125263]

 DM> is very likely to be outside your program space.  OS/2 doesn't like
 DM> that, and traps it.


/ Peter Schuller   [TeamOS/2]   petersch{at}kuai.se

--- FleetStreet 1.16 NR
* Origin: Hedgehog BBS 25h/day +46-(0)18-367781 (2:206/139)
SEEN-BY: 50/99 270/101 620/243 625/100 711/401 409 410 413 430 808 809 934
SEEN-BY: 711/955 712/407 515 624 628 713/888 800/1
@PATH: 206/139 0 201/505 0 200 2104 109/347 18 13/25 396/1 270/101 712/515
@PATH: 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.