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

  1. #46
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    What is OP?

  2. #47
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by CommonTater View Post
    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.
    You are confusing me. In another thread where I was wrestling with trying to concatenate stuff, you posted this code where you do the same:
    Quote Originally Posted by CommonTater View Post
    Like this....
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define BEGIN "@@"
    #define END "CCC"
    
    int main (void)
      {  char *result = calloc(100,sizeof(char));  // create some string space
    
        strcat(result, BEGIN);                       // result holds @@
        strcat(result, "this is a message");     //  result holds @@this is a message
        strcat(result, END);                         // result holds @@this is a messageCCC
    
        printf("%s\n\n", result);
        free(result); 
        return 0; }
    Alll so nice and simple...
    Not to be annoying, but I just would like to understand what is not correct.

  3. #48
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's not the same thing in the slightest.

    Name
    strcat, strncat - concatenate two strings

    Synopsis

    #include <string.h>
    char *strcat(char *dest, const char *src);
    char *strncat(char *dest, const char *src, size_t n);

    Description
    The strcat() function appends the src string to the dest string overwriting the '\0' character at the end of dest, and then adds a terminating '\0' character. The strings may not overlap, and the dest string must have enough space for the result.

    The strncat() function is similar, except that it will use at most n characters from src. Since the result is always terminated with '\0', at most n+1 characters are written.

    Return Value
    The strcat() and strncat() functions return a pointer to the resulting string dest.

    Conforming to
    SVr4, 4.3BSD, C89, C99.
    Assigning "string" to some random pointer does not allocate memory to it. In fact, the pointer just points to a string you aren't really supposed to change, whether the computer let's you or not.

    I do hope that people stop feeding you code though because of things like this.
    Last edited by whiteflags; 08-09-2011 at 03:15 AM.

  4. #49
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > What is OP?
    Original Poster (the thread starter)
    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.

  5. #50
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    It's not the same thing in the slightest.



    Assigning "string" to some random pointer does not allocate memory to it. In fact, the pointer just points to a string you aren't really supposed to change, whether the computer let's you or not.

    I do hope that people stop feeding you code though because of things like this.
    Darn, stupid, I see the difference now, thanks.

  6. #51
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    You are confusing me. In another thread where I was wrestling with trying to concatenate stuff, you posted this code where you do the same:
    Not the same at all... In my strcat example I am copying FROM string literals into a memory buffer... I am modifying the memory buffer, the string literals are never modified, only copied.

  7. #52
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Got it, got it, I already had my .......... kicked

  8. #53
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    Got it, got it, I already had my .......... kicked
    Gee... and it only took 52 messages...

    (Recent epidemic of people trying totally bizarre stuff, notwithstanding)

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