Thread: Total beginner questions

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    Total beginner questions

    Good day all - this is my first post.

    I've recently started to learn C and I love to be so close to the hardware. However, I have a question about char *. Does the following code cause a memory leak? I'm almost sure it does and sends shivers down the spine of any respectable c-programmer! :-)

    What is the best way to do what I'm trying to achieve? I'm thinking of defining a char-array and passsing the address of tha to the doSomeSillyThing function.

    Code:
    int doSomeSillyThing(char **msg)
    {
      *msg = "Are you sure this is not causing a memory leak?"
      return 10;
    }
    
    int main()
    {
      char **msg;
      int code = doSomeSillyThing(msg);
    }
    Thanks for the help...

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    No, it doesn't cause a memory leak, but only because you're trashing memory your program most likely doesn't own.

    To allocate memory dynamically use something like malloc().

    Code:
    int doSomeSillyThing(char **msg)
    {
      static char *bleh = "Are you sure this is not causing a memory leak?";
      *msg = malloc(strlen(bleh) + 1);
      strcpy(*msg, bleh);
      return 10;
    }
    Note: You must free() the memory you allocate. However, you have a different problem to address first. You should be declaring msg in main() as a char *, and then passing its address. So in main()....:

    Code:
    int main()
    {
      char *msg;
      int code = doSomeSillyThing(&msg);
      printf("msg = %s\n", msg);
      free(msg);
      return 0;
    }
    Note all the changes. They are important.

    Edit: And don't forget to include the proper header files. stdio.h is needed for printf() and stdlib.h is needed for malloc() and free().

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Complex thing in C I guess

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, bleh should be a const char*. You could declare it as a const char[] instead, upon which you could use sizeof instead of strlen(), e.g.,
    Code:
    static const char bleh[] = "Are you sure this is not causing a memory leak?";
    *msg = malloc(sizeof bleh);
    strcpy(*msg, bleh);
    And don't forget to include the proper header files. stdio.h is needed for printf() and stdlib.h is needed for malloc() and free().
    Also, <string.h> should be included for strcpy() and strlen().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by audinue View Post
    Complex thing in C I guess
    Not really. In fact, your compiler may already be warning you about using an un-initialized variable.

    What you do is define a pointer-to-C-string, then dereference it before giving it a value.

    You don't need to allocate it manually, as long as you don't plan on changing the contents of msg later. This is perfectly fine:

    Code:
    char *msg = "Some string";
    so this is too:

    Code:
    void setMsg(char **msg)
    {
        *msg = "Some string";
    }
    
    int main(void)
    {
        char *msg;
        setMsg(&msg);
    }
    You should probably throw in a few const specifiers to make it clear that you're not supposed to try and alter the contents.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More correctly:
    Code:
    void setMsg(const char** msg)
    {
        *msg = "Some string";
    }
    
    int main(void)
    {
        const char* msg;
        setMsg(&msg);
    }
    When assigning string literals, always use const. They are unmodifiable (most of the time anyway).
    However, when applying const here, we see the limit of this approach: we can't actually modify the msg string later due to it being const.
    If you need to change it, you'll have to retort to something along the lines of the solutions above.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  2. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  3. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. Replies: 4
    Last Post: 04-22-2003, 12:52 PM