String array and const

This is a discussion on String array and const within the C Programming forums, part of the General Programming Boards category; Hi, Could someone show me a simple example of an attempt to change the protected strings that are pointed to ...

  1. #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!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What you mean like a[1][2]='b'?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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" } 
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #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?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Thumbs up

    Thank you both for this informative lesson.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

Tags for this Thread


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