1

I am trying to set up and use an array in c++. The array has been declared and been used, but I keep getting back the error "variable set but not used", even though I have used it in the program.

It works on Linux but not Windows 10.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int sampling_rate = 44100;
    double dt = 1.0 / sampling_rate;
    int duration = 1;
    int number_of_samples = duration / dt;
    int sample_array[number_of_samples];
    int amplitude = 5000;

    cout << "sampeling rate = " << sampling_rate<<endl;

    for (int sample_number = 0; sample_number < sampling_rate; sample_number++)
    {
      double sample_time = sample_number * dt;
      sample_array[sample_number] = amplitude * sin(2 * M_PI * 293 * sample_time);
    }
}

I expect this code to make an array that can then be converted to a sine wave.

Desert
  • 11
  • 2
  • [MSVC](https://godbolt.org/z/_IcTrJ) just complains about *VLA* (read the previous comment). Nothing about "*variable not used*". I am assuming you are trying to compile with MSVC for windows. – BiagioF Apr 08 '19 at 09:22
  • The warning complains that the array is write-only, you have computed some values but are not reading them or using them in any way. Which indeed you are not. If you plan to add that later, you can ignore the warning for now. –  Apr 08 '19 at 09:26
  • Possible duplicate of [Variable Length Array (VLA) in C++ compilers](https://stackoverflow.com/questions/39334435/variable-length-array-vla-in-c-compilers) – Matthieu Brucher Apr 08 '19 at 09:28
  • 1
    @MatthieuBrucher No, that’s completely unrelated. – Konrad Rudolph Apr 08 '19 at 09:29

2 Answers2

1

Your compiler’s warning points out (correctly) that you only ever write to your array, you never read the results.

Since you’re not reading the results, why compute them in the first place? Your compiler is entirely correct in noting that this is useless work, and assumes that you probably forgot to actually, you know, use the computed values.

As this is only a warning and not an error you can ignore it for now if this code is work in progress. In “finished” code you should however be very alarmed by such a warning since it almost certainly points to a bug in your code.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
1

@KonradRudolph's answer discusses where the warning is coming from. You're code is missing the second half that actually does something with your array. Besides that, there are some serious flaws in the code (which is why it won't work on Windows):

what you're trying to do is create variable length array and they are not part of the standard. number_of_samples is evaluated at runtime and a [] array needs to have compile time known size.

For dynamic arrays you would use a std::vector but you don't actually need one since all your initial variables behave like constants. You just need to could declare them constexpr so that they are evaluated at compile time:

constexpr int sampling_rate = 44100;
constexpr double dt = 1.0 / sampling_rate;
constexpr int duration = 1;
constexpr int number_of_samples = duration / dt;
int sample_array[number_of_samples];

You're also trying to use M_PI from <math.h> but you forgot the #define _USE_MATH_DEFINES:

#define _USE_MATH_DEFINES
#include <math.h>

(you also shouldn't use namespace std; you can read here why)

Stack Danny
  • 7,754
  • 2
  • 26
  • 55