Home > database >  Is the problem in the below code related to memory allocation for array of pointers?
Is the problem in the below code related to memory allocation for array of pointers?

Time:12-25

In the below code, I am trying to read an input line from STDIN using function usergetline and in the main function, I am assigning the input string to an array of char pointers. (char *lineptr[MAXCOUNTLINE])

While within the 1st while loop, the input line is stored in the lineptr (as can be seen when I print the lineptr[iplinecount]), however, once I come outside the loop, all it prints is new line.

#include <stdio.h>
#include <string.h>

/* function declarations */
int usergetline(char *, int);

/* constants */
#define MAXCOUNTLINE 10
#define MAXLINECOUNTWIDTH 100
#define DEFPRINTFRLASTCOUNT 8

void main(int argc, char *argv[])
{
    char *ipline;
    int iplinecount,shifter;
    iplinecount=0;
    char *lineptr[MAXCOUNTLINE];
    /* continue to accept the lines till the time EOF is not encountered and 
     * max count of lines is not exceeded */
    while((iplinecount < MAXCOUNTLINE) && (usergetline(ipline,MAXLINECOUNTWIDTH) >0)){
        lineptr[iplinecount] = ipline;
        #ifdef DEBUG
        printf("iplinecount: %d\n",iplinecount);
        printf("ipline: %s\n",ipline);
        printf("strlen of ipline: %d\n",strlen(ipline));
        printf("*(lineptr iplinecount): %s\n",lineptr[iplinecount]);
        printf("strlen of *(lineptr iplinecount): %d\n",strlen(lineptr[iplinecount]));
        printf("value at ipline %p\n",ipline);
        printf("value at *(lineptr iplinecount) %p\n",lineptr[iplinecount]);
        #endif
        iplinecount  ;
    }
    printf("iplinecount = %d\n",iplinecount);
    shifter=0;   
    while(shifter < iplinecount){
        printf("strlen: %d\n",strlen(*(lineptr shifter)));
        printf("%s\n",*(lineptr shifter));
        shifter  ;
    }
}

/* usergetline: function to accpet an input line from STDIN */
int usergetline(char *ipline, int maxlengthofip)
{
    char c;
    int i=0;
    while((maxlengthofip-1) && ((c=getchar()) != EOF) && (c != '\n')){
        maxlengthofip--;
        *ipline=c;
        ipline  ;
        i  ;
    }
    if (c=='\n'){
        *ipline='\n';
        ipline  ;
        i  ;
    }
    *ipline = '\0';
    return i;
}

Suppose this file name is exercise5-13.c. I am using cygwin environment. After compilation (with -D DEBUG flag), when I execute the program, as in. ./exercise5-13.exe Output is:

testing
iplinecount: 0
ipline: testing

strlen of ipline: 8
lineptr[iplinecount]: testing

strlen of lineptr[iplinecount]: 8
value at ipline 0xffffcdf0
value at lineptr[iplinecount] 0xffffcdf0
iplinecount = 1
strlen=0

My limited understanding and argument against initialization of array of char pointer is that when I am assigning (line 21)

''' lineptr[iplinecount] = ipline; '''

then, lineptr is being assigned an address of ipline which in itself is pointing to the string constant fetched from the STDIN testing in this case. I was expecting that eachnew string fetched from STDIN would be first stored in line pointer and from their, the address of the first element of the string would be stored in the char pointer array. However, char pointer array is empty?

CodePudding user response:

You are using the uninitialized pointer

char *ipline;

//...

usergetline(ipline,MAXLINECOUNTWIDTH)

within the function usergetline that invokes undefined behavior.

You need to allocate dynamically memory where you are going to read data and the address of the allocated memory to assign to the pointer ipline. So the pointer ipline must be passed to the function by reference that is through a pointer to the pointer.

The function declaration in this case will look for example like

int usergetline(char **ipline, int maxlengthofip);
  • Related