![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
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! |
|
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Any idea why this code:
void Numbers::print(Numbers &n1)
{
cout << n1.firsts->value;
//temp = n1.firsts->value;
}(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! |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#7 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
Quote:
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! |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#9 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|