2

If I run this code for the first time it gives me the desired output. But on the second run, I don't get the desired output. It seems like memory is not given free.

#include <stdio.h>
#include <unistd.h>
#include <crypt.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>


char intToChar(int prefix);
void addNewAlpha(char *anyAlpha);
void addNewAlpha1(char *anyAlpha1);
void addNewAlpha2(char *anyAlpha2);



int main(void) {

    // place for one character is reserved because one character is printed 
    //in that loop
    char *firstAlpha = (char *) malloc(sizeof(char));

    for (int i = 0; i < 4; i++) {

        *firstAlpha = intToChar(i);
        printf("%s\n",firstAlpha);
        addNewAlpha(firstAlpha);
    }
    free(firstAlpha);

    return 0;
}

 // add new character to existing characters
void addNewAlpha(char *anyAlpha) {

    // place for 2 characters is reserved because two characters is printed 
    //in that loop
    char *strPtr =  malloc(2*sizeof(char));
    strcpy(strPtr,anyAlpha);

    for ( int i = 0; i < 4; i++) {

        strPtr[1] =  intToChar(i);
        printf("%s\n",strPtr);
        addNewAlpha1(strPtr);

    }
    free(strPtr);

}


void addNewAlpha1(char *anyAlpha1) {

    // place for 3 characters is reserved because 3 characters is printed 
    //in that loop
    char *strPtr1 = (char *) malloc(3*sizeof(char));
    strcpy(strPtr1, anyAlpha1);

    for ( int i = 0; i < 4; i++) {

        strPtr1[2] =  intToChar(i);
        printf("%s\n",strPtr1);

        addNewAlpha2(strPtr1);
    }
    free(strPtr1);
}


void addNewAlpha2(char *anyAlpha2) {

    // place for 4 characters is reserved because 4 characters is printed 
    //in that loop
    char *strPtr2 = (char *) malloc(4*sizeof(char));
    strcpy(strPtr2, anyAlpha2);

    for ( int i = 0; i < 4; i++) {

        strPtr2[3] =  intToChar(i);
        printf("%s\n",strPtr2);

    }
    free(strPtr2);
}

//used for int to char conversation for a-z and A-Z
char intToChar(int prefix) {
    if(prefix < 26)
        return (char)(prefix + 65);
    else
        return (char)(prefix + 71);
}

The desired output will be :

A
AA
AAA
AAAA
AAAB
AAAC
AAAD
AAB
AABA
and so on....

But sometimes it gets really weird ...

C
CA+
CAA
CAAA
CAAB
CAAC
CAAD
CAB
CABA
CABB

It should be "CA" not "CA+". What I think is that although I freed the memory is not really freed. Do you have any idea? Thanks!!!

1
char *firstAlpha = (char *) malloc(sizeof(char));

    for (int i = 0; i < 4; i++) {

        *firstAlpha = intToChar(i);
        printf("%s\n",firstAlpha);
        addNewAlpha(firstAlpha);

I would say that you have a problem with the amount of space reserved, which seems to be insufficient, if you look fine you only have room for a char before the first loop, but if we reserve space for five char in each loop

char *firstAlpha = (char *) malloc(5*sizeof(char));

the output is as expected

2
  • Hi, I appreciate your prompt response. But I didn't get it completely. In the first loop, I cover one character. In next function, I send the pointer to that character and I allocate memory for two characters and so on. In the first loop, I am dealing with just one character. Why do I need space for 4 characters? I will be thankful for any hints.
    – user10784
    Aug 17 '17 at 18:59
  • printf("%s\n",firstAlpha); Is a string two characters, not enough space
    – MARS
    Aug 17 '17 at 20:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .