below is a short c program that does some pointer minipulations
with strings. the output produced by the program follows directly
afterwards. right after that in the second post is about the same
program in powerbasic. as time permits i'll post several more sets
of comparison programs. a comment thread is in programming at...
http://www.powerbasic.com/support/pb...ad.php?t=22503
------------------
fred
"fharris"+chr$(64)+"evenlink"+chr$(46)+"com"
[this message has been edited by fred harris (edited august 03, 2007).]
with strings. the output produced by the program follows directly
afterwards. right after that in the second post is about the same
program in powerbasic. as time permits i'll post several more sets
of comparison programs. a comment thread is in programming at...
http://www.powerbasic.com/support/pb...ad.php?t=22503
Code:
#include "windows.h" #include <stdio.h> #define last_index 3 // int main(void) { char *szdata[]={"zero","one","two","three"}; // initial test data is in szdata[] bool blnfree=(bool)null; //array, but for purposes of exposition char **pmem=null; //this data will be copied to dynamically allocated unsigned int i,j; //storage accessed by char pointer to pointer pmem. handle hheap; // hheap=getprocessheap(); pmem=(char**)heapalloc(hheap,heap_zero_memory,sizeof(char*)*(last_index+1)); if(pmem) { printf("allocate 16 bytes to hold four char pointers.\n"); printf("there will be one allocation to hold the four\n"); printf("pointers, i.e., pmem, and then four more allocations\n"); printf("for memory into which to copy the strings themselves.\n\n"); printf("pmem=%u\n\n",pmem); puts(" uint (%u) str (%s)"); puts("i &pmem[i] pmem[i] pmem[i] "); puts("=================================================="); for(i=0;i<=last_index;i++) { pmem[i]=(char*)heapalloc(hheap,heap_zero_memory,strlen(szdata[i])+1); if(pmem[i]) { strcpy(pmem[i],szdata[i]); //copy *szdata[] strings to storage printf("%u\t%u\t\t%u\t\t%s\n",i,&pmem[i],pmem[i],pmem[i]); } } printf("\n"); for(i=0;i<=last_index;i++) { if(pmem[i]) { for(j=0;j<strlen(pmem[i]);j++) //try some fancy byte printf("%c\t",pmem[i][j]); // minipulations! printf("\n"); } } printf("\n"); for(i=0;i<=last_index;i++) { if(pmem[i]) { blnfree=!!heapfree(getprocessheap(),0,pmem[i]); printf("blnfree=%u\n",blnfree); } } printf("\n"); blnfree=!!heapfree(getprocessheap(),0,pmem); printf("blnfree=%u\n",blnfree); } else puts("memory allocation failure!"); getchar(); // return 0; }
Code:
/* 'output: ' 'allocate 16 bytes to hold four char pointers. 'there will be one allocation to hold the four 'pointers, i.e., pmem, and then four more allocations 'for memory into which to copy the strings themselves. ' 'pmem=2307184 ' ' uint (%u) str (%s) 'i &pmem[i] pmem[i] pmem[i] '================================================== '0 2307184 2303368 zero '1 2307188 2307208 one '2 2307192 2307224 two '3 2307196 2307240 three ' 'z e r o 'o n e 't w o 't h r e e ' 'blnfree=1 'blnfree=1 'blnfree=1 'blnfree=1 ' 'blnfree=1 */
fred
"fharris"+chr$(64)+"evenlink"+chr$(46)+"com"
[this message has been edited by fred harris (edited august 03, 2007).]
Comment