Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ofofmilk

Advanced (and existential ;-) questions about C++...

This topic is 5310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I would like to know if it''s possible to detect whether a string pointer (char*) points to a static string or not.
char* staticString = "Hello world !";

char buffer[64];
strcpy(buffer, staticString);
char* nonStaticString = buffer;
Of course if my code tries to write into the static string I will get an access violation. Maybe this is something I can query, if the address of the string is accessible in writing or not? but how? Btw, is it possible to obtain the write access to an "access violation" area? An other question about pointers, if I have less than 2Go of memory I can assume that the most significant bit of my pointer is always 0, am I right? About static strings again, if I have the following code:
// Somewhere in the code

char* str0 = "Hello";
...
// Somewhere else

char* str1 = "Hello";
Will str0 and str1 ALWAYS have the same value? Thanks, Ofofmilk. PS: I''m only using visual C++ (windows) for now.

Share this post


Link to post
Share on other sites
Advertisement
c++ allows you to get away with writing

char* string = "hello";

when of course it should really be

const char* string = "hello";

Then, whether or not the string is static or dynamic but const, you'll not be allowed to modify it by the compiler (unless you use const_cast in which case you're taking your life into your own hands... which may be fine if you know what you're doing).

I take it that the main drive behind this question is safety rather than whether it really is possible to know the difference between a static const and a dynamic const string.

If you always use the second (correct) form, then you won't have problems in your own code. However, there isn't much you can do about strings passed to you.

In larger terms it may be possible to find out where static strings are stored in the executable and check to see whether the string comes from that region. I don't know anything about that although it is bound to be platform specific and likely compiler specific.

[edited by - petewood on February 6, 2004 4:55:40 AM]

Share this post


Link to post
Share on other sites
1) Static strings are const.

char* foo = "Hello world !";
should be
const char* foo = "Hello world !";

Whether a static string really is modifiable or not is architecture-dependent. There is nothing in the C language that lets you detect that. Consult your platform''s API reference. (e.g. in windows, your executable code is divided in a number of sections, some of which are read-only). Whether it is possible to get write access to such an area is also platform-specific.

2) No. Virtual memory makes it possible to allocated pages all over the place. Your could have a 4K page allocated right at the top of your address space. All you know is that the allocated
pages will total 2GB at most (assuming you''re not swapping).

3) That''s compiler dependent, often as an optimisation option that can be toggled. Do not rely on it for program correctness.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
quote:
Original post by Ofofmilk
Of course if my code tries to write into the static string I will get an access violation. Maybe this is something I can query, if the address of the string is accessible in writing or not? but how?


Under Windows you can use VirtualQuery() or VirtualQueryEx() to find out what the protection level for a given memory page is. (Here''s a hint: it''s not worth the bother. Chances are, if you''re worrying about something like this, you''re doing something wrong.)

quote:

An other question about pointers, if I have less than 2Go of memory I can assume that the most significant bit of my pointer is always 0, am I right?

Under Win32 the virtual address space is divided into half. The bottom half (where the most significant bit is zero) is user space and the upper half (where the most significant bit is one) is kernel space. It''s possible (though relatively uncommon) to get raw pointers into kernel space, which means that the most significant bit on those pointers will be one. Also, the addressing is completely independent of physical memory. If it''s a kernal pointer and you only have 32 MB of RAN it''s still going to have a the most significant bit equal to one. If it''s a user space pointer and you have 4GB of RAM it''s still going to have a most significant bit equal to zero.

Share this post


Link to post
Share on other sites
Ok, thank you for your answers.
I''m more and more confronted to the limits of programming and C++. I just try to find tricks to optimize or make something possible that is not obvious in standard C++.
ie. I''m thinking about a string class that will not allocate a buffer and copy a given string if it is static (CMyString str = "Blabla..."), but have a different behaviour in this case, just keeping the pointer...

Share this post


Link to post
Share on other sites
quote:
Original post by DrPizza
quote:
Under Win32 the virtual address space is divided into half

Except when it isn''t, of course.


Picky, picky, picky.... Fine. Under Win32 (unless your program is linked with the IMAGE_FILE_LARGE_ADDRESS_AWARE flag and the boot.ini of the host machine contains the /3GB switch and the host machine runs Windows Server 2003, Windows XP Professional Edition or Media Center Edition, Windows 2000 Datacenter Server or Advanced Server or Windows NT 4.0 Enterprise Edition) the virtual address space is divided into half.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!