ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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 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
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);
}
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
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 :
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.
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, 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 ();
}
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
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;
}
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.