Problems with new and scope

Started by
16 comments, last by Orpheum 23 years, 8 months ago
wtf am i doing wrong?? please look at the code anyway tho... i really need help on this one!
There is no spoon.
Advertisement
to use the source thing... edit the message that has just:

[/source]

[/source]

and take out the first / that is the ending tag... the opening one should just have source between the [ ]

40

www.databyss.com
www.omlettesoft.com

"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
-40
Found two things: one thing you're doing wrong, and one thing you're doing poorly.

First, what you're doing wrong is writing outside of memory that's assigned to you. When you declare these local variables:
char szBuffer[BUFSIZ];char szVariable[64];char szData[256]; 

...the number of bytes assigned to each is BUFSIZ, 64 and 256, respectively. But when you clear the data, you're clearing one too many bytes. The code here:
for(i = 0; i <= BUFSIZ; i++)  szBuffer = '\0';<br>for(i = 0; i < 65; i++)<br>  szVariable = '\0';<br>for(i = 0; i < 257; i++)<br>  szData = '\0';<br> </pre>  <br>…writes one too many zeros.  I guarantee that it's overwriting something important in memory, which is why your data "disappears" after you leave the function.  Those loops should really be as follows:<br><pre><br>for(i = 0; i < BUFSIZ; i++)<br>  szBuffer = '\0';<br>for(i = 0; i < 64; i++)<br>  szVariable = '\0';<br>for(i = 0; i < 256; i++)<br>  szData = '\0';<br> </pre>  <br><br>I copied your program, made these changes, and it worked for me.<br><br>Second, the thing that you're doing poorly is that you don't even need these loops.  szBuffer is initialized completely by getline: that is, getline will make sure that szBuffer is a null-terminated character array, so it will be safe to use with string functions.  szVariable is almost able to be used as-is, except for the fact that you don't explicitly put a null character on the end of it.  If you do this, you don't need to clear all of szVariable each time either:<br><pre><br>i = 0; // set i to 0 right before you use it instead of at<br>       // the bottom of the loop; makes the code easier to read<br>while(szBuffer != ' ')<br>{<br>  szVariable = szBuffer;<br>  i++;<br>}<br>szVariable = '\0'; // add this line to null-terminate<br>  </pre>  <br>The exact same thing can be done with szData; right after you manually copy each character, set the next variable to '\0' and it will all work for you.<br><br>Also, I noticed that the variable j isn't used at all.  It can be removed.<br><br>That should do it for you.  Be careful: you have no safeguards in your code against this kind of line in your .ini file:<br><pre><br>this_is_some_bogus_variable_stoffel_put_in_just_to_overflow_your_sixty-four_character_variable_buffer = gotcha!<br>  </pre> <br><br>Edited by - Stoffel on July 24, 2000 1:56:39 PM    
Thanks for your input! I will give it a try ASAP... hopefully I wont have to continue this thread. Thanks for pointing out that I didn''t need to clean the buffers... I wasn''t sure if that was necessary or not, but it is always best to err on the side of caution dont you think? My 64 byte array should have some protection, but in reality, users of my program arent as devious as your average programmer so I was just being conservative (why I dont know, all our machines have 64-128 MB of RAM anyway). And for variable j, if you look more closely, you''ll see the line:

    szData[j] = szBuffer<i>;  =P    


If all goes well, that should be a source box. =)
There is no spoon.
should have been:

    szData[j] = szBuffer<i>;    

There is no spoon.
why the hell is the [ and ] on the second one < and >???
There is no spoon.
Ahh, you are correct. In that case, let me again suggest you put the initialization (j = 0) right before you use it, to make your code easier to follow.

The reason for the &lti>'s in the source is because (no spaces) is interpretted as the hyperlink tag for italics in this software. I think the angle-brackets are a work-around. Don't sweat it, we all know what you mean.<br><br>BTW, you should look at strtok. It's a complicated function, but some of these copying procedures you do can be done with a strtok command instead.<br><br>Edited by - Stoffel on July 24, 2000 2:53:02 PM
I use strtok() heavily in another portion of this porgram... its just such a bear to work with sometimes, so it was easier doing it this way for this situation. I tried your idea of i < 64 instead of 65... it didnt work... I noticed that the BUFSIZ statement had a <= instead of <. Now that I look, you put that too, but it is wierd that a single \0 can toast a whole object. Thanks Stoffel!!
There is no spoon.

This topic is closed to new replies.

Advertisement