01-06-2009
The actual difference is that the first example copies the address of your pmem pointer to the stack and the second example doesn't. The second example only works because you're using malloc() which allocates memory from the heap, otherwise you could have unexpected behavior because you would be returning a local variable (which is cleared upon function completion).
IMHO the second option would be faster because there's one less parameter to be copied to the stack and there's no desreferencing of the pointer's address to obtain the data. Anyway, you should feel no significant speed difference in any of the alternatives.
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
LEARN ABOUT SUNOS
sigstack
sigstack(3UCB) SunOS/BSD Compatibility Library Functions sigstack(3UCB)
NAME
sigstack - set and/or get signal stack context
SYNOPSIS
/usr/ucb/cc [ flag ... ] file ...
#include <signal.h>
int sigstack( nss, oss);
struct sigstack *nss, *oss;
DESCRIPTION
The sigstack() function allows users to define an alternate stack, called the "signal stack", on which signals are to be processed. When a
signal's action indicates its handler should execute on the signal stack (specified with a sigvec(3UCB) call), the system checks to see if
the process is currently executing on that stack. If the process is not currently executing on the signal stack, the system arranges a
switch to the signal stack for the duration of the signal handler's execution.
A signal stack is specified by a sigstack() structure, which includes the following members:
char *ss_sp; /* signal stack pointer */
int ss_onstack; /* current status */
The ss_sp member is the initial value to be assigned to the stack pointer when the system switches the process to the signal stack. Note
that, on machines where the stack grows downwards in memory, this is not the address of the beginning of the signal stack area. The
ss_onstack member is zero or non-zero depending on whether the process is currently executing on the signal stack or not.
If nss is not a null pointer, sigstack() sets the signal stack state to the value in the sigstack() structure pointed to by nss. If nss is
a null pointer, the signal stack state will be unchanged. If oss is not a null pointer, the current signal stack state is stored in the
sigstack() structure pointed to by oss.
RETURN VALUES
Upon successful completion, 0 is returned. Otherwise, -1 is returned and errno is set to indicate the error.
ERRORS
The sigstack() function will fail and the signal stack context will remain unchanged if one of the following occurs.
EFAULT Either nss or oss points to memory that is not a valid part of the process address space.
SEE ALSO
sigaltstack(2), sigvec(3UCB), signal(3C)
WARNINGS
Signal stacks are not "grown" automatically, as is done for the normal stack. If the stack overflows unpredictable results may occur.
NOTES
Use of these interfaces should be restricted to only applications written on BSD platforms. Use of these interfaces with any of the system
libraries or in multi-threaded applications is unsupported.
SunOS 5.10 22 Jan 1997 sigstack(3UCB)