.exe file has stopped working windows trying to find solution

i am writing a program where i have to perform different operation using float arrays. here is my complete program:
header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//floatArray.h
#ifndef FLOAT ARRAY_H
#define FLOAT ARRAY_H
class floatArray
{
public:
	floatArray();
	void print();
	floatArray(int size);
	floatArray(const floatArray& rhs);
	~floatArray();
	floatArray& operator=(const floatArray& rhs);
	void resize(int newSize);
	int size();
	float& operator [](int i);
private:
	float*mData;
	int mSize;
};
#endif

implementation file:
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
//floatArray.cpp
#include"floatArray.h"
#include<iostream>

using namespace std;
floatArray::floatArray()
{
	*mData=0.0f;
}
floatArray::floatArray(int size)
{
	size=mSize;
}
floatArray::floatArray(const floatArray& rhs)
{
	int len=rhs.mSize;
	mData=new float[len];
	for(int i=0;i<len;i++)
		rhs.mData[i]=mData[i];
}
floatArray::~floatArray()
{
	delete[] mData;
	mData=0;
}
void floatArray::resize(int newSize)
{
	mSize=newSize;
}
float& floatArray::operator[](int i)
{
	return mData[i];
}
int floatArray::size()
{
	return mSize;
}
floatArray& floatArray::operator=(const floatArray& rhs)
{
	if(this==&rhs)
		return *this;
	int len=rhs.mSize;
	delete[] mData;
	mData=new float[len];
	for(int i=0;i<len;i++)
		mData[i]=rhs.mData[i];
	return*this;
}
and finally main.cpp :
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
//main.cpp
#include<iostream>
#include"floatArray.h"
using namespace std;
void printFloatArray(floatArray& fa)
{
	cout<<"{";
	for(int i=0;i<fa.size();i++)
		cout<<fa[i]<<" ";
	cout<<"}"<<endl;
}

int main()
{
	floatArray a;
	a.resize(4);
	a[0]=1.0f;
	a[1]=2.0f;
	a[2]=3.0f;
	a[3]=4.0f;
	cout<<"printing a:";
	printFloatArray(a);
	floatArray b(a);
	cout<<"printing b:";
	printFloatArray(b);
	floatArray c=b=a;
	cout<<"printing c:";
	printFloatArray(c);
	a=a=a=a;
	cout<<"printing a:";
	printFloatArray(a);
}


problem is as soon as i compile it shows no error but following warnings
floatarray.h(2): warning C4067: unexpected tokens following preprocessor directive - expected a newline.
program do not execute saying floatArray.exe had stopped working.
is this a problem related to dynamic memory??
Last edited on
I believe you were warned about this constructor in
your other post
1
2
3
4
floatArray::floatArray()
{
	*mData=0.0f;
}
Hi.
I think problem is bold code:
floatArray::floatArray()
{
*mData=0.0f;
}

because before you allocate a memory to mData, your are using it. Why you want to zero it? If you want to sure that mData is not point to any place, try this code mData=0;.
Last edited on
thanks for such fast reply!! if this particular constructor is wrong then could you please tell how am i supposed to create floatArray with zero elements.
i did what MRRITE said but it doesnt seem to be working.
It dosen't work, because your class design is half baked.
It is all doomed to failure.

In the default constructor floatArray::floatArray() you say that you have
now set the mData member to 0. What about the mSize member.

In this other constructor:
1
2
3
4
5
floatArray::floatArray(int size)
{
	size=mSize; //What????
//Also what about the mData member????
}




In this function: - Is this enough??
1
2
3
4
void floatArray::resize(int newSize)
{
	mSize=newSize;
}


Come to think of it - what is the purpose of the mSize member - does it show how many actual values are use in the array - or the actual size of the array
bnecause those are two different things.

In this function
1
2
3
4
float& floatArray::operator[](int i)
{
	return mData[i];
}

There are absolutely no checks for array out of bounds access - or
wherther the mData pointer is valid. (The mData pointer will be INVALID
after a floatArray variable is created with the default constructor because mData is 0).

Oh the list goes on and on....



ok i agree my class design is half baked. here are the few changes i made as pointed by
guestgulkan:
1
2
3
4
5
floatArray::floatArray(int size)
{
	mSize=size;
	float* mData= new float[size];
}

and secondly:
1
2
3
4
5
6
void floatArray::resize(int newSize)
{
	float*mData=new float(newSize);
	mSize=newSize;
	delete []mData;
}

i appreciate your effort in pointing these mistakes out. still no success with program .further assistance would be helpful .
*edit
1
2
3
4
5
6
void floatArray::resize(int newSize)
{
	float*mData=new float[newSize];//changed () to []
	mSize=newSize;
	delete []mData;
}
Sorry - any further assistance will have to wait until tomorrow (31 July) - busy atm
ha ha.... its already 31st july in my region. but still i appreciate your interest .
okay i see some problem with implementation now.
i wasnt allocating memory to my pointer in:
1
2
3
4
5
floatArray::floatArray(int size)
{
	mSize=size;
	float* mData= new float[size];
}

so i did following changes:
1
2
3
4
5
floatArray::floatArray(int size)
{
	mSize=size;
	mData= new float[size];//changed float*mData to mData
}

and
1
2
3
4
5
6
void floatArray::resize(int newSize)
{
	mData=new float[newSize];//changed float*mData to mData
	mSize=newSize;
	delete []mData;
}

also i think its the resize function that creating the problem. bcoz as soon as i exclude a.resize(4) in main.cpp it prints a then stops.
also after debugging it stops at point floatArray.cpp line 17.
1
2
3
4
5
6
void floatArray::resize(int newSize)
{
	mData=new float[newSize];//changed float*mData to mData
	mSize=newSize;
	delete []mData;
}


You've got your order messed up.

1
2
3
4
5
6
7
void floatArray::resize(int newSize)
{
	delete []mData;
	mData=new float[newSize];//changed float*mData to mData
	mSize=newSize;

}
Last edited on
closed account (o1vk4iN6)
This doesn't do what is intended (assume header guard).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef FLOAT ARRAY_H
#define FLOAT ARRAY_H

    #ifndef FLOAT // is already defined
    #endif

#endif


// same behaviour as above
#ifndef FLOAT
#define FLOAT 

#endif 


changed it to:
1
2
3
4
5
6
7
8
9
void floatArray::resize(int newSize)
{
	float* temp=new float[newSize];
	for(int i=0;i<mSize;i++)//to allocate elements of mData to temp
		temp[i]=mData[i];
	delete []mData;
	mSize=newSize;
	mData=temp;
}
sorry was'nt available due to system crash.
i figured i need to change this function so as to create a new array for mSize>newSize as well. so here is my implementation again:
void floatArray::resize(int newSize)
{
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	float* temp=new float[newSize];
	if(mSize<newSize)
	{
	for(int i=0;i<mSize;i++)
		temp[i]=mData[i];
	}
	else
	{
	for(int i=0;i<newSize;i++)
		temp[i]=mData[i];
	}
	delete []mData;
	mSize=newSize;
	mData=temp;
}

still no luck with this program.
What does your whole program look like now?
Topic archived. No new replies allowed.