Is this undefined?

This is a discussion on Is this undefined? within the C Programming forums, part of the General Programming Boards category; I think what I am doing is legal, but I am not 100% sure. I have function "A" that sets ...

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

    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
    21,734
    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().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    398
    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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21