Thread: Here we go again: when to use Malloc and when not?

  1. #31
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Are you serious, const is in there.

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then either they forgot to change that example from really-old-C to old-C or they're just toying with you (or they mention in all the words around the example that they're doing something not-quite-right for illustrative purposes). I don't have the book so I don't know which.

  3. #33
    Registered User
    Join Date
    Jul 2011
    Location
    Champaign, Illinois, United States
    Posts
    27
    In my experience, using gcc, a statement like
    Code:
    char *p = "this is a string";
    was always acceptable. Trying to do anything with this string, however, is nearly impossible and I can't think of a situation where having a string literal would be very useful in a program, except for using for comparison. Dynamically allocating space for a string and then using strcpy was always taught to me as a more standard practice.

  4. #34
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Hm, kinda stupid, the answer is just there:
    "..pmessage is a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere..." (same page, my underlining)

  5. #35
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    There is no need for discussion on this topic really:

    Quote Originally Posted by C-99 6.7.8.32
    EXAMPLE 8 The declaration
    Code:
    char s[] = "abc", t[3] = "abc";
    defines ‘‘plain’’ char array objects s and t whose elements are initialized with character string literals. This declaration is identical to
    Code:
    char s[] = { 'a', 'b', 'c', '\0' },
    t[] = { 'a', 'b', 'c' };
    The contents of the arrays are modifiable. On the other hand, the declaration
    Code:
    char *p = "abc";
    defines p with type ‘‘pointer to char’’ and initializes it to point to an object with type ‘‘array of char’’ with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to modify the contents of the array, the behavior is undefined.
    Last edited by AndrewHunter; 08-08-2011 at 01:23 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #36
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by django View Post
    Hm, kinda stupid, the answer is just there:
    "..pmessage is a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere..." (same page, my underlining)
    I don't think anyone told you that you couldn't do it. In fact I specifically remember telling you that you could.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #37
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    With all due respect Andrew, you are missing the point. My question is not about changing the string literal (which causes undefined behaviour) but changing where the pointer points to. Anyway, as far as I am concerned,t he matter is resolved. I would like to know however, which of my code samples would have caused your compiler to warn.

  8. #38
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Ok, all cleared up. I will reread this post carefully tomorrow and learn. Thanks again for the article about pointers, it seems I need to reread that too

  9. #39
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by breimer273 View Post
    I can't think of a situation where having a string literal would be very useful in a program, except for using for comparison.
    Well, there's obviously the first argument to printf- that's a string literal.
    Code:
    while(!asleep) {
       sheep++;
    }

  10. #40
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    You cannot modify string literals, as you have been told over and over again.
    Quote Originally Posted by CommonTater View Post
    That's why I asked about his compiler... It should be complaining it's butt off...
    Adak used to argue that Turbo C let him modify string literals. I don't know if it warned or not.


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

  11. #41
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by TheBigH View Post
    Well, there's obviously the first argument to printf- that's a string literal.
    It could be. Doesn't have to be though.

  12. #42
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by _Mike View Post
    It could be. Doesn't have to be though.
    I was trying to find a post I made a long time ago that modified the first argument to printf to print some stars or something, but I can't. It doesn't help that the search here sucks.

    Edit: HAH! help with nested loops


    Quzah.
    Last edited by quzah; 08-08-2011 at 04:36 PM.
    Hope is the first step on the road to disappointment.

  13. #43
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    Could you point out a line in my sample code were your compiler would complain it's bud off?
    Code:
    char *test = "This is a test";
    strcpy(test,"And now it's over");
    That is modifying a string literal and you can't do that. The reason you can't do that is that test is pointing to the read only section of the program's PE Image.

  14. #44
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by django View Post
    LCC compiler for windows
    For the record, I used to run an old version of LCC because it was all I knew at the time. It worked fine for years, but there are definitely some major issues and bugs I've come across. The most frustrating is how it seems to default the character data type to either unsigned or signed, depending on where the characters are declared (it appears to be unsigned by default in "main()" but signed by default in a custom function).

    It works well for learning, but I'd recommend updating to something nicer. Though I haven't actually seen anyone here reference LCC, so I'd be interested in hearing if there were other opinions on it.

  15. #45
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    For the record, I used to run an old version of LCC because it was all I knew at the time. It worked fine for years, but there are definitely some major issues and bugs I've come across. The most frustrating is how it seems to default the character data type to either unsigned or signed, depending on where the characters are declared (it appears to be unsigned by default in "main()" but signed by default in a custom function).

    It works well for learning, but I'd recommend updating to something nicer. Though I haven't actually seen anyone here reference LCC, so I'd be interested in hearing if there were other opinions on it.
    Well, FWIW... Pelles C is a "much modified" derivative of LCC that seems to have overcome many of the original shortcomings... Might make a good update for our OP friend...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Malloc help
    By MyNameIs.. in forum C Programming
    Replies: 5
    Last Post: 04-19-2010, 02:19 PM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. malloc()
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-14-2007, 10:50 AM
  5. When and when not to use malloc()?
    By John.H in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 06:00 PM