Thread: the root of all my problems

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    the root of all my problems

    I had thought that if I have a function that returns a pointer and the compiler will not allow
    this:
    Code:
    datatype thing;
    &thing=function();
    that I could do this:
    Code:
    datatype thing, *ptr=&thing;
    ptr=function();
    so that the return value gets written into thing. I'm now thinking this might not be true because, in fact, *ptr is simply reassigned to a different address by function()...

    I guess I could test this somehow, but I thot I'd just ask
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, the correct thing to do here is something like this:
    Code:
    datatype *ptr;
    datatype thing;
    ptr = somefunc();
    if (ptr)
       memcpy(thing, ptr, sizeof(thing));
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    I'm now thinking this might not be true because, in fact, *ptr is simply reassigned to a different address by function()...

    I guess I could test this somehow, but I thot I'd just ask
    You are correct.
    Though, you actually do this with references in C++:
    Code:
    datatype thing;
    datatype& ref = thing;
    ref = function();
    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.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    shucks...i thot i knew everything already
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    You are correct.
    Though, you actually do this with references in C++:
    Code:
    datatype thing;
    datatype& ref = thing;
    ref = function();
    But that still doesn't work for functions that return a pointer to something. If you are SURE that it's not going to return NULL, you could of course do:
    Code:
    ref = *function();
    Edit: That works for structs and simple types, even in C - don't need a reference in this case - it's just a different way of doing memcpy() I described above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    But that still doesn't work for functions that return a pointer to something. If you are SURE that it's not going to return NULL, you could of course do:
    Code:
    ref = *function();
    Edit: That works for structs and simple types, even in C - don't need a reference in this case - it's just a different way of doing memcpy() I described above.

    --
    Mats
    Mm, maybe. Can't be 100% sure what the function returns.
    The only difference is that data won't have to be written twice.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Mm, maybe. Can't be 100% sure what the function returns.
    The only difference is that data won't have to be written twice.
    Huh? the code I posted really is the same as the memcpy() option - and the reference option - it's just how many variables you need and if you need to check the returned pointer or not. Once the compiler has had a go at it (with optimisation enabled), it will end up the same.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Huh? the code I posted really is the same as the memcpy() option - and the reference option - it's just how many variables you need and if you need to check the returned pointer or not. Once the compiler has had a go at it (with optimisation enabled), it will end up the same.
    Yeah, I was referring to the original memcpy. Dereferencing a pointer like that does not make two copies...

    And I don't always trust the compiler optimizations when it comes to calling functions like memcpy.
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Yeah, I was referring to the original memcpy. Dereferencing a pointer like that does not make two copies...

    And I don't always trust the compiler optimizations when it comes to calling functions like memcpy.
    Well, of course, if you don't NEED to use memcpy() because it's a struct or simple type, then you can do:
    Code:
    ptr = somefunc();
    if (ptr)
       thing = *ptr;
    The memcpy variant is more generic, but if the object is such that it can simply be copied, then that's fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that would probably be optimized by the compiler.
    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.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    hmm.

    So I was doing this, which I guess is basically the same as the "memcpy" option:
    Code:
    struct in_addr address, *ptr;
    ptr=(struct in_addr*)(info->h_addr);
    address.s_addr=ptr->s_addr;
    It works, but if I try dereferencing to save code:
    Code:
    struct in_addr address;
    address=*(struct in_addr)(info->h_addr);
    It passes the compiler but "address" is wrong.

    Of course this is not really a function call.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What are you trying to do?
    Just because you are assigning s_addr on both sides does not mean it will work if you omit them. Those fancy names are nothing but offsets to the compiler. Be very careful.
    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.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    hmm.

    So I was doing this, which I guess is basically the same as the "memcpy" option:
    Code:
    struct in_addr address, *ptr;
    ptr=(struct in_addr*)(info->h_addr);
    address.s_addr=ptr->s_addr;
    It works, but if I try dereferencing to save code:
    Code:
    struct in_addr address;
    address=*(struct in_addr)(info->h_addr);
    It passes the compiler but "address" is wrong.

    Of course this is not really a function call.
    First option is casting one pointer type to another. The second is trying to cast a type to another type, and that's only allowed for simple data types (including pointers), but not for example structs.

    You are basically saying:
    Code:
    struct A
    {
    ... 
    };
    
    struct B
    {
    ...
    };
    
    struct A thinga;
    struct B thingb;
    ...
    thinga = (struct A) thingb;
    And even if you have EXACTLY the same content on both sides, it's different types.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    What are you trying to do?
    Just because you are assigning s_addr on both sides does not mean it will work if you omit them.
    1) Well, technically I'm trying to get the information returned by gethostbyname() (which is what you would use with a string like www.internet.com) into a permanent in_addr, which is the "network order" (=bigendian) unint32 used by a lot of C socket/inet functions, or rather a struct containing such, which makes me wonder why it is a struct at all, since it doesn't have any other members besides the unint32_t.

    2) I think if you read your next sentence from the perspective of a person who is not you, you will be wondering what was meant.

    Thanks for the help tho. I seem to have "imagegrabber" working nicely...for now
    Last edited by MK27; 11-28-2008 at 11:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quadratic equation cpp program problems
    By faluiretoread in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2008, 03:22 PM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. Help needed! Finding Root of an Equation?
    By reader in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 10:10 AM
  4. Problem in accessing root home folder....
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 02-13-2008, 05:50 AM
  5. Binary Tree/Recursion
    By kwikness in forum C Programming
    Replies: 8
    Last Post: 12-14-2007, 10:01 AM