Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 2nd, 2005, 3:34 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Accessing a pointer-to-structure value

Sorry, this will be the last question of the day :p

say I had this code:

#include <iostream>

using namespace std;

struct Node
{
     int value;
};

int main()
{
     Node *p;

     p->value = 5;
     
     int number;

     number = p->value;

     cout << number;

     return 0;
}

This gives me a segmentation fault. How would I fix this?
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 2nd, 2005, 3:40 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Node *p = new Node;

You can also use Node p if you want.

you get the seg fault because you write to non-allocated memory.
Polyphemus_ is offline   Reply With Quote
Old Oct 2nd, 2005, 3:43 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Polyphemus_
Node *p = new Node;

You can also use Node p if you want.

you get the seg fault because you write to non-allocated memory.

thanks

*slaps head* I guess that is what I get for working all day on my own version of a valarry class using a linked-list...
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 2nd, 2005, 4:15 PM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Any idea why this code:

void Numbers::print(Numbers &n1)
{
	cout << n1.firsts->value;
	//temp = n1.firsts->value;
}
gives me a seg. fault?

(where Numbers is a linked list, and firsts is the first Node, and value is an integer value )
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 2nd, 2005, 4:56 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Three possibilities I can think of;

1) n1.firsts is not initialised correctlu, and therefore does not point at a valid object

2) you've changed the type of the value member in your class so it's a pointer, and haven't initialised that correctly;

3) you've managed to pass an invalid reference to the function, viz;
Node *x = NULL;
Node::print(*x);   // passing an invalid reference
grumpy is offline   Reply With Quote
Old Oct 2nd, 2005, 7:11 PM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by Jessehk
Any idea why this code:

void Numbers::print(Numbers &n1) { cout << n1.firsts->value; //temp = n1.firsts->value; }

gives me a seg. fault?

(where Numbers is a linked list, and firsts is the first Node, and value is an integer value )
Wasn't it solved in the other thread?
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Oct 2nd, 2005, 8:28 PM   #7
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by InfoGeek
Wasn't it solved in the other thread?
No.

everything in my number class works accept the copy constructor.

I'm trying to do it like this:

Numbers::Numbers(Numbers &num1) {
	firsts = currents = NULL;
	numberOf = 0;

	news = new Node;
	int temp = num1.firsts->value;
	news->value = temp;

	currents = news;

	firsts = news;
	firsts->nexts = NULL;

	currents = firsts;

	for(int x = 0; x < num1.numberOf; x++, num1.currents =  num1.currents->nexts) {
		news = new Node;
		currents->nexts = news;
		currents->value = num1.currents->value;
		currents = news;
	}

	currents->nexts = NULL;*/
}

and I keep getting seg. faults if I make it a copy constructor.

The really wierd thing is that if i rename it to print(Numbers &), it works fine ( which tells me that my data is fine, but my logic isn't).

I seem to get frusterated easily
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Oct 2nd, 2005, 9:45 PM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
1) You are committing one of the deadly sins of copy constructors - you are modifying the object being copied. Try making the function into Numbers::Numbers(const Numbers &). That will trigger some error messages, which you will need to fix. Odds are, those error messages will be related to a cause of your problem.

2) Whenever you dereference a pointer, you probably need to check that it is non-NULL. For example, computing num1.firsts->value and num1.currents->value.

3) Check the default constructor to make sure it initialises all the members of the class correctly. Otherwise, the copy constructor cannot work correctly (if an object is not initialised correctly, a copy of it cannot be created successfully either).

4) Your print() function, if it works this way, is also incorrect. Your problem is that the function changes the object passed to it (whether that is a copy constructor or a print() function) --- so, when you repeat the operations, you get yourself into trouble. On that basis, I suspect you will probably find calling your print() method twice also gives a segmentation fault.

5) Also, keep in mind that a copy constructor is invoked in a different context from other members: when an object is being created. Even if the code works correctly in your print() method, there is no reason to assume it will work correctly when pasted into a copy constructor.
grumpy is offline   Reply With Quote
Old Oct 3rd, 2005, 7:55 PM   #9
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Thanks for all the advice grumpy.

I have made the function take a const argument and I am still having problems.

I think I am going to "stash this one in a drawer" and look again when I have become a bit more knowledgable (hopefully) from books and expirience.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC