Advertisement

Check if memory is writeable

Started by October 30, 2003 05:36 AM
32 comments, last by ZMaster 21 years, 4 months ago
No brunow, sorry, I''ve said that before, that was my fault, my example should be something like this:

const char szTmp[] = "Test 2";
strcpy((char*)szTemp, "Test");

I''m not doing any memory overwriting in my project. I just wrote the above code from scratch, to show my problem and made a little mistake.
I also made an exact copy of your code and it gives me the same error again in the first case (I don''t get the message "Inside catch"). In the second case everything works fine (as expected) because szTemp isn''t a const char anymore - that''s clear.

So it looks like the exception handling doesn''t work, right? And it''s not the same behaviour you have, Enigma, because I can''t write to a const if I compile my code.

I''m using Bloodshed''s Dev-C++ on a Windows XP Intel compatible machine. Maybe I have to set a compiler flag, to enable exception handling (but it shouldn''t be disabled by default, right?). What compilers do you use?

Very strange

You can''t.
And if you could, it would be very bad coding practice. There is another way around your problem.
Could you describe what you are trying to accomplish?
Advertisement
Visual C++ 6.0 / Windows 2000

But that''s really weird that both of you run the program without any exception.

Enigma : the 1st case shouldn''t work because all constants (strings) declared in your code should be considered by your compiler (at least the one I use...) as non-writeable.

But it''s true that it can really depends on the compiler.

Another example if you write :

char *szTmp1 = "Hello";
char *szTmp2 = "Hello";

Normally, szTmp1 and szTmp2 should have the same value at the compilation ... and they should address the same location in memory, where "Hello" is written. And you shouldn''t be able to modify it. But again, it depends on the compiler. That''s the only reason I can see.

oh and yes I have by default exception handling enabled in my options
What you''re looking for is IsBadWritePtr() - determine if a block of memory is writable. If you declare variables const, the compiler might put them in a block of read-only memory, and you want to check for that

Of course, that''s windows only - so maybe you should instead design your program so you never risk writing on a read only area.

- Robert
quote:
Original post by groby
so maybe you should instead design your program so you never risk writing on a read only area.


It's not always possible...

What would you do in a such case :

Imagine you are in a team, you have to write your own function but you won't write the part of the caller side.

Here is YOUR function (this one is really for nothing but it's just an example lol) :

void WhatWillHappen(char *pKillMe)
{
pKillMe[0] = 0;
}

You don't know what you will receive in the parameter 'pKillMe'
What will happen and what can you do with the following cases :

char buffer[16];
WhatWillHappen(buffer);
WhatWillHappen("Hello");
WhatWillHappen(0);
WhatWillHappen((char *)1);


[edited by - brunow on October 30, 2003 11:13:31 AM]
Advertisement
What I''m trying to do is: I''m working on a ingame console for my Engine project, at the moment. I can add variables and functions, so that thos can be called/changed from inside the game console. A void* pointer to a variable is stored within my console class, so that I can change and retrieve the value of this variable on runtime using the console. The problem is, if I add a const "variable" to the list, it''s value can''t be changed because I always run into this memory error, we are talking about. I added a readonly flag to my AddVariable() function so I can deny writing to this const manuelly. But to avoid runtime errors (you know, they don''t appear that often, but when they do, it''s really hard to get rid of them ) I wanted consts to be detected automatically.
quote:
Original post by ZMaster
I added a readonly flag to my AddVariable() function so I can deny writing to this const manuelly. But to avoid runtime errors (you know, they don''t appear that often, but when they do, it''s really hard to get rid of them ) I wanted consts to be detected automatically.


You say that you can choose manually to not write in a const variable. So how can you be sure that your error is due to the consts ?? Runtime errors happen for many reasons. Have you tried to debug the line that provokes this error ?
Ok, solved, thank you very very much for your help, ppl.

@groby: IsBadWritePtr() works perfectly, thanks.
@brunow: I''m pretty sure it was the const thing. If I only changed the type from const char to char, everything worked as it should. And with IsBadWritePtr() I don''t get that error again.

This topic is closed to new replies.

Advertisement