C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-25-2009, 07:09 PM   #1
Registered User
 
Join Date: Jun 2009
Posts: 3
Smile String array and const

Hi,

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" };
I would like to see the compiler complaining but I don't know what to do to try to modify them.

Thanks!
megaptera is offline   Reply With Quote
Old 06-25-2009, 07:10 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
What you mean like a[1][2]='b'?
tabstop is offline   Reply With Quote
Old 06-25-2009, 07:57 PM   #3
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 06-26-2009, 04:48 AM   #4
Registered User
 
Join Date: Jun 2009
Posts: 3
Quote:
Originally Posted by tabstop View Post
What you mean like a[1][2]='b'?
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" } };
The first one should be used only when no modifications will be performed on the strings ever, regardless whether the const quantifier is used or not, and the second one can be modified when there is no const but it wastes memory when strings are of different lengths. Is this correct?

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   Reply With Quote
Old 06-26-2009, 04:57 AM   #5
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 06-26-2009, 07:04 AM   #6
Registered User
 
Join Date: Jun 2009
Posts: 3
Thumbs up

Thank you both for this informative lesson.
megaptera is offline   Reply With Quote
Reply

Tags
const, pointer, string, string array

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:51 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22