If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
(2)the other is to return a pointer, like:
Could anyone tell the difference(advantages and disadvantages )about those two methods ?
Thanks in advance !
Last edited by Franklin52; 01-06-2009 at 07:17 AM..
Reason: adding code tags
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)
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)
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)
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)
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)
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)
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 HPUX
bsdmalloc
bsdmalloc(3MALLOC)bsdmalloc(3MALLOC)NAME
bsdmalloc - memory allocator
SYNOPSIS
cc [ flag ... ] file ... -lbsdmalloc [ library ... ]
char *malloc(size);
unsigned size;
int free( ptr);
char *ptr;
char *realloc( ptr, size);
char *ptr;
unsigned size;
These routines provide a general-purpose memory allocation package. They maintain a table of free blocks for efficient allocation and coa-
lescing of free storage. When there is no suitable space already free, the allocation routines call sbrk(2) to get more memory from the
system. Each of the allocation routines returns a pointer to space suitably aligned for storage of any type of object. Each returns a
null pointer if the request cannot be completed.
The malloc() function returns a pointer to a block of at least size bytes, which is appropriately aligned.
The free() function releases a previously allocated block. Its argument is a pointer to a block previously allocated by malloc() or real-
loc(). The free() function does not set errno.
The realloc() function changes the size of the block pointed to by ptr to size bytes and returns a pointer to the (possibly moved) block.
The contents will be unchanged up to the lesser of the new and old sizes. If the new size of the block requires movement of the block, the
space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the
block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer,
the space pointed to is freed.
The malloc() and realloc() functions return a null pointer if there is not enough available memory. They return a non-null pointer if size
is 0. These pointers should not be dereferenced. When realloc() returns NULL, the block pointed to by ptr is left intact. Always cast the
value returned by malloc() and realloc().
If malloc() or realloc() returns unsuccessfully, errno will be set to indicate the following:
ENOMEM size bytes of memory cannot be allocated because it exceeds the physical limits of the system.
EAGAIN There is not enough memory available at this point in time to allocate size bytes of memory; but the application could try
again later.
Using realloc() with a block freed before the most recent call to malloc() or realloc() results in an error.
Comparative features of the various allocation libraries can be found in the umem_alloc(3MALLOC) manual page.
brk(2), malloc(3C), malloc(3MALLOC), mapmalloc(3MALLOC), umem_alloc(3MALLOC)WARNINGS
Use of libbsdmalloc renders an application non-SCD compliant.
The libbsdmalloc routines are incompatible with the memory allocation routines in the standard C-library (libc): malloc(3C), alloca(3C),
calloc(3C), free(3C), memalign(3C), realloc(3C), and valloc(3C).
21 Mar 2005 bsdmalloc(3MALLOC)