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
MEMUSAGE(1)							 Linux user manual						       MEMUSAGE(1)

NAME
memusage - profile memory usage of a program SYNOPSIS
memusage [option]... program [programoption]... DESCRIPTION
memusage is a bash script which profiles memory usage of the program, program. It preloads the libmemusage.so library into the caller's environment (via the LD_PRELOAD environment variable; see ld.so(8)). The libmemusage.so library traces memory allocation by intercepting calls to malloc(3), calloc(3), free(3), and realloc(3); optionally, calls to mmap(2), mremap(2), and munmap(2) can also be intercepted. memusage can output the collected data in textual form, or it can use memusagestat(1) (see the -p option, below) to create a PNG file con- taining graphical representation of the collected data. Memory usage summary The "Memory usage summary" line output by memusage contains three fields: heap total Sum of size arguments of all malloc(3) calls, products of arguments (nmemb*size) of all calloc(3) calls, and sum of length argu- ments of all mmap(2) calls. In the case of realloc(3) and mremap(2), if the new size of an allocation is larger than the previ- ous size, the sum of all such differences (new size minus old size) is added. heap peak Maximum of all size arguments of malloc(3), all products of nmemb*size of calloc(3), all size arguments of realloc(3), length arguments of mmap(2), and new_size arguments of mremap(2). stack peak Before the first call to any monitored function, the stack pointer address (base stack pointer) is saved. After each function call, the actual stack pointer address is read and the difference from the base stack pointer computed. The maximum of these differences is then the stack peak. Immediately following this summary line, a table shows the number calls, total memory allocated or deallocated, and number of failed calls for each intercepted function. For realloc(3) and mremap(2), the additional field "nomove" shows reallocations that changed the address of a block, and the additional "dec" field shows reallocations that decreased the size of the block. For realloc(3), the additional field "free" shows reallocations that caused a block to be freed (i.e., the reallocated size was 0). The "realloc/total memory" of the table output by memusage does not reflect cases where realloc(3) is used to reallocate a block of memory to have a smaller size than previously. This can cause sum of all "total memory" cells (excluding "free") to be larger than the "free/total memory" cell. Histogram for block sizes The "Histogram for block sizes" provides a breakdown of memory allocations into various bucket sizes. OPTIONS
-n name, --progname=name Name of the program file to profile. -p file, --png=file Generate PNG graphic and store it in file. -d file, --data=file Generate binary data file and store it in file. -u, --unbuffered Do not buffer output. -b size, --buffer=size Collect size entries before writing them out. --no-timer Disable timer-based (SIGPROF) sampling of stack pointer value. -m, --mmap Also trace mmap(2), mremap(2), and munmap(2). -?, --help Print help and exit. --usage Print a short usage message and exit. -V, --version Print version information and exit. The following options apply only when generating graphical output: -t, --time-based Use time (rather than number of function calls) as the scale for the X axis. -T, --total Also draw a graph of total memory use. --title=name Use name as the title of the graph. -x size, --x-size=size Make the graph size pixels wide. -y size, --y-size=size Make the graph size pixels high. EXIT STATUS
Exit status is equal to the exit status of profiled program. EXAMPLE
Below is a simple program that reallocates a block of memory in cycles that rise to a peak before then cyclically reallocating the memory in smaller blocks that return to zero. After compiling the program and running the following commands, a graph of the memory usage of the program can be found in the file memusage.png: $ memusage --data=memusage.dat ./a.out ... Memory usage summary: heap total: 45200, heap peak: 6440, stack peak: 224 total calls total memory failed calls malloc| 1 400 0 realloc| 40 44800 0 (nomove:40, dec:19, free:0) calloc| 0 0 0 free| 1 440 Histogram for block sizes: 192-207 1 2% ================ ... 2192-2207 1 2% ================ 2240-2255 2 4% ================================= 2832-2847 2 4% ================================= 3440-3455 2 4% ================================= 4032-4047 2 4% ================================= 4640-4655 2 4% ================================= 5232-5247 2 4% ================================= 5840-5855 2 4% ================================= 6432-6447 1 2% ================ $ memusagestat memusage.dat memusage.png Program source #include <stdio.h> #include <stdlib.h> #define CYCLES 20 int main(int argc, char *argv[]) { int i, j; int *p; printf("malloc: %zd ", sizeof(int) * 100); p = malloc(sizeof(int) * 100); for (i = 0; i < CYCLES; i++) { if (i < CYCLES / 2) j = i; else j--; printf("realloc: %zd ", sizeof(int) * (j * 50 + 110)); p = realloc(p, sizeof(int) * (j * 50 + 100)); printf("realloc: %zd ", sizeof(int) * ((j+1) * 150 + 110)); p = realloc(p, sizeof(int) * ((j + 1) * 150 + 110)); } free(p); exit(EXIT_SUCCESS); } BUGS
To report bugs, see <http://www.gnu.org/software/libc/bugs.html> SEE ALSO
memusagestat(1), mtrace(1) ld.so(8) GNU
2014-09-06 MEMUSAGE(1)
All times are GMT -4. The time now is 07:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy