Check if memory is writeable
Hi,
I need to check wether a certian variable points to a writeable memory block or not, whick means it is NOT const:
const char szTmp[] = "Test";
strcpy((char*)szTemp, "Test 2");
The above code results into a runtime error because szTemp is const.
Is there a possibility to check wether szTemp ist const or not at runtime?
thx
Flo
You can check with exceptions handling :
try
{
*(long *)(szTmp) = *(long *)(szTmp);
}
catch(...)
{
// an exception occurs certainly the memory is not writeable
// szTmp is also called : wild pointer
// write here what you need to do in a such case
// for example return with an error code etc ...
}
try
{
*(long *)(szTmp) = *(long *)(szTmp);
}
catch(...)
{
// an exception occurs certainly the memory is not writeable
// szTmp is also called : wild pointer
// write here what you need to do in a such case
// for example return with an error code etc ...
}
Const is purely a compile time construct - it cannot cause a runtime error. Your program is crashing because you are writing beyond the bounds of your array.
szTmp is an array of five character [''T'', ''e'', ''s'', ''t'', ''\0''].
you then try and copy seven characters into it. [''T'', ''e'', ''s'', ''t'', '' '', ''2'', ''\0'']
You should either ensure that you''re arrays are big enough:
Or limit your copying to the size of the array
Or use a string class, i.e. std::string
I recommend you read up a bit more on const and arrays because it doesn''t look like you fully understand them.
Enigma
szTmp is an array of five character [''T'', ''e'', ''s'', ''t'', ''\0''].
you then try and copy seven characters into it. [''T'', ''e'', ''s'', ''t'', '' '', ''2'', ''\0'']
You should either ensure that you''re arrays are big enough:
const char szTmp[7];...
Or limit your copying to the size of the array
...strncpy((char*)szTmp, "Test 2", 4); // copy four characters because we want to keep the ''\0'' string terminator at the end of the string
Or use a string class, i.e. std::string
I recommend you read up a bit more on const and arrays because it doesn''t look like you fully understand them.
Enigma
No Enigma, that''s definitely not the problem. My code was kind of a bad example. I have the same problem with const ints or const floats, too. And as far as I can say, I think I understand the concepts of arrays and memory allocation 
@brunow: I tried your code but it didn''t work. The code within the ''try'' statement produces the same error ("could not write to memory" or something like that). Furthermore, I think you first have to ''throw'' an error, to ''catch'' one (this is what I have learned about exception handling).
Nevertheless, thanks for your ideas.
Has anyone another possible solution?

@brunow: I tried your code but it didn''t work. The code within the ''try'' statement produces the same error ("could not write to memory" or something like that). Furthermore, I think you first have to ''throw'' an error, to ''catch'' one (this is what I have learned about exception handling).
Nevertheless, thanks for your ideas.
Has anyone another possible solution?
no, when I wrote :
catch(...)
you have to write it as it is even the (...)
it means that it will catch all kind of exceptions. But you had to change the code to suit your needs. And it will happen if you try to write inside an unauthorized location.
But I agree with Enigma, your code is not really good as you are writing in memory after the bounds of your array !! I didn''t noticed that after the first read.
So can you give us an example where it crashes with a const float or int for example ?
catch(...)
you have to write it as it is even the (...)
it means that it will catch all kind of exceptions. But you had to change the code to suit your needs. And it will happen if you try to write inside an unauthorized location.
But I agree with Enigma, your code is not really good as you are writing in memory after the bounds of your array !! I didn''t noticed that after the first read.
So can you give us an example where it crashes with a const float or int for example ?
Try this example you will see that the try/catch works :
#include <stdio.h>
#include <string.h>
void main()
{
char *szTmp = "Book";
printf("%s\n",szTmp);
try
{
szTmp[0] = ''L'';
}
catch(...)
{
printf("Inside catch\n");
return;
}
printf("%s\n",szTmp);
}
Here "Book" is a constant string, when you compile your code an address in memory will be assign to this characters string and where you won''t be able to write anything inside. ok ? szTmp is initialized with this address.
When you try to replace the first character of "Book" by the character ''L'', it throws an exception and you should enter in the catch statement.
Now try to replace :
char *szTmp = "Book";
by :
char szTmp[] = "Book";
it''s not the same declaration! Here you are declaring an array of unknonw size, but the compiler will reserve enough characters to fill the array with "Book" + 1 more for ''\0'', so it will reserve 5 characters for this array. If you try to write more than 4 characters + ''\0'' you will overwrite the memory. But it won''t necessarily crash.
Hope it will make things clearer...
#include <stdio.h>
#include <string.h>
void main()
{
char *szTmp = "Book";
printf("%s\n",szTmp);
try
{
szTmp[0] = ''L'';
}
catch(...)
{
printf("Inside catch\n");
return;
}
printf("%s\n",szTmp);
}
Here "Book" is a constant string, when you compile your code an address in memory will be assign to this characters string and where you won''t be able to write anything inside. ok ? szTmp is initialized with this address.
When you try to replace the first character of "Book" by the character ''L'', it throws an exception and you should enter in the catch statement.
Now try to replace :
char *szTmp = "Book";
by :
char szTmp[] = "Book";
it''s not the same declaration! Here you are declaring an array of unknonw size, but the compiler will reserve enough characters to fill the array with "Book" + 1 more for ''\0'', so it will reserve 5 characters for this array. If you try to write more than 4 characters + ''\0'' you will overwrite the memory. But it won''t necessarily crash.
Hope it will make things clearer...
OK, ZMaster - sorry for assuming you were a n00b!
However, we don''t seem to be seeing the same behaviour. When I run brunow''s code I get:
Which is exactly the behaviour I expect - no exceptions, no problems. Provided you are writing for a standard Intel compatible PC (I don''t know about other platforms) the character array containing ''Book'' in this example will always be writable. It is simply an area of RAM.
Is this behaviour not what you observe?
Enigma
However, we don''t seem to be seeing the same behaviour. When I run brunow''s code I get:
BookLook
Which is exactly the behaviour I expect - no exceptions, no problems. Provided you are writing for a standard Intel compatible PC (I don''t know about other platforms) the character array containing ''Book'' in this example will always be writable. It is simply an area of RAM.
Is this behaviour not what you observe?
Enigma
I'm not sure of what you mean but which code have you tried ?
the one with :
char *szTmp = ...
or
char szTmp[] = ...
??
the first one shouldn't work!!
[edited by - brunow on October 30, 2003 10:20:14 AM]
the one with :
char *szTmp = ...
or
char szTmp[] = ...
??
the first one shouldn't work!!
[edited by - brunow on October 30, 2003 10:20:14 AM]
May I just butt in and ask what you are actually trying to accomplish, ZMaster? I think you may be making things harder than they are.
If something is defined as const, then you shouldn''t be writing to it and the compiler will catch it at *compile time* if you do try to write to it. If you''re getting memory errors at runtime, then you are writing outside the bounds of your memory locations, which is a serious problem. If you''re having problems with ints and floats and stuff, maybe you should post the actual code you''re having problems with here and we''ll look for better ways to accomplish what you''re trying to achieve.
If something is defined as const, then you shouldn''t be writing to it and the compiler will catch it at *compile time* if you do try to write to it. If you''re getting memory errors at runtime, then you are writing outside the bounds of your memory locations, which is a serious problem. If you''re having problems with ints and floats and stuff, maybe you should post the actual code you''re having problems with here and we''ll look for better ways to accomplish what you''re trying to achieve.
brunow: I tried the first one. I copied the code you posted, compiled it as a c++ program with the Borland 5.5 compiler and ran it. My understanding of c++ leads me to believe that what I am observing is absolutely correct. Perhaps you could could explain why you believe this example should not work (I''m quite willing to believe I''m wrong if you can justify it)?
Enigma
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement