Thread: Is this undefined?

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    Is this undefined?

    I think what I am doing is legal, but I am not 100% sure. I have function "A" that sets a pointer from a returned pointer location from function "B".

    Code:
    void A(void)
    {
        char *myvar;
            myvar = B();
    }
    
    char *B(void)
    {
        char *myvar2;
            myvar2 = malloc(100);
        return myvar2;
    }
    Also, I thought that a neater method would be if B returned nothing but modified A's pointer's address. Could B malloc some space at a pointer passed to it?

    If I am unclear, let me know.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What makes you think that it might result in undefined behaviour? At a glance, it looks perfectly fine, except that B() is really only casting the pointer returned by malloc() to char*, and pretty much does nothing else.

    Quote Originally Posted by carrotcake1029
    Could B malloc some space at a pointer passed to it?
    Yes, but you would need to pass a pointer to a pointer so that the pointer in A()'s scope can point to the object returned by the malloc() call in B().
    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

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    That was just an example, obviously I will do more.

    So would something like this be what you are talking about for the function to be able to allocate space. I may just avoid this, I think it could get ugly in some cases.

    Code:
    void A(void)
    {
        char *myvar;
            B(&myvar);
            //Is this a pointer to a pointer, or would i need to do char **myvar?
    }
    
    void B(char **myvar2)
    {
        *myvar2 = malloc(100);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by carrotcake1029
    So would something like this be what you are talking about for the function to be able to allocate space.
    Yes.

    Quote Originally Posted by carrotcake1029
    I may just avoid this, I think it could get ugly in some cases.
    It depends. You also should provide a size argument (a pointer too) so that B() can set it, otherwise A() would have no idea how many elements were allocated.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM