#define ALLOC_DATA(pointer, datatype, elements) \
do { \
if (!((pointer) = (datatype *) calloc ((elements), sizeof(datatype)))) \
{ \
logfmt("Unable to calloc new data from file: %s function: %s, line: %d\t *** ABORTING ***", __FILE__, __FUNCTION__, __LINE__); \
abort(); \
} \
} \
while(0)
#define PURGE_DATA(data) \
do { \
if(data) \
{ \
free((void *)data); \
} \
else { \
if(logFail) { \
log_string((char *)Format("clear_free_lists: unable to purge_data as it was NULL from: %s %s %d", __FILE__, __FUNCTION__, __LINE__)); \
} \
} \
data = NULL; \
} while(0)
memset(pReset, 0, sizeof(RESET_DATA ));
= (datatype *) calloc(…)
memset(pReset, 0, sizeof(RESET_DATA ));
#include <stdlib.h>
struct test {
int a;
struct {
int c;
char *d;
unsigned char x;
} b;
};
static const struct test zero_test;
int main(void) {
/* clearing automatic objects is the easiest */
struct test w = {0};
/* allocated space is a little bit trickier */
struct test *t = malloc(sizeof *t);
*t = zero_test; /* C89 */
*t = (struct test) { 0 }; /* C99 */
return t->b.x + w.b.x;
}
sizeof(RESET_DATA)
= (datatype *) calloc(…)
memset(pReset, 0, sizeof(RESET_DATA ));
#include <stdlib.h>
struct test {
int a;
struct {
int c;
char *d;
unsigned char x;
} b;
};
static const struct test zero_test;
int main(void) {
/* clearing automatic objects is the easiest */
struct test w = {0};
/* allocated space is a little bit trickier */
struct test *t = malloc(sizeof *t);
*t = zero_test; /* C89 */
*t = (struct test) { 0 }; /* C99 */
return t->b.x + w.b.x;
}
sizeof(RESET_DATA)
jlambert@ATLAS ~
$ cat tm.c
#include <stdlib.h>
#include <stdio.h>
void dumpmem(char *p, char *msg) {
char i;
printf("%s\n",msg);
for (i=0;i<100;i++) {
if (!(i % 16)) printf("\n");
printf("%02hhx ",*(p+i));
}
printf("\n");
}
int main() {
char *p;
char i;
p = malloc(100*sizeof(char));
dumpmem(p, "Memory after 1st malloc:");
for (i=0;i<100;i++) *(p+i) = i;
dumpmem(p, "Memory after initializing:");
printf("Free the memory\n");
free(p);
p = malloc(100*sizeof(char));
dumpmem(p, "Memory after 2nd malloc:");
return 0;
}
jlambert@ATLAS ~
$ gcc tm.c
jlambert@ATLAS ~
$ ./a
Memory after 1st malloc:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
Memory after initializing:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
Free the memory
Memory after 2nd malloc:
4c d6 23 61 4c d6 23 61 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
68 00 00 00
#include <stdlib.h>
#include <stdio.h>
void dumpmem(char *p, char *msg) {
char i;
printf("%s\n",msg);
for (i=0;i<100;i++) {
if (!(i % 16)) printf("\n");
printf("%02hhx ",*(p+i));
}
printf("\n");
}
int main() {
char *p, *q;
char i;
p = malloc(100*sizeof(char));
dumpmem(p, "Memory after 1st malloc:");
for (i=0;i<100;i++) *(p+i) = i;
dumpmem(p, "Memory after initializing:");
printf("Free the memory\n");
free(p);
dumpmem(p, "Dump memory after free:");
for (i=0;i<100;i++) *(p+i) = i;
dumpmem(p, "Reinitialize freed memory:");
printf("Core dumps here on 2nd malloc because\nwe've corrupted the magic bytes in our memory manager\n");
q = malloc(100*sizeof(char));
dumpmem(q, "Memory after 2nd malloc:");
return 0;
}
Memory after 1st malloc:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
Memory after initializing:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
Free the memory
Dump memory after free:
4c d6 23 61 4c d6 23 61 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
68 00 00 00
Reinitialize freed memory:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
Core dumps here on 2nd malloc because
we've corrupted the magic bytes in our memory manager
Aborted (core dumped)
Memory after 1st malloc:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00
Memory after initializing:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
Free the memory
Dump memory after free:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
Reinitialize freed memory:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
Core dumps here on 2nd malloc because
we've corrupted the magic bytes in our memory manager
Memory after 2nd malloc:
00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
60 61 62 63
I have a question about something and I don't understand. I decided to use one of the static code analyzers, and me being a inexperienced programmer, don't understand one of the results. It told me Size of pointer 'pReset' used instead of size of its data. What does that mean and how can I fix it? If someone can give me a plain English explanation, I'd greatly appreciate it. I've got several instances of those I'd like to fix.