![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 3
| Could someone show me a simple example of an attempt to change the protected strings that are pointed to from the following array: Code: const char *a[2] = { "string1", "string2" };
Thanks! |
| megaptera is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| What you mean like a[1][2]='b'? |
| tabstop is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> I would like to see the compiler complaining but I don't know what to do to try to modify them. You can't because they're declared const. But even if they weren't, I wouldn't recommend it. There's always the chance that they are stored in a read-only section of memory, so I'm guessing that the operation would be undefined. Try instead: Code: char a[][32] =
{
{ "string1" },
{ "string2" }
};
|
| Sebastiani is offline | |
| | #4 |
| Registered User Join Date: Jun 2009
Posts: 3
| Yes, this is what I was after. The compiler complains when const is used, and if it's not used then the program gets compiled but then crashes. This is what I wanted to see. Thank you Sebastiani for this notation. My book mentions that read-only sections of the memory but doesn't (clearly) show an alternative. So the options are: Code: const char *c[2] = { "alpha", "beta" };
const char d[][6] = { { "alpha" }, { "beta" } };
Why some compilers store elements of string arrays in read-only sections of memory when no const is used? Is there any particular reason behind it? |
| megaptera is offline | |
| | #5 |
| Guest Join Date: Aug 2001
Posts: 4,923
| >> and the second one can be modified when there is no const but it wastes memory when strings are of different lengths. You can, of course, use malloc to optimize the size of each string, but then you'll have to manage the memory, which can be quite a pain. >> Why some compilers store elements of string arrays in read-only sections of memory when no const is used? Is there any particular reason behind it? Historical reasons, I suppose. Most executable formats contain a section for read-only data (.rodata, or similar) that is used to store text literals and such, but note that this isn't required. The compiler is free to put it wherever it pleases, so it might just end up in a read-write section (eg: data segment). |
| Sebastiani is offline | |
| | #6 |
| Registered User Join Date: Jun 2009
Posts: 3
| Thank you both for this informative lesson. |
| megaptera is offline | |
![]() |
| Tags |
| const, pointer, string, string array |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Smart pointer class | Elysia | C++ Programming | 63 | 11-03-2007 07:05 AM |
| Need help implementing a class | jk1998 | C++ Programming | 8 | 04-05-2007 03:13 PM |
| How do i un-SHA1 hash something.. | willc0de4food | C Programming | 4 | 09-14-2005 05:59 AM |
| Template Array Class | hpy_gilmore8 | C++ Programming | 15 | 04-11-2004 11:15 PM |
| Type and nontype parameters w/overloading | Mr_LJ | C++ Programming | 3 | 01-02-2004 01:01 AM |