Hi ,
I am trying for a program which takes any no of characters as input and stores, so obviously i would need DMA.
My approch is to
Take a buffer of some fixed size(5 in this case)
Fill the buffer till the fixedsize-1
Allocate memory dynamically and store the data
But there is a problem with the realloc functions.I am making two pointres point to same location, when i reallocate memory next time using one pointer other one also getting corrupted( i mean changed).
I appreciate any help in solving the problem.
int main( void )
{
printf(" %s \n", glinfi());
getchar();
}
char *glinfi(void)
{
char *p = NULL,*tmp = NULL;
char ch;
char buff[BUFF_LEN] = {0};;
int nc ,nl,m5;
nc = nl = m5 =0;
for(;(((ch=getchar())!='\n')&& (ch != EOF));) {
buff[nc] = ch;
if(nc == BUFF_LEN - 2) {
m5++;
if (!(tmp = (char *)realloc(tmp,m5*BUFF_LEN))){
printf("** Outta Memory **\n\n");
return p;
}
buff[nc+1] = '\0';
memset(tmp,0,BUFF_LEN);
if (m5 == 1) {
p = tmp;
strcpy(p,buff);
}
if(m5 > 1) {
strcpy(tmp,p);
p = tmp;
strcat(p,buff);
}
nc =0;
}else {
nc++;
}
}
if(ch == '\n') {
if (!(tmp = (char *)realloc(tmp,m5*BUFF_LEN+3))){
printf("** Outta Memory **\n\n");
return p;
}
memset(tmp,0,BUFF_LEN);
strcpy(tmp,p);
p =tmp;
buff[nc+1] =0;
strcat(p,buff);
}
return p;
}
BTW , i have seen this one below,it is doing the same thing with files, but whats the case when i give more then 16 chars as input.
http://www.daniweb.com/software-development/c/threads/251685/infinite-character-input