0

I was writing a program in c for vignere cipher, when in a function to generate the key as the same length of input name,i encountered a bug where if i remove the "printf" line displaying the length of input string it, prints weird stuff on the sceen, it happens only when i delete that "printf" line from the GenKey() function.

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

char *GenKey(char *key, char *source){
    int i=0,j=0;
    char ReturnKey[strlen(source)];

    printf("%d\n",strlen(source));      // THIS LINE HERE CAUSES PROBLEM

    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
        }
        return ReturnKey;
}

int main()
{
    int i;
    char name[10000];
    char container[10000];
    char VigKey[]="INFERNO";
    char *NamePtr;
    char *KeyPtr;
    printf("give a name: ");
    fgets(name,10000,stdin);

    char GeneratedKey[strlen(name)];
    KeyPtr=VigKey;
    NamePtr=name;
    strcpy(GeneratedKey,GenKey(KeyPtr,NamePtr));
    printf("%s",GeneratedKey);
}

Output(Before deleting that line):

give a name: ATTACKATDAWN
13
INFERNOINFER

Now i delete that line

char *GenKey(char *key, char *source){
    int i=0,j=0;
    char ReturnKey[strlen(source)];

   // NOW I HAVE DELETED THAT LINE

    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
        }
        return ReturnKey;
}

Output(After deleting that line):

give a name: ATTACKATDAWN
INFERNOINFERα╫`

1 Answers1

1

Try creating the ReturnKey character array on the heap with malloc like this:

char *GenKey(char *key, char *source){
    int i=0,j=0;
    char *ReturnKey = malloc(sizeof(char) * strlen(source));
    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
    }
    return ReturnKey;
}

Before when you were creating ReturnKey, you were doing so as a local variable which only lives in the context of that function. Even though you would see your word still there, that was just because it was still in that position in memory but was no longer being referencing by an object.

When you create a dynamic array with a function like malloc, you are creating it on the so called "heap" which doesn't get released when it goes out of scope, this way you can return it from the function (you actually return a pointer to that location in memory).

When using malloc note that the memory IS NOT RELEASED, and thus you must at some point later release it yourself by calling free otherwise you will leak memory.

This is what the full code might look like:

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

char *GenKey(char *key, char *source){
    int i=0, j=0;

    // malloc takes the size of the data type * the length
    char *ReturnKey = malloc(sizeof(char) * strlen(source));

    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
    }
    return ReturnKey;
}

int main()
{
    int i;
    char name[10000];
    char container[10000];
    char VigKey[]="INFERNO";
    char *NamePtr;
    char *KeyPtr;
    printf("give a name: ");
    fgets(name,10000,stdin);

    KeyPtr=VigKey;
    NamePtr=name;
    char *GeneratedKey = GenKey(KeyPtr,NamePtr);
    printf("%s",GeneratedKey);
    free(GeneratedKey);  // IMPORTANT!!!
}

Here is a more in depth article on using malloc and free:

https://www.codingunit.com/c-tutorial-the-functions-malloc-and-free

Asleepace
  • 3,466
  • 2
  • 23
  • 36