01-07-2009
Quote:
Originally Posted by
aaronwong
Thanks to you all for your comments!
So it is believed that:
(1) there is no actual functional difference between the two methods;
Not as you've written them, no.
Quote:
(2) it is needed to pass a pointer-to-pointer to a function only if the original value of the "pointer-to-pointer" parameter is used in the function, or we want to change its value in this function. Otherwise, it only increases the function invoking overhead.
I think that's correct, though pointers always make for awkward language.
Quote:
(3) it should be careful when return a local pointer value in a function, because a pointer pointing to stack space(local variables) returned to the invoker points to an invalid space. The pointer returned from a subroutine should point to heap space(like malloc()) or global&static variables.
Quite right, stack variables are only valid until the function returns. You
can give stack pointers to functions you're calling however, just like you did inside the main() of your first example, since that function is guaranteed to return before main() does.
10 More Discussions You Might Find Interesting
1. Programming
void main()
{
int a={1,2,3,4,5,6,7,8,9,10};
int *p=a;
int *q=&a;
cout<<q-p+1<<endl;
}
The output is 10, how?
if we give cout<<q it will print the address, value won't print....
if we give cout<<p it will print the address, value won't print....
p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
2. Programming
Hi,
char *s="yamaha";
cout<<s<<endl;
int *p;
int i=10;
p=&i;
cout<<p<<endl;
1) For the 1st "cout" we will get "yamaha" as output. That is we are getting "content of the address" for cout<<s.
2) But for integer "cout<<p" we are getting the "address only".
Please clarify how we are... (2 Replies)
Discussion started by: sweta
2 Replies
3. Programming
could any one tell why the following is showing segmentation fault while using **ptr
but working fine using **a
#include<stdio.h>
... (1 Reply)
Discussion started by: useless79
1 Replies
4. Programming
what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies
5. Programming
Hello all
im trying to build function that will return void function pointer
what is mean is ( not working )
the main function
void * myClass::getFunction(int type){
if(type==1)
return &myClass::Test1;
if(type==2)
return &myClass::Test2;
}
void myClass::Test1(){... (1 Reply)
Discussion started by: umen
1 Replies
6. Programming
Can anyone tell me what the following statements do?
float (*tab);
tab=(float (*)) calloc(MAXCLASS,
(MAXCLASS+1)*sizeof(float)); (3 Replies)
Discussion started by: littleboyblu
3 Replies
7. Programming
I have a fundamental question on C pointer arithmetry..
Suppose i have a c string pointer already pointing to a valid location, Can I just do a
charptr = charptr +1;
to get to the next location, irregardless if my program is 32 or 64 bits?
or should i do it this way:
charptr =... (1 Reply)
Discussion started by: Leion
1 Replies
8. Programming
Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It.
Can someone help?
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int f1(char **str_);
int main(int argc, char **argv)
{
char *str = NULL;
f1(&str);
... (3 Replies)
Discussion started by: pharaoh
3 Replies
9. Programming
I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here... (1 Reply)
Discussion started by: yifangt
1 Replies
10. Programming
I am passing a char* to the function "reverse" and when I execute it with gdb I get:
Program received signal SIGSEGV, Segmentation fault.
0x000000000040083b in reverse (s=0x400b2b "hello") at pointersExample.c:72
72 *q = *p;
Attached is the source code.
I do not understand why... (9 Replies)
Discussion started by: jose_spain
9 Replies
STACK(5) File Formats Manual STACK(5)
NAME
stack - 2.10BSD PDP-11 C stack frame conventions
DESCRIPTION
The standard C stack frame layout:
------------------
|...nth argument | push arguments in reverse order
------------------
|second argument |
------------------
| first argument |
------------------ JSR PC,*$_FOO
| return address |
------------------ JSR R5,CSV
| old R5 value | <-----
------------------ |
|previous overlay| |
| number | |
------------------ |
| r4 | |
------------------ |
| r3 | |
------------------ |
| r2 | |
------------------ |
| first local var| | This is the top of the stack
------------------ | when the called routine ``starts''
| routine | |
| allocates | |
| storage | | SUB $n,SP
| temporary | |
------------------ |
| push arguments | |
| of next routine| |
------------------ | JSR PC,*$_BAR
| return address | |
------------------ | JSR R5,CSV
| old R5 value---+-------
------------------ ^
|previous overlay| |
| number | |
------------------ |
| r4/43/r2/... | |
------------------
| and so on..... |
The stack pushes downward through memory addresses. Overlay numbers saved in non-overlaid objects are always zero, but the simplification
of not having to maintain two different stack frame formats more than outweighs the extra few micro seconds (less than four) necessary to
save the zero ...
Functions returning integers leave their return value in R0; functions returning floating constants use FR0; functions returning longs
leave return values in R1/R0 (R0 high word, R1 low); functions returning structures leave a pointer to bss storage (one chunk of which is
allocated for each such routine) in R0, and the caller will copy from that bss storage to the local destination.
Local variables are allocated in such a way that they are referred to as ``-N(R5)'', arguments are referred to as ``+N(R5)''; arguments
start at 4(R5), the first integer local declared will be at -10(R5).
The SP normally points at the first word available for parameter pushing. A function taking only single word as a parameter can be called
simply by moving the parameter into (SP) and calling the function, without having to clean the parameter off the stack on return. Any
parameters passed after the first (actually "Nth") must be pushed before the call and cleaned off afterwards. If the function has no local
variables and calls no functions, it will allocate no stack and the word labelled ``first local var'' will be unused.
It is important to note that routines know how many arguments they pass to a function, and will adjust the stack accordingly after a func-
tion returns.
NOTE
This stack frame format is the same as that used by overlaid objects in 2.9BSD.
AUTHOR
John F. Woods, MIT Concouse Computer Center
3rd Berkeley Distribution STACK(5)