Thread: Question on multiple return values

  1. #31
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by nonoob View Post
    I'd be more comfortable with calling menu like: menu(&principal, &rate, &months);, with those variables being defined in main.
    As am I.
    I merely want to make it clear, that yes, you can return data via an array, as distasteful to some as it may be.

  2. #32
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Steve A. View Post
    Not true.
    It's clearly in the code.
    No. What you're not saying is that your trick --and it is a trick-- only works for static types. I saw the declaration and that is why I pointed it out.
    Without the static modifier the array is disposed of, released from the stack, the instant the program returns... any pointer retuned to that variable is immediately invalid and very likely to produce incorrect results.



    No, it is an array.
    It is clearly declared as an array.
    But you are NOT returning the array... C can't do that. What you are doing is what I said; returning a pointer and choosing to treat it as an array.

    All that is clearly indicated within the code.
    I'm not keeping any secrets here.
    The beginner is going to learn about scope eventually.
    Yes they will. But unless you tell them the limitations and pitfalls of of the scheme you're using, you are indeed misleading them about what the language can and cannot do.

    No, actually, its because an array doesn't fit into the EAX register, but, a pointer to the array does.
    But, that's being too technical, isn't it.
    Yes that's part of it... but even a static array is an entity of unknown size. The language doesn't have a list someplace that says "this array is this big, that array is that big"... that information is simply not stored anywhere. You can declare a static int array of 3 elements inside your function, return a pointer to it then treat that pointer as a pointer to a 2 dimensional array of 30 x 50... neither the the compiler nor the runtime will know or care. Thus it is up to you to treat these things correctly. In the end it has very little to do with CPU registers and a lot more to do with careful programming, which these guys won't do unless they have appropriate information... Telling them C can return arrays because you know a trick that makes it look that way is simply wrong.


    No, actually Tater, I did think about it. And I knew you would bring it up.
    I'm well aware of the consequences of static variables.
    However, they are an established part of the C programming language.
    And, nearly all programming languages.
    You cannot ignore that they do exist.
    Nobody said any such thing... do not put words in my mouth to further your own arguments.


    Explain then why the text books and documentation go to lengths to describe the usage of these "bad" features.
    Why then, are they part of the ANSI Standard ?
    Nobody said it was a bad feature... you're putting words in my mouth again!

    What I did say is that showing them stuff like this without explaining why and how it works is likely to lead to them misunderstanding how the C language actually works.

  3. #33
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by CommonTater View Post
    No. What you're not saying is that your trick --and it is a trick-- only works for static types.
    What trick ??

    Quote Originally Posted by Wikipedia
    Static variable:
    From Wikipedia, the free encyclopedia:
    In computer programming , a static variable is a variable that has been allocated statically — whose lifetime extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated.
    In many programming languages, such as Pascal, all local variables are automatic and all global variables are allocated statically. In these languages, the term "static variable" is generally not used, since "local" and "global" suffice to cover all the possibilities.

    In the C programming language, the function of static variables can be illustrated as such:
    Code:
    #include <stdio.h>
     
    void func() {
            static int x = 0; // x is initialized only once across three calls of func()
    
            printf("%d\n", x); // outputs the value of x
            x = x + 1;
    }
     
    int main(int argc, char * const argv[]) {
            func(); // prints 0
            func(); // prints 1
            func(); // prints 2
            return 0;
    }
    C and related languages:
    In the C programming language (and its close descendants such as C++ and Objective-C), static is a storage class (not to be confused with classes in object-oriented programming), along with auto (for automatic variables), register (a variant of auto) and extern (for static variables and functions which are visible in other source files). Every variable in a C program is in exactly one of these storage classes.

    Every C variable declared as static has static lifetime and is not visible outside its own translation unit. There are other effects depending on where the variable is defined:
    Static global variables: variables declared as static at the top level of a source file (outside any function definitions) are only visible throughout that file ("file scope").
    Static local variables: variables declared as static inside a function are statically allocated while having the same scope as automatic local variables. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again.
    static member variables: in C++, member variables declared as static inside class definitions are class variables (shared between all class instances, as opposed to instance variables).
    Nowhere is "static" equated with "trick".

    But you are NOT returning the array... C can't do that. What you are doing is what I said; returning a pointer and choosing to treat it as an array.
    Because that's how you do it, Tater.

    Nobody said any such thing... do not put words in my mouth to further your own arguments.
    What are you referring to here ??

    Nobody said it was a bad feature... you're putting words in my mouth again!
    Maybe...by your response ...? I don't know...

  4. #34
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Steve A. View Post
    Because that's how you do it, Tater.
    Well, actually no... because that's how the LANGUAGE does it.
    Like you said, you can't return an array because it won't fit in the register.

    Ok... since you seem far more interested in the argument than the facts... one more try at this before I ignore you...

    I did not say that what you are doing is wrong. I've used it myself, when it's appropriate. ... ok, got that?

    What I did point out is that although it appears to return an array, all it's really doing is returning a pointer that you are choosing to treat as an array in your code. The point was not what you are doing... the point was that by not providing an explaination of how it works, people will mistakenly believe that C can return an actual array and until they learn about static modifiers and rules of scope, your unexplained trick is likely to cause them some misunderstaning of how C actually works... Get it? I didn't say your code was wrong... you simply should have explained it far enough to avoid misunderstandings.

    Or in more direct terms... Don't be telling people C does stuff it does not know how to do.

    (oh yeah... and welcome to my ignore list!)

  5. #35
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    ???...listen...
    is that the sound of a bicycle being peddled backwards ?

  6. #36
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Steve A. View Post
    Well, saying that is not exactly correct either.
    From the perspective of the beginner, they don't care about the symantics.

    you can also return an array (or more specifically, a pointer to an array) to a calling function,
    like so:
    Code:
        myArray = call_MyFunction();
    we do it all the time.
    Whether we are returning an array or a pointer to an array, it's symantics to the beginner.
    The point is, it can be done and is done.
    To imply that C cannot return an array places a restriction on C which does not exist.
    Yes, you can do it but I think you're bringing up something irrelevant. It's funny that you say beginners don't care about semantics, but you won't admit what's factually true about the examples you post. I think beginners should care about semantics (it's not like they go away), and I'm not going to say something wrong just because you want to say you can return arrays.

    There are problems with the static array approach "We" don't "do it all the time" because:

    • it's not thread safe
    • you will be recycling the same array for each invocation of the function
    • therefore, copy the pointed-to data if you want to keep it
    • you will be returning a pointer to that array's first element, which should not be freed

    If the programmer cares about any one of these at least, then static is not the answer.

    But I've been up front before in saying that my answer is only one way to get around the return value semantics. I mentioned that you could use multiple pointers, instead of an array. It just seemed to be the one NinjaFish was interested in. I'm sorry.
    Last edited by whiteflags; 04-18-2011 at 04:44 PM.

  7. #37
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    There are problems with the static array approach "We" don't "do it all the time" because:
    • it's not thread safe
    • you will be recycling the same array for each invocation of the function
    • therefore, copy the pointed-to data if you want to keep it
    • you will be returning a pointer to that array's first element, which should not be freed

    If the programmer cares about any one of these at least, then static is not the answer.
    Why using static arrays and returning pointers is a very bad idea...
    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    As I said in my original post about this... What happens if you call the function twice?

    I'm thinking our friend Steve A who "does this all the time" just might have some serious debugging to do...
    Last edited by CommonTater; 04-18-2011 at 07:03 PM.

  8. #38
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by CommonTater View Post
    Why using static arrays and returning pointers is a very bad idea...
    Quote Originally Posted by CommonTater
    Nobody said it was a bad feature... you're putting words in my mouth again!
    Why does everything here (this forum) have to turn into a pi_ss_ing contest ?
    Tater, you and I both know your example is from page-1, chapter-1, of Pointers-101.

    Everyone who learns about pointers learns about the side-effects of using them. Big-wup.
    Anybody can take just about any C method and illustrate a worst case senario.
    Your example is simply that.
    Here I thought that maybe you really had something to show us.
    I'm a bit disappointed that you didn't have something more interesting.

    Just because I don't share the same religious ferver about C that you do, it doesn't automatically make me wrong and like wise the opposite.
    We simply have differing opinions on certain subjects.

    It's my belief that a student of C should be exposed to every aspect of the language and not be told that "no you can't use that, because I don't like it".

    Your example could be better served, instead, by saying: "if you do decide to use pointers to return values, be aware of the side-effects" and then show your example. Just like the textbooks do on the very same subject.

  9. #39
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Steve A. View Post
    Why does everything here (this forum) have to turn into a pi_ss_ing contest ?
    Pretty simple really... you posted bad advice, didn't explain the pitfalls... You tried to tell people C can do something it cannot.

    You got tagged for it.
    THEN you decided to push the argument, even to the point of claiming I'd said things I did not.

    It's a contest because you made it into one.

    Even now, when there is proof of your misstatements right before your eyes... you STILL persist in trying to be right.
    It's kinda sad really... My better half, outspoken lady she is, would call it "testosterone poisoning".

    Really.. you screwed up... admit it and move on.
    (With God as my witness, I promise you, your testicles will not fall off if you admit an error)
    Last edited by CommonTater; 04-19-2011 at 08:59 AM.

  10. #40
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by Steve A. View Post
    No, actually, its because an array doesn't fit into the EAX register, but, a pointer to the array does.
    But, that's being too technical, isn't it.
    Um. The size of EAX register has very little to do with what can and can not be returned.
    In fact, functions can return any data type including a huge struct which may contain arrays. You will see memory-move instructions generated for such a case.

  11. #41
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by CommonTater View Post
    You tried to tell people C can do something it cannot.
    Clearly, I supplied examples of how it does.

    You got tagged for it.
    ???
    More like I got flamed for treading on your turf.

  12. #42
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by nonoob View Post
    In fact, functions can return any data type including a huge struct which may contain arrays.
    That is exactly my point.

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Steve A.
    Clearly, I supplied examples of how it does.
    You didn't: the fact is that one cannot return an array in C. It is possible to return a pointer to the first element of an array and thereby simulate returning an array, but it is not the same thing. It is possible to return a struct object that contains an array, but it is not the same thing.

    Quote Originally Posted by Steve A.
    That is exactly my point.
    Next time, illustrate your point instead of illustrating something else.
    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

  14. #44
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Edit: I missed seeing laserlight post; need to refresh more often.

    Steve A: In the C Language Arrays are treated as Pointers when passed on the stack.
    Recognize that fact; then think about what CommonTater said. He is much more right than you are.

    Tim S.
    Last edited by stahta01; 04-19-2011 at 09:48 AM. Reason: Grammer

  15. #45
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Steve A. View Post
    Clearly, I supplied examples of how it does.
    C DOES NOT return arrays. It cannot because it does not know their size. You used a clever programming trick to make it seem like it does... and I provided a simple, clear, example of why it's a bad idea to use your trick in real programs. Simply because returning pointers couples all arrays back to the static one in the function... use that thing twice and you end up with array values that change "by magic" whenever you call the function. On it's surface it looks real clever... in reality it's a patently stupid thing to do unless you know exactly the consequences of doing it... which you either did not or could not explain. Frankly I'm guessing you discovered this trick of yours and thought yourself ever so clever, remaining blissfully unaware of it's consequences.

    More like I got flamed for treading on your turf.
    No sir... I have no turf. Such stupid notions are the premise of an immature mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how is it possible to return multiple values?
    By Masterx in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2009, 06:49 PM
  2. How to use return values
    By Furious5k in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2009, 09:07 PM
  3. Return values
    By pritin in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2007, 05:24 PM
  4. Replies: 4
    Last Post: 03-11-2005, 05:45 PM
  5. C++ .Net DLL return values
    By shuesty in forum C++ Programming
    Replies: 15
    Last Post: 01-19-2003, 05:39 PM