Sponsored Content
Top Forums Programming pass a pointer-to-pointer, or return a pointer? Post 302273937 by redoubtable on Tuesday 6th of January 2009 09:04:53 AM
Old 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

pointer

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

Regarding char Pointer

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

pointer problem

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

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

5. Programming

How to return void function pointer

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

matrix pointer

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

Pointer Arithmetic In C

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

Pointer to pointers

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

Pointer for 2D array seems to be 3D in C

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

Segmentation fault when I pass a char pointer to a function in C.

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_allocator(7rheolef)					    rheolef-6.1 					 stack_allocator(7rheolef)

NAME
stack_allocator - stack-based allocator DESCRIPTION
Stack-based allocator, conform to the STL specification of allocators. Designed to use stack-based data passed as a parameter to the allo- cator constructor. Does not "free" the memory. Assumes that if the allocator is copied, stack memory is cleared and new allocations begin at the bottom of the stack again. Also works with any memory buffer, including heap memory. If the caller passes in heap memory, the caller is responsible for freeing the memory. This allocator handles a limited area of memory: if this limit is reached, a "std::bad_alloc" exception is emmited. For a non-limited mem- ory handler in the same spirit, see "heap_allocator"(9). EXAMPLE
const size_t stack_size = 1024; vector<unsigned char> stack (stack_size); stack_allocator<double> stack_alloc (stack.begin().operator->(), stack.size()); typedef map <size_t, double, less<size_t>, stack_allocator<pair<size_t,double> > > map_type; map_type a (less<size_t>(), stack_alloc); a.insert (make_pair (0, 3.14)); a.insert (make_pair (1, 1.17)); for (map_type::iterator iter = a.begin(), last = a.end(); iter != last; iter++) { cout << (*iter).first << " " << (*iter).second << endl; } IMPLEMENTATION
template <typename T> class stack_allocator { protected: struct handler_type; // forward declaration: public: // typedefs: typedef size_t size_type; typedef std::ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; // constructors: stack_allocator() throw() : handler (new handler_type) { } stack_allocator (unsigned char* stack, size_t stack_size) throw() : handler (new handler_type (stack, stack_size)) { warning_macro ("stack_allocator cstor"); } stack_allocator (const stack_allocator& sa) throw() : handler (sa.handler) { ++handler->reference_count; } template <typename U> stack_allocator (const stack_allocator<U>& sa) throw() : handler ((typename stack_allocator<T>::handler_type*)(sa.handler)) { ++handler->reference_count; } ~stack_allocator() throw() { warning_macro ("stack_allocator dstor"); check_macro (handler != NULL, "unexpected null mem_info"); if (--handler->reference_count == 0) delete handler; } // Rebind to allocators of other types template <typename U> struct rebind { typedef stack_allocator<U> other; }; // assignement: stack_allocator& operator= (const stack_allocator& sa) { handler = sa.handler; ++handler->reference_count; return *this; } // utility functions: pointer address (reference r) const { return &r; } const_pointer address (const_reference c) const { return &c; } size_type max_size() const { return std::numeric_limits<size_t>::max() / sizeof(T); } // in-place construction/destruction void construct (pointer p, const_reference c) { // placement new operator: new( reinterpret_cast<void*>(p) ) T(c); } // C++ 2011: default construct a value of type T at the location referenced by p void construct (pointer p) { new ( reinterpret_cast<void*>(p) ) T(); } void destroy (pointer p) { // call destructor directly: (p)->~T(); } // allocate raw memory pointer allocate (size_type n, const void* = NULL) { warning_macro ("allocate "<<n<<" type " << typename_macro(T)); check_macro (handler->stack != NULL, "unexpected null stack"); void* p = handler->stack + handler->allocated_size; handler->allocated_size += n*sizeof(T); if (handler->allocated_size + 1 > handler->max_size) { warning_macro ("stack is full: throwing..."); throw std::bad_alloc(); } return pointer (p); } void deallocate (pointer p, size_type n) { warning_macro ("deallocate "<<n<<" type "<<typename_macro(T)); // No need to free stack memory } const handler_type* get_handler() const { return handler; } // data: protected: struct handler_type { unsigned char* stack; size_t allocated_size; size_t max_size; size_t reference_count; handler_type() : stack (NULL), allocated_size(0), max_size(0), reference_count(1) { warning_macro ("stack_allocator::mem_info cstor NULL"); } handler_type (unsigned char* stack1, size_t size1) : stack (stack1), allocated_size(0), max_size (size1), reference_count(1) { warning_macro ("stack_allocator::mem_info cstori: size="<<max_size); } ~handler_type() { warning_macro ("stack_allocator::mem_info dstor: size="<<max_size); } }; handler_type* handler; template <typename U> friend class stack_allocator; }; // Comparison template <typename T1> bool operator==( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw() { return lhs.get_handler() == rhs.get_handler(); } template <typename T1> bool operator!=( const stack_allocator<T1>& lhs, const stack_allocator<T1>& rhs) throw() { return lhs.get_handler() != rhs.get_handler(); } rheolef-6.1 rheolef-6.1 stack_allocator(7rheolef)
All times are GMT -4. The time now is 09:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy