struct Node{
int n;
Node *next;

};
struct Some{

Node * ptr;
};

int main()

{

Node *aNode = new Node;
Some *aSome;

aSome->ptr = aNode; 

}

Why do I get a Seg Fault here?

Recommended Answers

All 29 Replies

because no memory has been allocated to aSome -- its nothing more than a pointer that has a random address.

would this be the fix?

struct Node{
int n;
Node *next;

};
struct Some{

Node * ptr;
};

int main()

{

Node *aNode = new Node;
Some *aSome;

aSome->ptr = aNode; 
aSome = aSome-> ptr;

}

but then it says can't convert Node to Some in assignment.

He said you're not allocating aSome......

Oh yeah, delete everything when you're done.

struct Node{
int n;
Node *next;

};
struct Some{

Node * ptr;
};

int main()

{

Node *aNode = new Node;
Some *aSome = new Some;

aSome->ptr = aNode; 


}

would aSome->ptr = aNode suffice?
Or do I have to do

aSome = aSome->ptr; ?

would aSome->ptr = aNode suffice?
Or do I have to do

aSome = aSome->ptr; ?

Think about what the Some attribute ptr is.
The latter line would attempt to assign an instance of Node from it's own attribute ptr to aSome which is of type Some, which doesn't make sense.
ptr is of type Node, therefore you want to assign it an instance of a Node. And aSome is of type Some so if you want to assign a value to it it must also be of type Some

I didn't understand what you said @_@

Ok you have a struct called Some.

This has an attribute called ptr, which is of type 'Node'.

If you want to assign a value to ptr then that value must also be of type 'Node'.

Doing aSome = aSome->ptr; would be attempting to assign a Node to aSome, however aSome is of type Some, so this would not work.

If you want aSome to hold aNode then you would do as you said earlier: aSome->ptr = aNode;

Alright, another question.

Say, I have another some pointer.
And I want as a conidtion that if that "other" some pointer is NULL.
I want the first Node that aSome is pointing to to be transfer to that other Node.

How would I go about doing this?

Alright, another question.

Say, I have another some pointer.
And I want as a conidtion that if that "other" some pointer is NULL.
I want the first Node that aSome is pointing to to be transfer to that other Node.

How would I go about doing this?

Ok so you have:

Some *nullSome = NULL;
if( !nullSome )
{
nullSome->ptr = aSome->ptr;
}

Now the thing to remember is that both aSome->ptr and nullSome->ptr point to the same thing.

This can get very tricky memory-management-wise, for instance if you delete that pointer (i.e freeing the memory it points to) then you must be certain that no other instance of Some is pointing to it (otherwise an attempt to use it will generally result in undefined behaviour or a segfault), also you don't want to lose your original node, if you don't have something always pointing to it then there will be no way to free the memory later.

For this reason linked-lists (which is what your Node structure is) is usually encapsulated in a datatype that manages the adding, removing and retrieval of data.

The code that you have, I already thought up.
My question was, how do I make it that it's pointing to the first node? that say, aSome->ptr is pointing to?

because say, i have aSome->ptr in a while loop.
that's creating a linked list, which would then contain a lot of Nodes.

if I have nullSome->ptr = aSome->ptr.
How do I know which Node it's pointing to?

The code that you have, I already thought up.
My question was, how do I make it that it's pointing to the first node? that say, aSome->ptr is pointing to?

because say, i have aSome->ptr in a while loop.
that's creating a linked list, which would then contain a lot of Nodes.

if I have nullSome->ptr = aSome->ptr.
How do I know which Node it's pointing to?

Ok this is why you need a class to manage the nodes.

What does your while loop look like? My concern is that you may be creating new Nodes but not actually storing them in a retrievable place.

isn't a struct like a class?
But everything is public?
so why would I need a class to manage the nodes?

the code will be posted shortly.

