Thread: Another pointer question

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Another pointer question

    I have a questing about pointers. When I initialize a pointer using
    Code:
    char * crap_ptr = "crappy pointer"
    I can't modify it for example using strcat, I have the same problem with the following example
    Code:
    int somefunction(char * some_ptr)
    {
        strcat(some_ptr, "hmm");
        return 0;
    }
    
    somefunction("error"); /* will generate fatal error */
    I'm guessing its because they are part of the program itself and located in the stack and malloc allocates heap memory.

    So I was hoping if someone could tell me if I'm right or not

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    "strings" are usually stored in read-only memory, so trying to modify them usually results in a crash.

    If your compiler makes "strings" constant, they're usually placed in the same part of memory as your compiled code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> If your compiler makes "strings" constant, they're usually placed in the same part of memory as your compiled code.

    static data doesn't have execute permission though, I wouldn't expect to find them in the same section per se.
    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
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You're trying to modify a char array that's a constant:
    Code:
    char *myString = "Hello";
    Is the Same thing as:
    Code:
    const char BLAH[] = "Hello";
    char *myString = BLAH;
    You should do this instead, so you can modify the contents:
    Code:
    char myString[] = "Hello";
    myString[0] = 'P';
    OR
    Code:
    char *myString = malloc(255 * sizeof(*myString));  /* Create 255 characters of space */
    strcpy(myString, "Hello");
    myString[0] = 'P';

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When illustrating the use of malloc, it's best to remeber it's always used with it's companion function, free.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I always forget free, ggrraahh!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    Thanks a lot guys, the problem was I used this to test functions etc. So I'll just stick to strcpy first like Kleid-0 described.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM