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

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    Does that mean that my second code snipplet is also bad programming, so things like:
    Code:
    char *p = "so fine";
    <...lots of other code...>
    p = "even finder";
    That will work because all you are doing is changing the address pointed to...

    BTW, what compiler are you using... that allows you to mess with changing string literals?

  2. #17
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I was not clear I guess. I am reusing a pointer by assigning different string literals to it, is that bad programming?

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    I was not clear I guess. I am reusing a pointer by assigning different string literals to it, is that bad programming?
    Strange... but not evil.

  4. #19
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by CommonTater View Post
    BTW, what compiler are you using... that allows you to mess with changing string literals?
    LCC compiler for windows

  5. #20
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by CommonTater View Post
    Strange... but not evil.
    Ok then, suppose I am writing a function foo, and am testing it in main to see if it works. It takes a *char and does something with it. How would you do this?
    Code:
    void foo(char *story) {
       ...
       }
    
    int main(void) {
    
       char *p = "test";
       foo(p);
    
       p = "another test string";
       foo(p);
    
       etcetera
    }

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    Ok then, suppose I am writing a function foo, and am testing it in main to see if it works. It takes a *char and does something with it. How would you do this?
    Code:
    void foo(char *story) {
        printf("%s",story);
       // and absolutely nothing that changes the value of story.
       }
    
    int main(void) {
    
       char *p = "test";
       foo(p);
    
       p = "another test string";
       foo(p);
    
       etcetera
    }

  7. #22
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You cannot modify string literals, as you have been told over and over again. If you want to modify text you need to store them as C-style strings. See Lesson 9: C-Style Strings.

    A quick example:
    Code:
    void foo(char*);
    
    int main(void){
    
         char myString[] = "test";
         char myString2[] = "another test string";
    
         foo(myString);
         foo(myString2);
    
         return (0);
    }
    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.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    You cannot modify string literals, as you have been told over and over again. If you want to modify text you need to store them as C-style strings. See Lesson 9: C-Style Strings.
    That's why I asked about his compiler... It should be complaining it's butt off...

  9. #24
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by AndrewHunter View Post
    You cannot modify string literals, as you have been told over and over again. If you want to modify text you need to store them as C-style strings. See Lesson 9: C-Style Strings.
    I did not express myself clearly. I am well aware that string literals cannot be changed. The function does not change them but simply takes them as an input. My point/question was, that I have the same pointer point to different string literals consecutively and whether this is correct use of the 'reused' pointer (which is a variable after all).

  10. #25
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    That's why I asked about his compiler... It should be complaining it's butt off...
    Yeah, I know. I don't have any experience with those little C compilers so I am not sure how they are setup, however you would expect some sort of warning.
    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.

  11. #26
    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
    I did not express myself clearly. I am well aware that string literals cannot be changed. The function does not change them but simply takes them as an input. My point/question was, that I have the same pointer point to different string literals consecutively and whether this is correct use of the 'reused' pointer (which is a variable after all).
    Yes, it is possible. I am not sure why you would implement a solution like this, but you technically 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.

  12. #27
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by AndrewHunter View Post
    Yeah, I know. I don't have any experience with those little C compilers so I am not sure how they are setup, however you would expect some sort of warning.
    Could you point out a line in my sample code were your compiler would complain it's bud off?

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This line:
    Code:
    char *p = "test";
    should cause warnings, as you are losing const-ness (the right side is "pointer to constant char", while the left side is "pointer to char").

  14. #29
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by tabstop View Post
    This line:
    Code:
    char *p = "test";
    should cause warnings, as you are losing const-ness (the right side is "pointer to constant char", while the left side is "pointer to char").
    Strange, it looks a lot like
    Code:
    char *pmessage = "now is the time";
    which comes straight from Kernighan and Ritchie (2nd edition p.104)

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should be reading a book that dates from after the invention of "const" then.

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