Node * aNode = new Node;

     List * aList = new List;
     List * bList = new List;

      for (int i=0; i<25; i++)
  { 
     char choice;
     cout<<endl;
     cout<<"What do you choose?  "<<endl;
     cin>> choice;
     
     
        int counter =1;
        
        while (letter == 'A')
      {
        

             switch(choice)
               {
                case 'c':
                
                {
              
                aNode->next = new Node;
 		aNode = aNode->next;
		aNode ->data = counter;
                counter++;
		aList -> next_n = aNode;

               }
               break;
               
               default:
                       cout<<"Invalid input"<<endl;
                       }   // end of switch bracket            
            
             if( bList == NULL)
             {
  
		bList->nextp = aList->nextp     
                
                                }
}

Ok without knowing what 'List' is it looks like things are going to get lost there.

You're better off with something like this:

class LinkedList_Integer
{
protected:
  Node *first, *last;
  int count;
public:
  LinkedList_Integer(){ first=last=NULL; count=0; }
  ~LinkedList_Integer()
  {
    //loop through your nodes, starting at the end, and delete them. 
    //(you must start at the end and work backwards)
  }

void add_data( int _data )
{
  if(count==0)
  {
      first = new Node;
      first->data = _data;
      last=first;
  }else{
      last->next = new Node;
      last->next->data = _data;
      last=last->next;  
  }
  count++;
}

int get_data( int index )
{
  Node *n=first;
  for( int i=0; i<index; ++i )
  {
     if(n->next != NULL)
     {
        n=n->next;
     }else{
        std::cout << "Index out of bounds" << std::endl;
        return -1;
     }
  }
  return n->data;
}
};

Im doing that off the top of my head but I think the logic and arithmetic should hold.
This way you have a class (you could use a struct but it's not preferred in OO) which manages the adding and accessing of elements and then destroys them at the end (you can implement that yourself). It maintains a pointer to the first and last nodes so that nodes don't get lost.
To make it more generic you can use Templates, that way you are not restricted to using integers as the data type for the Nodes.
Writing it all in one big block is messy and error-prone.

but that doesn't answer the question.

Say, I have another some pointer.
And I want as a conidtion that if that "other" some pointer is NULL.
I want the first Node that aSome is pointing to to be transfer to that other Node.

How would I go about doing this?

Say

Struct Node 
{
Node * next;
int data;

Struct List
{
Node *ptr;
}


Also, what do you mean List seems like things are getting lost there?

but that doesn't answer the question.

Say, I have another some pointer.
And I want as a conidtion that if that "other" some pointer is NULL.
I want the first Node that aSome is pointing to to be transfer to that other Node.

How would I go about doing this?

Ok so you don't mean the pointer to otherSome, you mean otherSome's ptr attribute which is a pointer to a Node yes?
So a simple if statement?

if( otherSome->ptr == NULL )
{
  otherSome->ptr = aSome->ptr;
}

And when you say the first Node that aSome is pointing to, remember aSome's ptr attribute is only ever pointing to one Node.

Say

Struct Node 
{
Node * next;
int data;

Struct List
{
Node *ptr;
}


Also, what do you mean List seems like things are getting lost there?

Ok I guess it may be a case of variable naming not being descriptive. Your List struct isn't actually a list and realistically it does nothing, all it does is hold a Node, why have this abstraction? You already have a variable named aNode, putting that as an attribute of a struct that has no other attributes gives you nothing.

Basically, what I'm trying to do is.

There's a pointer, call cash_register.
And there's a linked list called Customers.
When Cash_register is NULL.
The first person on line, via first node in linked list aka customer.
Goes to Cash_register, meaning Cash_register points to that node.

Basically, what I'm trying to do is.

There's a pointer, call cash_register.
And there's a linked list called Customers.
When Cash_register is NULL.
The first person on line, via first node in linked list aka customer.
Goes to Cash_register, meaning Cash_register points to that node.

Ah ok then, you need to maintain a pointer to the first node anyway otherwise you have no way to free the memory.

So adding nodes:

//init first node
Node *first = new Node;
first->ptr = NULL;

//put this in an 'add node' function
Node *last = first;
while( last->next != NULL ) //while we are not at the end
{
 last=last->next; //get the next node
}
//at this point we have the last node so we can add one
last->next = new Node; //create a new node on the end
last->next->next = NULL; //set it's 'next' pointer to NULL
last->next->data = whatever; //set it's data

In order to 'process' all of the nodes in the linked list:

while( cashRegister->ptr != NULL ) //while we aren't at the end
{
  //do the cash register processing
  cashRegister->ptr = cashRegister->ptr->next; //set the cash register's ptr to the next node in the list, when it gets to the end this will be 'NULL' and the loop will terminate
}

Cash_register and Customer are from different structs.

Cash_register and Customer are from different structs.

It doesn't matter where they are from, the principal is the same.

Perhaps post the code you have, like for the Cash_register and Customer so I can see the relationship between all your data types.

It seems you want a linked list of nodes (the customers) that should be passed to the processor (the cash_register) which will do 'something' to all of nodes (customers) in the linked list. The above way is a method for doing that, but im not sure how that fits to your datatypes so I just made assumptions.

The Node and List structs I posted earlier, is just what I have.
Just that Node is Customer.
And List is Cash-Register

and why did you call the Cash_register the processor?

The Node and List structs I posted earlier, is just what I have.
Just that Node is Customer.
And List is Cash-Register

Ok well the 'List' struct is effectively pointless as it does nothing but hold a pointer to a node why not just use a variable? 'List' provides nothing but an extra layer of unnecessary abstraction.

and why did you call the Cash_register the processor?

The reason for that is for the program to actually do anything there must be a data processing step. You have a collection that you want to pass through the 'cash register' so you obviously want the cash register to process that data in some way, be that modifying the data, printing it out, etc...

What is it exactly that you are trying to do? The above example shows how to create the linked list and then iterate over it to extract/process the data. What is the purpose of the 'List' struct? It holds a Node pointer, but why?

The List is representing the Cash_register pointer, if that's pointing to NULL, then the first node in the Customer linked list nodes will go to Cash_register.

The List is representing the Cash_register pointer, if that's pointing to NULL, then the first node in the Customer linked list nodes will go to Cash_register.

Well ofcourse that is very simple. Just an if statement checking to see if the Cash_register's ptr attribute is NULL, if so assign it the first node in the Customer list.
But that's not actually going to do anything. That will not iterate over the list, see the previous examples for how to do that. And by what process will the cash_register's ptr become NULL? Presumably it will be NULL at the start, then it will have the first customer node assigned and then presumably it will never be NULL again, if it does get set to NULL again then it will just have the first custome node assigned again.
As you can see that would really achieve nothing, there is pretty much no point to doing it.

What's the actual thing you are trying to achieve with this project?

It will be NULL, when the user presses a button, like x.
And that'll make the "first node" that the cash_register pointing to, return to the line, (Though that's not really what happens in real world)
but the customer is returned to the line.

And when the first node goes to the cash register.
The second node is now the first node on the customer line.

It will be NULL, when the user presses a button, like x.
And that'll make the "first node" that the cash_register pointing to, return to the line, (Though that's not really what happens in real world)
but the customer is returned to the line.

And when the first node goes to the cash register.
The second node is now the first node on the customer line.

So you want the first customer to return to the back of the line?
Why worry about setting it to NULL at all? You know how to access the next person in line so just set it directly to that.

cash_register->ptr=cash_register->ptr->next;

Otherwise the setting it to NULL method just means you would set it to NULL, then check if it's NULL (which it obviously will be) and set the next customer on it. So that part would be redundant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.