0

I am trying to print the values that are in an array. Inside ascii(), I printed the values to check if the values are getting transferred to the main function without any problems. I also made random_values so that all integers inside the array are between 33 and 126.

Everything looked fine, but the problem is that when I comment that part code that I wrote to check inside ascii(), the values in the main function gets messed up. It gives me values like 384, 386, 387.

I think this is some kind of memory problem but I don't know much about memory and pointers.

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

int length;

void random_values (int a[], int l) {

    for (int i = 0; i < l; i++) {
        a[i] = (rand() % 94 + 33);
    }

}

void set_length () {
    //generates random integer between 8 and 15 which is used as the length of the array[]
    length = (rand() % 8 + 8);
}

int ascii (int **pass)
{

    int array[length];

    //for assigning random values to the array []
    random_values(array, length);
    

    //the printed output from the main fuction is different if I comment this part - random_values() don't work
    for (int i = 0; i < length; i++) 
    {
        printf("%d ", array[i]);
    }

    *pass = array;
    return 0;
}


int main () {

    //to prevent rand() from producing the same value every time
    srand(time(NULL));

    set_length();

    int *password = malloc(sizeof(int) * length);

    ascii(&password);


    for (int i = 0; i < length; i++) {
        printf("%d ", password[i]);
    }


    //just to check
    printf("\nlength is %d", length);
    printf("\n");

}
lemnel
  • 19
  • 4
  • 1
    You have **undefined behaviour** with or without your 'internal printing'. The `ascii` function is returning a pointer to a local (temporary) variable. It just works 'by accident' sometimes. – Adrian Mole Sep 20 '20 at 12:36
  • 1
    Undefined behavior for using the value of a pointer to an object after the end of that objects lifetime. – EOF Sep 20 '20 at 12:36
  • Your random_values function does the thing that you want, no need for the ascii function. Technically, EOF and Adrian are right, returning the adress of the local variable is risky. – tango-1 Sep 20 '20 at 12:56
  • @tango is not risky. You can't do it. – 0___________ Sep 20 '20 at 17:13
  • @P__J__ I got just warning, compilation is done with g++. – tango-1 Sep 20 '20 at 17:15
  • @tango "just warning". Yep what is the warning, we can ignore them. To ignore warnings or to do not? That is the question— Whether ’tis nobler in the mind to suffer The slings and arrows of outrageous fortune. – 0___________ Sep 20 '20 at 17:21
  • @tango for your level of C knowledge - treat all warnings as errors. – 0___________ Sep 20 '20 at 17:23
  • @tango-1 Just because it compiles doesn't change the fact that it's 100% broken. Code can be syntactically valid but logically buggy or completely meaningless. – Blastfurnace Sep 20 '20 at 17:23
  • @P__J__ I dont say that ignore the warnings, compilation can be done with warnings :) Calm down guys, okey you are the expert.. for your english knowledge :)) – tango-1 Sep 20 '20 at 17:28
  • @P__J__ I can compile the code, you say that you cannot. I can , but I dont. :)) – tango-1 Sep 20 '20 at 17:30
  • @tango please.... code will compile because its syntax is OK. But the code has very important bug and it should not be run. If the code invokes the UB its behaviour is unpredictable. So **never ever** return the reference to the local variable – 0___________ Sep 20 '20 at 17:35
  • @P__J__ BRO, I said that risky too. What is your purpose? I know it should not be run. – tango-1 Sep 20 '20 at 17:36
  • @P__J__ compiler looks what? The risky bugs.. ı dont think so. I said that IT CAN BE COMPILE – tango-1 Sep 20 '20 at 17:37

1 Answers1

2

You are sending the pointer of a local variable. I would change ascii function to:

int ascii (int *pass)
{
     //for assigning random values to the array []
     random_values(pass, length);

    //the printed output from the main fuction is different if I comment this part - random_values() don't work
    for (int i = 0; i < length; i++)
    {
        printf("%d ", pass[i]);
    }

    return 0;
}
javierj
  • 85
  • 1
  • 3