Hi,

I'm programming a simple line editor but I'm running into 1 issue that I just can't seem to figure out. I'm not the greatest at C++, if somebody could have a look I would be grateful.

Basically I have this Main...

http://pastebin.com/m6e249679

Then I have this source file...

http://pastebin.com/mdb671dd

and header file...

http://pastebin.com/m3cbd209a

Now the problem is in dtioEdit(). What I want to be able to do is make changes to a string and press Escape and have the string revert back to do its original state. You can find the code for that in the source file's line 392.

Now my problem is that I do properly revert the string back to the original state in dtioEdit() and then the program returns to the Main to do a strcmp (line 74) to check if the strings are the same. For some reason the string does not revert back to its original state in the Main but it does when I'm in the dtioEdit() function. I'm guessing it is a pointer issue.

So for example this is what should happen...

string: My name is Bob <----- Original string

Make change to string: My name is Jen

Press Escape

currently string: My name is Bob


Currently this is what is happening...

string: My name is Bob <----- Original string

Make change to string: My name is Jen

Press Escape

currently string: My name is Jen

I Keep a copy of the original string (called s) and I revert it on line 406 in the source file. Then the function exits because Escape is supposed to revert back to the original string and then end editing. Now we go back to the Main but for some reason my string has not been reverted back to the original string like it had within the function. I don't understand why that is happening..

I'm pretty sure the problem is in the if(keyPress == ESCAPE) block (line 392). Anyone know what I can do for my string s, to be updated in the Main properly and not just only in the function itself?

Thanks

Recommended Answers

All 3 Replies

line 383: copying 1 too many bytes because currentSizeOfS has already been incremented by 1 to account for the null terminator.

line 400: The s pointer was passed by value, nor by reference, so allocation additional memory here only has scope in this function, not the calling function. Just delete this line because the calling function has already allocated the memory and all you have to do is copy the string in originalContentOfs back to s, as you are already doing on lines 406 and 407.

I see there are other instances where s is being reallocated -- those lines will not work either for the same reason as line 400.

How to correct: pass s by reference so that memory allocations will be reflected in the calling function int dtioEdit(char **s, int row, int col, int len, int *ppos) Not the double star.

Should I trust this external site?

Should I trust this external site?

I only opened the second link and didn't have a problem. Its just the source code with line numbers. It would have been a lot better, and safer, had he just attached the files or posted them directly here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.