Re: reverse Fuction "Segmentation fault"

Discussion in 'C++' started by MiniDisc_2k2, Jul 3, 2003.

  1. MiniDisc_2k2

    MiniDisc_2k2 Guest

    "sam" <> wrote in message
    news:7zMMa.221752$...
    > If I run first code, I am getting "Segmentation fault" error but the

    second
    > one works fine. Why is not allocating memory properly? Is this compiler
    > issue? what is your opinion?
    > 1)
    > int main(){
    > char * text = "Hello"; //2


    Perfectly valid. In assembly, what's going on here is that text becomes the
    address of a _read only_ (hint hint) null terminated character array.
    Therefore, the type of text should more practically be const char* or even
    more practically const char const * (constant character array, constant
    address). Whenever you initialize a pointer like this and not with the new
    operator, it's a good idea to make it a constant, and moreover a constant
    address as well.

    > reverse (text, text + strlen(text));


    This is illegal, since text cannot be modified.

    > cout << text << endl;
    > return (0);
    > }
    >
    > 2)
    > int main(){
    > char * text = new char[10];


    Because you used the new operator here, you have modifiable data.

    > strcpy(text,"Hello")


    Making this legal

    > reverse (text, text + strlen(text));


    And this legal

    > cout << text << endl;
    > return (0);
    > }
    >
    > Thank you very much.
    > Sam
    >
    >


    If this were brought down to assembly, you would see that in #1 the string
    "Hello" is actually part of the program, and therefore cannot be modified
    (you would, in essence, be modifying the exe file you're running the program
    from).

    -- MiniDisc_2k2
    To reply, replace nospam.com with cox dot net.
    MiniDisc_2k2, Jul 3, 2003
    #1
    1. Advertising

  2. * MiniDisc_2k2:
    | Therefore, the type of text should more practically be const char* or even
    | more practically const char const * (constant character array, constant
    | address).

    You would need a 'const char *const' or 'char const *const' for that.
    Rememeber that 'const char' and 'char const' are the same, and a const
    modifier for a pointer value should go immediately after the *.

    --
    Robert Bauck Hamar
    Robert Bauck Hamar, Jul 3, 2003
    #2
    1. Advertising

  3. MiniDisc_2k2

    MiniDisc_2k2 Guest

    I'm sorry I didn't mean that. You're right. Typo.

    "Robert Bauck Hamar" <> wrote in message
    news:...
    > * MiniDisc_2k2:
    > | Therefore, the type of text should more practically be const char* or

    even
    > | more practically const char const * (constant character array, constant
    > | address).
    >
    > You would need a 'const char *const' or 'char const *const' for that.
    > Rememeber that 'const char' and 'char const' are the same, and a const
    > modifier for a pointer value should go immediately after the *.
    >
    > --
    > Robert Bauck Hamar
    >
    MiniDisc_2k2, Jul 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    867
  2. Pud
    Replies:
    0
    Views:
    575
  3. Replies:
    0
    Views:
    529
  4. Ivan Vecerina
    Replies:
    0
    Views:
    485
    Ivan Vecerina
    Jun 29, 2003
  5. dogbite
    Replies:
    4
    Views:
    693
    osmium
    Oct 10, 2003
Loading...

Share This Page