File tree Expand file tree Collapse file tree 3 files changed +16
-17
lines changed Expand file tree Collapse file tree 3 files changed +16
-17
lines changed Original file line number Diff line number Diff line change 5
5
* By Faisal Saadatmand
6
6
*/
7
7
8
- #define NULL 0
8
+ #include <stddef.h> /* for NULL */
9
+
9
10
#define NALLOC 1024 /* minimum #units to request */
10
11
11
12
typedef long Align ; /* for alignment to long boundary */
Original file line number Diff line number Diff line change 6
6
* By Faisal Saadatmand
7
7
*/
8
8
9
- #define NULL 0
10
- #define NALLOC 1024 /* minimum #units to request */
11
- #define MAXBYTES 1048576 /* 1 Megabytes */
9
+ #include <stddef.h> /* for NULL */
10
+ #include <limits.h> /* for INT_MAX */
11
+
12
+ #define NALLOC 1024 /* minimum #units to request */
12
13
13
14
typedef long Align ; /* for alignment to long boundary */
14
15
@@ -36,7 +37,7 @@ void *knr_malloc(unsigned nbytes)
36
37
Header * prevp ; /* pointer to previous block */
37
38
unsigned nunits ;
38
39
39
- if (nbytes > MAXBYTES ) /* error check */
40
+ if (nbytes == 0 || nbytes > INT_MAX ) /* error check */
40
41
return NULL ;
41
42
42
43
/* round up to allocate in units of sizeof(Header) */
@@ -132,19 +133,15 @@ void knr_free(void *ap)
132
133
133
134
#include <stdio.h>
134
135
135
- #define SIZE 21 /* chenge to test error checking */
136
+ #define SIZE 21 /* chenge value to test error checking */
136
137
137
138
int main (void )
138
139
{
139
- int i ;
140
140
char * s ;
141
141
142
- if ((s = (char * ) knr_malloc (SIZE * sizeof (char ))) != NULL ) {
143
- for (i = 0 ; i < SIZE - 1 ; i ++ )
144
- s [i ] = i + '0' ;
145
- s [i ] = '\0' ;
146
- printf ("%s\n" , s );
147
- } else
142
+ if ((s = (char * ) knr_malloc (SIZE * sizeof (char ))) != NULL )
143
+ printf ("Valid size\n" );
144
+ else
148
145
fprintf (stderr , "Invalid size\n" );
149
146
150
147
knr_free (s );
Original file line number Diff line number Diff line change 6
6
* By Faisal Saadatmand
7
7
*/
8
8
9
- #define NULL 0
10
- #define NALLOC 1024 /* minimum #units to request */
11
- #define MAXBYTES 1048576 /* 1 Megabytes */
9
+ #include <stddef.h> /* for NULL */
10
+ #include <limits.h> /* for INT_MAX */
11
+
12
+ #define NALLOC 1024 /* minimum #units to request */
12
13
13
14
typedef long Align ; /* for alignment to long boundary */
14
15
@@ -36,7 +37,7 @@ void *knr_malloc(unsigned nbytes)
36
37
Header * prevp ; /* pointer to previous block */
37
38
unsigned nunits ;
38
39
39
- if (nbytes > MAXBYTES ) /* error check */
40
+ if (nbytes == 0 || nbytes > INT_MAX ) /* error check */
40
41
return NULL ;
41
42
42
43
/* round up to allocate in units of sizeof(Header) */
You can’t perform that action at this time.
0 commit comments