Skip to content

Commit 9845df9

Browse files
committed
Refine my_calloc
Use actual allocated block size in bytes rather than the requested bytes to initialize memory to zeros.
1 parent 4c7f300 commit 9845df9

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

chapter08/8-6.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* Exercise 8-6. The standard library function calloc(n, size) returns a
33
* pointer to n objects of size size, with the storage initialized to zero.
44
* Write calloc, by calling malloc or by modifying it.
5+
*
6+
* Note: to clear memory, you could use memset function in my_calloc instead,
7+
* but without measures faster.
8+
*
59
* By Faisal Saadatmand
610
*/
711

@@ -62,17 +66,22 @@ void *knr_malloc(unsigned nbytes)
6266
prevp = p; /* save current pointer's address */
6367
}
6468
}
65-
#include <string.h>
69+
6670
/* my_calloc: general-purpose storage allocator. Initialize memory to zeros */
6771
void *my_calloc(unsigned n, unsigned size)
6872
{
6973
unsigned char *p; /* char is exactly 1 byte */
74+
Header *hp;
75+
unsigned bsize; /* actual block size in bytes */
7076
unsigned i;
7177

72-
if ((p = (unsigned char *) knr_malloc(n * size)) != NULL)
73-
for (i = 0; i < n * size; i++)
78+
if ((p = (unsigned char *) knr_malloc(n * size)) != NULL) {
79+
hp = (Header *) p - 1;
80+
bsize = (hp->s.size - 1) * sizeof(Header);
81+
for (i = 0; i < bsize - 1; i++)
7482
p[i] &= 0x0u; /* clear each byte */
75-
return (void *) p;
83+
}
84+
return (void *) p;
7685
}
7786

7887
/* morecore: ask system for more memory */

chapter08/8-7.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ void *knr_malloc(unsigned nbytes)
7070
/* my_calloc: general-purpose storage allocator. Initialize memory to zeros */
7171
void *my_calloc(unsigned n, unsigned size)
7272
{
73-
unsigned char *p; /* char is exactly 1 byte */
73+
unsigned char *p; /* char is exactly 1 byte */
74+
Header *hp;
75+
unsigned bsize; /* actual block size in bytes */
7476
unsigned i;
7577

76-
if ((p = (unsigned char *) knr_malloc(n * size)) != NULL)
77-
for (i = 0; i < n * size; i++)
78+
if ((p = (unsigned char *) knr_malloc(n * size)) != NULL) {
79+
hp = (Header *) p - 1;
80+
bsize = (hp->s.size - 1) * sizeof(Header);
81+
for (i = 0; i < bsize - 1; i++)
7882
p[i] &= 0x0u; /* clear each byte */
79-
return (void *) p;
83+
}
84+
return (void *) p;
8085
}
8186

8287
/* morecore: ask system for more memory */

chapter08/8-8.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@ void *knr_malloc(unsigned nbytes)
7171
void *my_calloc(unsigned n, unsigned size)
7272
{
7373
unsigned char *p; /* char is exactly 1 byte */
74+
Header *hp;
75+
unsigned bsize; /* actual block size in bytes */
7476
unsigned i;
7577

76-
if ((p = (unsigned char *) knr_malloc(n * size)) != NULL)
77-
for (i = 0; i < n * size; i++)
78+
if ((p = (unsigned char *) knr_malloc(n * size)) != NULL) {
79+
hp = (Header *) p - 1;
80+
bsize = (hp->s.size - 1) * sizeof(Header);
81+
for (i = 0; i < bsize - 1; i++)
7882
p[i] &= 0x0u; /* clear each byte */
79-
return (void *) p;
83+
}
84+
return (void *) p;
8085
}
8186

8287
/* morecore: ask system for more memory */

0 commit comments

Comments
 (0)