CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015

    Call function with pointers

    Hi. Suppose I want to implement a function like this:

    void function_name(int length, short *array)
    {
    array = (short*)malloc(sizeof(short)*length);
    ....
    ....
    }

    This unfortunatelly doesn't work. To work I must place the allocation line (array = (short*)malloc(sizeof(short)*length)
    in my main(), just before calling the function.
    Why is this happening?
    Thanx
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640
    If you are trying to get the function to allocate memory for you, then you either need to pass the array pointer by value, or pass in a pointer to the pointer to the array.

    Currently, you are passing the array pointer by reference, therefore the function will not be able to modify it.

    Pass by value:
    Code:
    void function_name(int length, short* &array)
    {
      array = (short*)malloc(sizeof(short)*length);
    ....
    ....
    }
    Or, pointer to pointer:
    Code:
    void function_name(int length, short **array)
    {
    *array = (short*)malloc(sizeof(short)*length);
    ....
    ....
    }
    
    int main()
    {
      short *myShort;
      function_name(3, &myShort);
    }
    Viggy

  3. #3
    Join Date
    Dec 2001
    Location
    Greece, Athens
    Posts
    1,015
    Thanx alot MrViggy.
    Theodore
    Personal Web Page (some audio segmentation tools): www.di.uoa.gr/~tyiannak

  4. #4
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by MrViggy
    Currently, you are passing the array pointer by reference, therefore the function will not be able to modify it.
    It is being passed as:
    Code:
    short *array
    Where do you see a reference? However it (the array pointer) is passed by reference in your solution.

    By definition of the term "by reference" it should be modifiable if it is passed "by reference".

    The terms "by reference" and "by value" can be very confusing when used for C/C++ in the manner they are used for other languages. Those terms have virtually no use in the context of the C language and a C++ reference (& operator) is different from a reference for other languages.

    Of course, you probably meant to say that the array pointer was being passed "by value", and if so, then that might be an example of how confusing the terminology is when it is used for C/C++.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  5. #5
    Join Date
    Jun 2000
    Location
    austin
    Posts
    101
    mrviggy, you are getting the definitions of "pass by value" and "pass by reference" backwards.

    when you pass by value, the function gets a >copy< of the orginial value. thus, whatever changes are made to that var are not seen in the calling scope.

  6. #6
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Sam: you have to be careful about what "by reference" and "by value" refer to, but the terms make perfect sense in C and C++.
    All the buzzt
    CornedBee

  7. #7
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by CornedBee
    Sam: you have to be careful about what "by reference" and "by value" refer to, but the terms make perfect sense in C and C++.
    Actually, it is possible to say that in C and C++ everything is passed by value, including references.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  8. #8
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    No. In every language you can say that SOMETHING is passed by value, but the thing you want to refer to (like a variable) may not be passed by value.

    The question is whether the method or the subject is fixed.
    All the buzzt
    CornedBee

  9. #9
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Please beleive me that not everyone agrees with that. Many people would say that in:
    Code:
    void Func(char *String)
    String is passed by reference and many people would say that String is passed by value. As a pointer, String is passed by value, but as a string, String (using non-C++ terminology) is passed by reference.

    I think I had a similar discussion a while back (I think over a year ago) with Paul McKenzie.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449
    Everything in C is passed by value. When a parameter is char *, the type is pointer to char. Since a pointer is an integral value (and it must be an integral value), it is no different than passing an int, double, float, etc. Now in the called function what you do with the value is another issue. If it's a pointer, the value can be dereferenced.

    In C++, you have a pass-by-reference in which reference types are used. A reference is not a true "value", in that it is theoretically dimensionless -- it has no "size" (you can get the sizeof() what is being referred, but you can't get the sizeof() a reference), plus many other things that you can do with true value types that disqualifies references.

    Basically, a reference is a C++ invention, and any compiler can come up with any way it wants when representing what a reference really is. For most compiler's, a reference does boil down to a pointer of some sort, but it isn't a hard, fast, rule. This is why it is legitimate to state that pass-by-reference and pass-by-value are two different and well-defined concepts when referring to C and C++. For other languages, then it may mean different things.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Jun 2000
    Location
    austin
    Posts
    101
    Originally posted by Sam Hobbs
    Please beleive me that not everyone agrees with that. Many people would say that in:
    Code:
    void Func(char *String)
    String is passed by reference and many people would say that String is passed by value. As a pointer, String is passed by value, but as a string, String (using non-C++ terminology) is passed by reference.

    I think I had a similar discussion a while back (I think over a year ago) with Paul McKenzie.
    whats the point of picking things to death? why make such a big fuss over simple terminology that you learn in programming 101?

    Code:
    void f(int x); //pass by value
    void f(int &x); //pass by reference
    void f(int *x); //pass by reference by pointer
    void f(char *str); //the char pointer is passed by value, but the "string" is passed by reference.  i'd say its "pass by reference by pointer"
    i don't see how or >why< the above c++ terminology should be debated.

    -- C

  12. #12
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by clockworks
    i don't see how or >why< the above c++ terminology should be debated.
    Then ignore this thread. I assume you don't look at every post in every thread. If you are getting email notifications of replies to this thread, you can usubscribe to getting email notifications of replies to this thread.

    For those of us that have seen thousands of posts in this forum, we know how important it is to be accurate in terminology. You might not get help when you ask for it if you are not clear and accurate about what you are asking.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  13. #13
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    In other languages, when an item is passed "by reference", it is modifiable. If it is not modifiable, it is passed "by value". The modifyability of a parameter is the inherent destinction that defines the terms.

    In C++, beginners can get confused when something is passed as a "LPSTR". It is not a reference, right? Therefore the parameter is passed "by value" and not modifiable, right? Of course experienced C++ programmers understand, but this could be confusing for beginners.

    Again, the terms "by value" and "by reference" are intended to differentiate non-modifiable and modifiable parameters (respectively), yet in C++ many parameters that are not references are often considered to be modifiable. When someone understands C++ well enough to understand that something is modifiable only if the parameter is a pointer to it, then they understand well enough that we don't need to use the terms "by value" and "by reference".
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  14. #14
    Join Date
    Jun 2000
    Location
    austin
    Posts
    101
    Originally posted by Sam Hobbs
    Then ignore this thread. I assume you don't look at every post in every thread. If you are getting email notifications of replies to this thread, you can usubscribe to getting email notifications of replies to this thread.

    For those of us that have seen thousands of posts in this forum, we know how important it is to be accurate in terminology. You might not get help when you ask for it if you are not clear and accurate about what you are asking.
    but the member who posted this thread could have got a very simply answer to his question. instead, he gets bombarded with discussion of ultra-technicalities by the seasoned veterans.

    i think my post outlined exactly what he needed to know as a novice, without being confused with discussions about "computer philosopy" instead of "computer science".

    i agree with you that when you post questions, you must be as articulate as possible or you will receive no help or wrong help. but this discussion of articulation, i believe, is more confusing than helpful...especially for a newbie.

    -- christopher

  15. #15
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by clockworks
    but the member who posted this thread could have got a very simply answer to his question. instead, he gets bombarded with discussion of ultra-technicalities by the seasoned veterans.
    Note that the first reply above says "you either need to pass the array pointer by value, or pass in a pointer to the pointer to the array". However passing an array pointer by value is not what is needed; if the terminology being discussed here is used, then it would be that the array pointer needs to be passed by reference. The sample caaled "Pass by value" is good, but there is no need for the pass "by value" terminology, and instead of helping, it is confusing.
    Originally posted by clockworks
    but this discussion of articulation, i believe, is more confusing than helpful...especially for a newbie.
    Discussions such as this do happen in this forum. That's the way it works. Sometimes the conversations don't help the person that created the thread, but if they result in something that helps others, then it usually is worthwhile.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured