int main() { char buf_s[] = "This is data from static storage.", *buf_d; printf("%s\n", buf_s); buf_d = (char *) mallocf(CBUFSIZE); strcpy(buf_d, "This is data from dynamic storage."); printf("%s\n", buf_d); return 0; }
// test overwritten data with bfreef() int main() { static char sarr[SARRSIZ]; char *sarr_ptr = __ptralign(sarr,HSIZE) + HSIZE; // new pointer char *str = mallocf(STRSIZ); int i; strncpy(str, "This is a test.", STRSIZ); str[STRSIZ-1] = '\0'; strncpy(sarr, "This is data from a static array.", SARRSIZ); sarr[SARRSIZ-1] = '\0'; printf("str = %s\n", str); printf("sarr position = %p (%s)\n", sarr, (unsigned long)sarr%HSIZE ? "unaligned" : "aligned"); printf("sarr = %s\n", sarr); bfreef(sarr, SARRSIZ); freef(str); printf("sarr = %s\n", sarr); printf("sarr_ptr position = %p\n", sarr_ptr); printf("sarr =\n"); for (i=0; i<SARRSIZ; ++i) printf("0x%02x%c", (unsigned char) sarr[i], (i+1)*5%80 ?' ' : '\n'); while (str != sarr_ptr) // assumption is that sarr size % 16 == 0 str = mallocf(SARRSIZ-HSIZE); // egregious memory leak printf("found str = sarr_ptr\n"); strncpy(str, "This is data that overwrites static data.", SARRSIZ-HSIZE); str[SARRSIZ-HSIZE-1] = '\0'; printf("str = %s\n", str); printf("sarr =\n"); for (i=0; i<SARRSIZ; ++i) printf("0x%02x%c", (unsigned char) sarr[i], (i+1)*5%80 ?' ' : '\n'); freef(str); return 0; }
float *zeropaddingex(const float *ptr, size_t length, size_t *newLength, size_t additionalLength) { size_t nl = length; int log = 2; while (nl >>= 1) { log++; } nl = (1 << log); *newLength = nl; float *ret = mallocf(nl + additionalLength); memcpy(ret, ptr, length * sizeof(float)); memsetf(ret + length, 0.f, nl - length); return ret; }