Categories:
.NET (357)
C (330)
C++ (184)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (2)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Struts (1)
Windows (1)
XHTML (173)
Other Resources:
I im dynamically allocating an array, like this ...
I'm dynamically allocating an array, like this:
int *iarray = (int *)malloc(nints);
malloc isn't returning NULL, but the code isn't working.
✍: Guest
A:malloc is a low-level, typeless allocator. It doesn't know how you're going to use the memory; all it does is to allocate as many bytes of memory as you ask it. Therefore (except when you're allocating arrays of char) you must multiply by the size of the elements in the array you're allocating:
int *iarray = malloc(nints * sizeof(int));
or
int *iarray = malloc(nints * sizeof(*iarray));
(The latter fragment can be more reliable if the type of iarray might change, since there's only one place to change it. Also, the casts have been removed;
2016-04-06, 2080👍, 0💬
Popular Posts:
.NET INTERVIEW QUESTIONS - What is the difference between thread and process? A thread is a path of ...
How Oracle Handles Dead Locks? - Oracle DBA FAQ - Understanding SQL Transaction Management Oracle se...
What is Traceability Matrix? Traceability Matrix is one of the document will prepare by QA.To make s...
How To Retrieve the Count of Updated Rows? - Oracle DBA FAQ - Working with Database Objects in PL/SQ...
How do you locate the first X in a string txt? A) txt.find('X'); B) txt.locate('X'); C) txt.indexOf(...