Stack around the variable 's' was corrupted.

Write your question here.

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
  #include "Mean.h"
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
template<class T>
class CMean
{
private:
	size_t t;
	T* array[];
public:
	CMean<T>(const T v[], size_t num){
		t = num;
		for (size_t i{}; i < t; i++)
			array[i] = new T(v[i]);
	}
	~CMean(){
		size_t len = t;
		for (size_t i{}; i < len; i++)
			delete array[i];
	}
	T mo(){
		T temp{};
		for (size_t i{}; i < t; i++){
			temp += *array[i];
		}
		temp /= t;
		return temp;
	}
};
int main()
{
	double a[] {1.1, 2.2, 3.3};
	CMean<double> s(a, _countof(a));
	cout << s.mo() << endl;
	return 0;
}

After return 0; i have the error : Stack around the variable 's' was corrupted.
In CMean the array array has no size (which is illegal in standard C++) and therefore has no elements, but you act as if it does.

Since you're using _countof I assume you're using VS. In which case the following warnings should be generated when you compile:

1>c:\...\main.cpp(10): warning C4200: nonstandard extension used: zero-sized array in struct/union
1>  note: This member will be ignored by a defaulted constructor or copy/move assignment operator
1>  note: see reference to class template instantiation 'CMean<double>' being compiled
1>c:\...\main.cpp(34): warning C4815: 's': zero-sized array in stack object will have no
1>  elements (unless the object is an aggregate that has been aggregate initialized)


So it tells you exactly what the problem is. Don't ignore your compiler warnings.

[edit: reformat output]
Last edited on
Topic archived. No new replies allowed.