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

It might be easier if you don't use that fixed-length buffer at all. Just put the characters into temp, when temp gets filled up call realloc() to make it bigger. You need two int variables, one to keep track of the current position to add a new character and the other to keep track of the current size of temp. You don't want to realloc temp for each byte, but expand it for BUFF_LENGTH bytes. Like this

char* temp = NULL;
int curSize = 0; // current size of temp
int curPos = 0; // next character read goes here
int c;

while( (c = getchar()) != '\n') && (c != EOF))
{
    if( curPos == curSize) // time to realloc
    {
        curSize += BUFF_LENGTH;
        temp = realloc(temp,curSize);
    }
    temp[curPos++] = c;
}

But Sir,when the realloc fails it returns the NULL, making my all previous content lost.
I tried using using different pointer to store temp , but dint help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.