Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
I wrote a little wrapper around malloc ...
I wrote a little wrapper around malloc, but it doesn't work:
#include lt;stdio.h>
#include lt;stdlib.h>
mymalloc(void *retp, size_t size)
{
retp = malloc(size);
if(retp == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
}
✍: Guest
Are you sure the function initialized what you thought it did? Remember that arguments in C are passed by value. In the code above, the called function alters only the passed copy of the pointer. To make it work as you expect, one fix is to pass the address of the pointer (the function ends up accepting a pointer-to-a-pointer; in this case, we're essentially simulating pass by reference):
void f(ipp)
int **ipp;
{
static int dummy = 5;
*ipp = &dummy;
}
...
int *ip;
f(&ip);
Another solution is to have the function return the pointer:
int *f()
{
static int dummy = 5;
return &dummy;
}
...
int *ip = f();
2016-06-10, 1712👍, 0💬
Popular Posts:
What are the two kinds of comments in JSP and what's the difference between them? <%-- JSP Co...
What’ is the sequence in which ASP.NET events are processed ? Following is the sequence in which the...
How To Decrement Dates by 1? - MySQL FAQs - Introduction to SQL Date and Time Handling If you have a...
How To Add Column Headers to a Table? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells I...
Can Two Forms Be Nested? - XHTML 1.0 Tutorials - Understanding Forms and Input Fields Can two forms ...