exe file had stopped working

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 5015 Views - Last Post: 30 July 2012 - 06:35 PM Rate Topic: -----

#1 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

exe file had stopped working

Posted 29 July 2012 - 02:34 AM

i am writing a program where i have to perform different operation using float arrays. here is my complete program:
header file:
//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:
//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 :
//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??

Is This A Good Question/Topic? 0
  • +

Replies To: exe file had stopped working

#2 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 29 July 2012 - 02:47 AM

i this problem related to constructor
floatArray::floatArray()
{
	*mData=0.0f;
}

then how am i supposed to create an array with zero elements??
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,354
  • Joined: 31-December 10

Re: exe file had stopped working

Posted 29 July 2012 - 06:45 AM

Just set the mData pointer to NULL or zero and that can signify an empty array. It's when you add elements to the array that you need to deal with memory allocation.
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,225
  • Joined: 25-December 09

Re: exe file had stopped working

Posted 29 July 2012 - 06:57 AM

The following message:

Quote

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.

Is being caused by the following two lines:
#ifndef FLOAT ARRAY_H
#define FLOAT ARRAY_H

You need to put underscores between the FLOAT and the ARRAY_H:
#ifndef FLOAT_ARRAY_H
#define FLOAT_ARRAY_H

The pre-processor is getting confused because of the space. As already noted this is not the cause of your program crash, but it will cause problems if not fixed.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 29 July 2012 - 02:58 PM

so what could be a possible reason for the crash?? i am unable to figure out yet!!
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,225
  • Joined: 25-December 09

Re: exe file had stopped working

Posted 29 July 2012 - 03:40 PM

Have you run your program through your debugger? Your debugger will tell you where it detects the error and allow you to view the variables at the time of the crash.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 30 July 2012 - 03:58 AM

ok i did ran the debugger. i m still learning to use it. however it break at :
line 17 main.cpp.
also i figured that my class is half baked so i made the following changes:
floatArray::floatArray(int size)
{
	mSize=size;
	float* mData= new float[size];
}

and secondly:
void floatArray::resize(int newSize)
{
	float*mData=new float(newSize);
	mSize=newSize;
	delete []mData;
}

still no success with program .further assistance would be helpful.
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 680
  • View blog
  • Posts: 2,354
  • Joined: 31-December 10

Re: exe file had stopped working

Posted 30 July 2012 - 04:40 AM

In the resize function, you need to delete the old memory first, and then allocate the new memory.
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1735
  • View blog
  • Posts: 3,338
  • Joined: 30-May 10

Re: exe file had stopped working

Posted 30 July 2012 - 09:31 AM

Be careful with your () and []

> 4 float* mData= new float[size];
Allocates an array of floats.

> 3 float*mData=new float(newSize);
Allocates just ONE float, and initialises it to newSize (promoted from int to float)
Was This Post Helpful? 1
  • +
  • -

#10 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 30 July 2012 - 02:38 PM

agreed that was a silly mistake. changed it float*mData= new float[newSize];
still no luck with program. ran through the debugger breaking at same point i.e.
line 17 main.cpp
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,225
  • Joined: 25-December 09

Re: exe file had stopped working

Posted 30 July 2012 - 03:30 PM

Post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 30 July 2012 - 03:36 PM

//floatArray.h
#ifndef FLOAT_ARRAY_H
#define FLOAT_ARRAY_H
#include<iostream>
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


//floatArray.cpp
#include"floatArray.h"
#include<iostream>

using namespace std;
floatArray::floatArray()
{
	*mData=0.0f;
	
}
floatArray::floatArray(int size)
{
	mSize=size;
	float* mData= new float[size];
}
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];
	delete[]mData;
}
floatArray::~floatArray()
{
	delete[] mData;
	mData=0;
}
void floatArray::resize(int newSize)
{
	float*mData=new float[newSize];
	mSize=newSize;
	delete []mData;
}
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;
}


//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(4);
	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);
}


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4217
  • View blog
  • Posts: 13,225
  • Joined: 25-December 09

Re: exe file had stopped working

Posted 30 July 2012 - 03:45 PM

You are not allocating memory for your pointer. In the following snippet:
floatArray::floatArray(int size)
{
	mSize=size;
	float* mData= new float[size];
}

You create a new variable named mData and assign memory to it, then the function returns and the pointer to that memory is lost. You are not using the mData variable that is part of your class, you created another with the same name.

You really need to see about increasing the warning level of your compiler, you should be getting several warnings:

Quote

main.cpp||In constructor ‘floatArray::floatArray(int)’:|
main.cpp|33|warning: declaration of ‘size’ shadows a member of 'this' [-Wshadow]|
main.cpp|36|warning: declaration of ‘mData’ shadows a member of 'this' [-Wshadow]|
main.cpp|36|warning: unused variable ‘mData’ [-Wunused-variable]|
main.cpp||In member function ‘void floatArray::resize(int)’:|
main.cpp|53|warning: declaration of ‘mData’ shadows a member of 'this' [-Wshadow]|
main.cpp||In function ‘void printFloatArray(floatArray&)’:|
main.cpp|81|warning: no previous declaration for ‘void printFloatArray(floatArray&)’ [-Wmissing-declarations]|
||=== Build finished: 0 errors, 5 warnings ===|


Also in your default constructor you are trying to assign a value to an uninitialized pointer, not good.

Jim
Was This Post Helpful? 1
  • +
  • -

#14 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 30 July 2012 - 04:41 PM

sry for this noob question. could you please tell me how to increase my warning level. okay i got your point that i am not using mData that is part of my class. how am i supposed to allocate memory to my pointer??

This post has been edited by cniper: 30 July 2012 - 04:44 PM

Was This Post Helpful? 0
  • +
  • -

#15 cniper  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 02-July 12

Re: exe file had stopped working

Posted 30 July 2012 - 04:58 PM

hey i seemed to be working a little!! these are the changes i made:
floatArray::floatArray(int size)
{
	mSize=size;
	mData= new float[size];//float*mData to mData
}
void floatArray::resize(int newSize)
{
	mData=new float[newSize];
	mSize=newSize;
	delete []mData;//float*mData to mData
}

after debugging it breaks at point floatArray.cpp line 17.
mData=new float[size]

This post has been edited by cniper: 30 July 2012 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2