LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Returning an object (https://www.linuxquestions.org/questions/programming-9/returning-an-object-79247/)

Mohsen 08-05-2003 01:08 PM

Returning an object
 
Is it possible to return an object (which is newed in the function) as the function return value? Or for instance an array of int?
Code:

int* MyFunc () {
  int* k = new int[10];
  return k;
}

or
Code:

int* MyFunc () {
  int RetVal[3] = {1, 2, 3}
  retrun RetVal;
}

As the rule of automatic variables, in the last code the memory allocated for the array (of int) should be freeed (spl?) before the end of function's block, so the values could not be retrieved somewhere by assigning function tio an array, for example:
Code:

main() {
  int a[10] = MyFunc();
}


kev82 08-05-2003 01:27 PM

Code listing 1 is perfectly fine, just remember to delete anything you new.

The second code listing as you say retval will be deallocated off the stack before MyFunc() returns and thus a 'wild' pointer will be returned. the 3rd code listing doesnt make sense you cant assign to an array like that i think what you meant was
Code:

main() {
    int *a = MyFunc();
}


magicvash 08-05-2003 10:59 PM

Re: Returning an object
 
Quote:

Originally posted by Mohsen

Code:

int* MyFunc () {
  int* k = new int[10];
  return k;
}


To use this, do exactly as you said.

Code:

int main() {

  int* myarray = MyFunc();
  // code continues...
  // ...
 delete [] myarray;
 return 0;
}


Mohsen 08-05-2003 11:11 PM

Thanks.
1. Is there the same case in C?
2. Is it explicit to compiler to determine the size of an array of something except char* returned from (passed to) a function? e.g.
Code:

int myfunc (int* k) {
  //...
}
main () {
  int k = 10;
  int v[] = {1, 2, 3};

  myfunc (&k); /*How does it determine that an array or a pointer to int is pased?*/
  myfunc (v);
}


magicvash 08-05-2003 11:53 PM

Quote:

1. Is there the same case in C?
What do you mean by that?

Quote:

2. Is it explicit to compiler to determine the size of an array of something except char* returned from (passed to) a function? e.g.
Code:

int myfunc (int* k) {
  //...
}
main () {
  int k = 10;
  int v[] = {1, 2, 3};

  myfunc (&k); /*How does it determine that an array or a pointer to int is pased?*/
  myfunc (v);
}


CS 101: pointer == array == pointer == array (etc..)
Essentially the variable you use to declare an array is a pointer to a an address that begins a contiguous block of memory. So if you declare an array of 10 int's, then on my system this is what will happen:

Code:

int v[] = {1,2,3}
This will cause 3*sizeof(int) bytes of contigious (i.e. all together) memory to be allocated and the address of the first byte is stored in v. so if you do:

Code:

printf("%d",v);
you should get the address of the first byte of the array 'v'

Another example of this is char* being used as a string. char* and char[] are almost the same (minus some memory management issues). so if i do

Code:

char v[3] = {'H',"i',0} // must have null char
char* w = "Hi";
printf("%s",v==w ? "yes" : "no);

I should get "yes" printed out since v is exactly the same as w

Hope that's what you're looking for

Mohsen 08-06-2003 07:48 AM

Thanks magicvash and kev82 for the help.

Quote:

Quote:

1. Is there the same case in C?
What do you mean by that?
1. I mean that for example we can return a structure from a function in ANSI C, and although this is (assume) an automatic variable, but can be retrieve from the scope out of function (from where it called). Why?


2. So we should always pass the size of array which is passed to a function.

ERROR :tisk: :
Quote:

Code:

char v[3] = {'H',"i',0} // must have null char
char* w = "Hi";
printf("%s",v==w ? "yes" : "no);

I should get "yes" printed out since v is exactly the same as w
v==w?
I think that it wont give you yes because the address of the first character of character pointer v is not the same as character pointer w, isn't it. That's why we should use strcmp except.

TheLinuxDuck 08-06-2003 07:58 AM

Mohsen:

Yes, you can do the same thing with C. You'd have to use malloc/calloc instead of new, though:
Code:

int* MyFunc () {
  int* k = malloc(sizeof(int) * 10);
  // k can be NULL or a valid memaddy at this point.
  return k;
}
main() {
  int *a = MyFunc();
}

Also, the v==w thing will print 'no', for the reason you stated.

magicvash 08-06-2003 08:48 AM

Quote:

v==w?
I think that it wont give you yes because the address of the first character of character pointer v is not the same as character pointer w, isn't it. That's why we should use strcmp except.
DURH :D
That's what I get for writing code at 2 am :)

Mohsen 08-06-2003 11:00 AM

TheLinuxDuck, I meant something like this: (in which an automatic variable is defined in the function block)
Code:

/* This is a C file */
struct S {
  int a, b;
};

struct S MyFunc () {
  struct S S_instance = {1, 2};
  /* ... */
  return S_instance;
}

main () {
  struct S S_ins;
  S_ins = MyFunc ();
}

When will the automatic variable S_instance free?

kev82 08-06-2003 12:35 PM

well you learn something every day, i didnt think you could pass objects like that in c only in c++ but according to gcc you can.

what hapens depends on the compiler but basically
return s_instance; - takes a copy of s_instance
} - end of block, s_instance deallocated
s_ins = myfunc() - the copy of s_instance made by the return statement is copied into s_ins and the copy is deallocated

Mohsen 08-06-2003 11:24 PM

No, Ritchie explicitly mentioned that we can both get a parameter or return of a structure.
Quote:

Let us investigate structures by writing some functions to manipulate points and rectangles. There are at least three possible approaches: pass components separately, pass an entire structure, or pass a
an also here is an example by him:
Code:


/* makepoint: make a point from x and y components */
struct point makepoint(int x, int y)
{
  struct point temp;
  temp.x = x;
  temp.y = y;
  return temp;

}


Mohsen 08-06-2003 11:36 PM

Something more
I have heard that there are some differences here between C and C++.
int C the automatic variable we be disposed as soon as the compiler reached to the next block (with the same depth), but in C++ the variable is disposed when its block finished.
here is an example:
Code:

//C++
class CPP{
public:
  int a, b;
};
CPP MyFunc () {
  CPP RetVal;
  /*...*/
  return RetVal; /* This -as I think- will be disposed at the
                              next line */
}



// C code
struct C {
  int a, b;
};

struct C MyFunc1 () {
  struct C RetVal;
  /*...*/
  return RetVal;
}

int MyFunc2 () {}

main () {
  struct C* CPtr;
  CPtr = MyFunc1 ();
  MyFunc2 (); /* I think after this line, our pointer will be a
                        wild one */
  return 0;
}

I tried hard to find something in Ritchie's or Stroustrap's book but I can't.

chens_83 08-07-2003 02:41 AM

Well , once a function has gone out of scope, the local variables are destroyed. (So to speak) . The variables that you allocate will remain constant in the heap until the program is killed. The wild pointer that you speak of wont be wild, because it is not on the heap, (no new , or malloc has been called) Remember a pointer is just like a int (4 byte) and they also go out of scope (destroyed) once the function has exited.

If you want to check out what is happening try using a debugger and watch your variables disappear once they are out of scope.


All times are GMT -5. The time now is 06:13 AM.