Getting a stack smashing detected when user puts in higher numbers

Hey guys. just a disclaimer this is a homework question. The program takes user input and the user sets an array size as well as the numbers in the array. I think i have everything else working but it seems that if the user decides to input around 8 or higher as an array size I get the smashing error. It's supposed to take each array[value] and % 3 to determine if it's divisible by 3. It always works with the first few numbers but then stops for some reason with the error.

EDIT:
Thanks! I moved my initialization statement and it fixed that. The only thing left to figure it out is why I will get a zero occasionally as a listed number. My equation is if index[x] % 3 == 0 print index[x].. Is there something obvious I'm missing here? I'll keep plugging away at it.

EDIT:
Hey guys I got it and I figured I'd note it for future problem havers.
In the display multiples function I have it set to run until newArraySize hits its max and if you set it instead to newArraySize - 1 you wont get the extra number thrown in
Any tips would be appreciated.


https://onlinegdb.com/HkY-FvyTz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include <iostream>
using namespace std;

void getSize(int&);
//void print(int);
void getList(int, int[]);
void displayMultiples(int, int[]);
 
int getSize();
int getList();
int displayMultiples();
int main()
{
   int arraySize;
   int newArray[arraySize];
   
   getSize(arraySize);
   
   getList(arraySize, newArray);//added
   
   displayMultiples(arraySize, newArray);//added
   
 //  print(arraySize);
   
   
   
   return 0;
}


void getSize(int& newArraySize)
{
   int arraySize;
   cout << "Enter the size of the list: " ; 
   cin >> newArraySize;
   cout << endl;
   //int sizeArray[arraySize - 1 ];
}



void getList(int newArraySize, int ultraNewArray[])//added array in arguments
{
   int newArray[newArraySize];//makin this -1 does not fix it    
   
   for (int x = 0 ; x <= newArraySize-1; x++)
   {
      int placeholder;
      cout << "Enter number for index "<< x << ":" << endl;
      cin >> placeholder; cout << endl;
      ultraNewArray[x] = placeholder;
      cout << endl;
      
    
      
   }    
      
      
}


void displayMultiples(int newArraySize, int ultraNewArray[])
{
   cout << "The following are divisible by 3:\n";
   for (int y = 0 ; y <= newArraySize ; y++)//adding - 1 lets the program run all the way but still causes stack overflow
   {
       if (ultraNewArray[y] % 3 == 0)
       {
           cout << ultraNewArray[y] << endl;
       }
      
   } 
}








Last edited on
1
2
int arraySize;
int newArray[arraySize];

arraySize uninitalized and might contain sth. like -12544343.
arraySize must be a constant value in standard C++.

If you are allowed to use VLAs then move line 18 between line 15 and 16
There might be more errors but fix this one first and post again.

1
2
3
4
5
6
int main()
{
   int arraySize;
   int newArray[arraySize];
   //...
}

After this, newArray is probably an array of size 0. Later on you're checking indices of stuff that's not owned by anyone, and could output anything.

for the record, in C++ whenever you want to have dynamic array sizes, use vectors.

1
2
3
4
5
6
7
const int arraySize = 15;
int* array = new int[arraySize];   // formed from a constant, fine.

int k;
cin >> k;
int* array2 = new int[k];  // dynamic size (not const), illegal.
vector<int> array3(k);    // vector with dynamic, fine. 


Topic archived. No new replies allowed.