01-06-2009
Quote:
Originally Posted by
aaronwong
I just want to know the advantages and disadvantages of those two methods by this simple example... actually, in practice, especially in memory management application for a special or embedded system, it is necessary to implement our own function to malloc a block of memory. Then the case raises.
Like everyone else I too prefer the second method one over the first as it is simpler, easier to understand and uses much less storage in the stack segment. Also I don't see the need for a function call just to malloc a chunk of memory. For an embedded system where resources are scarce you are better off mallocing that block of memory in main.
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 HPUX
xtrealloc
XtRealloc() XtRealloc()
Name
XtRealloc - change the size of an allocated block of storage.
Synopsis
char *XtRealloc(ptr, num);
char *ptr;
Cardinal num;
Inputs
ptr Specifies a pointer to memory allocated with XtMalloc(), XtCalloc(), or XtRealloc(), or NULL.
num Specifies the new number of bytes of memory desired in the block.
Returns
A pointer to allocated memory.
Description
XtRealloc() changes the size of the block of allocated memory pointed to by ptr to be at least num bytes large. In order to make this size
change, it may have to allocate a new block of memory and copy the contents of the old block (or as much as will fit) into the new block.
If it allocates a new block of memory, it frees the old block. In either case, it returns a pointer to a block of memory which is of the
requested size. If there is insufficient memory to allocate the new block, XtRealloc() terminates by calling XtErrorMsg().
If ptr is NULL, XtRealloc() simply calls XtMalloc() to allocate a block of memory of the requested size.
Usage
Note that XtRealloc() may move the contents of your allocated memory to a new location; the return value may or may not be the same as ptr.
Not all memory can be safely reallocated. If there are multiple pointers to a block of memory scattered through out an application (such
as pointers to a widget record), then reallocating that memory is not safe, because all pointers to it cannot be updated. Other memory
(such as the array of children maintained privately by the Composite widget class) can be safely updated because there should be only one
pointer to it in the application (in this case the pointer is the composite.children field of the widget). These cautions are no different
than those required with the standard realloc() function.
In most cases, you will have to cast the return value of XtRealloc() to an appropriate pointer type.
Note that because XtRealloc() behaves like XtMalloc() when passed a NULL pointer, (something that realloc() does not do), you don't have to
write special case code to allocate the first chunk of memory with XtMalloc() and subsequent chunks with XtRealloc(); you can simply use
XtRealloc() everywhere.
Memory allocated with XtRealloc() must be deallocated with XtFree(). The function XtRealloc() is implemented by the Toolkit independently
of the particular environment, so programs ported to a system not supporting malloc will still work.
See Also
XtCalloc(1), XtFree(1), XtMalloc(1), XtNew(1), XtNewString(1).
Xt - Memory Allocation XtRealloc()