11 Replies - 1130 Views - Last Post: 16 February 2010 - 11:13 AM Rate Topic: -----

#1 neptunusmaris  Icon User is offline

  • New D.I.C Head

Reputation: -18
  • View blog
  • Posts: 42
  • Joined: 31-July 09

File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:20 AM

Hi C/C++ ninjas,

I have a problem understanding why you cannot do file i/o operations in a C++ constructor when declaring it as such:

MyClass *my = new MyClass;



Now for example within that constructor I could be executing file i/o operations such as printf or cout or even opening a file pointer ... the program fails and then the infamous "Segmentation Fault" occurs.

Why is this? I've never been taught why in class or never in my experience with C++ have come across this.

Thanks,
Neptune

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O in constructor when using class as pointer->object?

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:28 AM

I really can't think of a reason you should be having issues.

Why are you using printf in C++? Try it with cout etc.

Also, make sure you have the ()'s after MyClass
Was This Post Helpful? 0
  • +
  • -

#3 neptunusmaris  Icon User is offline

  • New D.I.C Head

Reputation: -18
  • View blog
  • Posts: 42
  • Joined: 31-July 09

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:32 AM

View Postbodom658, on 16 February 2010 - 09:28 AM, said:

I really can't think of a reason you should be having issues.

Why are you using printf in C++? Try it with cout etc.

Also, make sure you have the ()'s after MyClass



Well I just gave an example... either way with printf and cout or ()'s or not it still doesnt work...
Was This Post Helpful? 0
  • +
  • -

#4 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:37 AM

Perhaps you can show us a class that gives you these problems?
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:40 AM

View Postbodom658, on 16 February 2010 - 05:28 PM, said:

Also, make sure you have the ()'s after MyClass
That makes no difference - the default constructor is used regardless. On a related topic, there's another problem when using empty parenthesis if you're not allocating MyClass with 'new':
class MyClass {};

int main()
{
    MyClass my_object();  // Uh-oh!
} 
What is my_object? is it an object? or is it a function? You might think that its an object, but the compiler thinks its a function. When you come to do something with my_object which looks sensible, the compiler will throw up an error - and because you probably read the code thinking that my_object is an object, the error will be very confusing!

This post has been edited by Bench: 16 February 2010 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 neptunusmaris  Icon User is offline

  • New D.I.C Head

Reputation: -18
  • View blog
  • Posts: 42
  • Joined: 31-July 09

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:43 AM

View PostFerencn, on 16 February 2010 - 09:37 AM, said:

Perhaps you can show us a class that gives you these problems?



here and it doesn't matter if i use cout or printf I commented one or the other out and neither work alone or together.

class Test {
	public:
		Test(char *name);
		Test();
		char *m_name;
};

Test::Test(char *name) {
	strcpy(m_name, name);
	printf("Test print\n");
	cout << "Test message" << endl;
}

Test::Test() {  }


Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:47 AM

View Postneptunusmaris, on 16 February 2010 - 05:20 PM, said:

Now for example within that constructor I could be executing file i/o operations such as printf or cout or even opening a file pointer ... the program fails and then the infamous "Segmentation Fault" occurs.
A segmentation fault suggests to me that you de-referenced an uninitialised or 'dangling' pointer. its probably nothing to do with I/O and probably nothing to do with having I/O in a constructor either - I expect you tried to read data into some space which doesn't belong to your program

This post has been edited by Bench: 16 February 2010 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 neptunusmaris  Icon User is offline

  • New D.I.C Head

Reputation: -18
  • View blog
  • Posts: 42
  • Joined: 31-July 09

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:52 AM

View PostBench, on 16 February 2010 - 09:47 AM, said:

View Postneptunusmaris, on 16 February 2010 - 05:20 PM, said:

Now for example within that constructor I could be executing file i/o operations such as printf or cout or even opening a file pointer ... the program fails and then the infamous "Segmentation Fault" occurs.
A segmentation fault suggests to me that you de-referenced an uninitialised or 'dangling' pointer. it probably nothing to do with I/O, probably nothing to do with having I/O in a constructor - I expect you tried to read data into some space which doesn't belong to your program


Where am I "read data into some space which doesn't belong to your program" in here:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

class Test {
	public:
		Test(char *name);
		Test();
		char *m_name;
};

Test::Test(char *name) {
	strcpy(m_name, name);
	printf("Test print\n");
	cout << "Test message" << endl;
}

Test::Test() {  }

int main() {
	Test *t = new Test("Neptune");
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:54 AM

m_name is uninitialised - whatever memory location it points to doesn't belong to your program:

View Postneptunusmaris, on 16 February 2010 - 05:43 PM, said:

class Test {
	public:
		Test(char *name);
		Test();
		char *m_name;
};

Test::Test(char *name) {
	strcpy(m_name, name);
	printf("Test print\n");
	cout << "Test message" << endl;
}

Test::Test() {  }




This is a good argument for using strings. Don't ever use char* or char[] to represent plain text data.
#include <string>

class Test {
	public:
		Test(const string& name);
		Test();
		string m_name;
};

Test::Test(const string& name) {
	m_name = name;
	cout << "Test message" << endl;
} 

Was This Post Helpful? 1
  • +
  • -

#10 neptunusmaris  Icon User is offline

  • New D.I.C Head

Reputation: -18
  • View blog
  • Posts: 42
  • Joined: 31-July 09

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 10:57 AM

Just saw your new edit "Bench" I try it...

Thanks Bench it works now ... and now I understand the need to get out my "C" gear and into my "C++" gear
Was This Post Helpful? 0
  • +
  • -

#11 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 11:01 AM

It's your char arrays. I changed them to string types, and everything works fine.

EDIT: Hah. Beat me to it. That's what I get for not refreshing before posting. Glad you got it working.

This post has been edited by bodom658: 16 February 2010 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#12 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: File I/O in constructor when using class as pointer->object?

Posted 16 February 2010 - 11:13 AM

Good job guys... and a lesson learned!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1