Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
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, 1931👍, 0💬
Popular Posts:
What is NullPointerException and how to handle it? When an object is not initialized, the default va...
How To Escape Special Characters in SQL statements? - MySQL FAQs - Introduction to SQL Basics There ...
How To Use Subqueries with the IN Operator? - MySQL FAQs - SQL SELECT Statements with JOIN and Subqu...
Can we use the constructor, instead of init(), to initialize servlet? Yes , of course you can use th...
Explain simple Walk through of XmlReader ? In this section we will do a simple walkthrough of how to...