-1

I tested it at DEV C++ and Code Blocks , result was same. At console when I pressed Enter button than I saw "name has stopped working".

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>

using namespace std;

struct ll {
 int value;
 ll * next;
};

int main() {
  int n;
  ll a;
  cin>>n;
  a.value=n;
  ll cur;
  cur=a;
  // error is something here 
  while (n!=0){
    cin>>n;
    cur=*cur.next;
    cur.value=n;
  }
  //  has stopped working
  system("pause");
  return 0;
}
Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
  • 1
    Your cur's next is not initialized, it may contain almost anything, it probably points to the part of memory, where You're not allowed to write to. Initialize Your variables! – Roman Hocke Jan 02 '14 at 20:14
  • 1
    `system("pause")`!!! My eyes are bleeding! – Proxy Jan 02 '14 at 20:42

1 Answers1

4

You're not allocating memory for the next node in the linked list.

int main(){
  int n;
  ll a;
  cin>>n;
  a.value=n;
  ll cur; // memory for the 2-member struct called ll is allocated on the stack here.

But in here, you're not allocating new memory for the next object:

  while (n!=0){
    cin>>n;
    cur=*cur.next; // cur.next is null, or worse, undefined and refers to a random address.
    cur.value=n;
  }

Allocating the head of your linked list on the stack works fine, but you need to at least find memory for the additional elements of your linked list. I'd recommend allocating everything on the heap.

Here's how to allocate memory from the heap for each additional node:

  while (n!=0){
    cin>>n;
    cur.next = new ll();
    cur=*cur.next;
    cur.value=n;
  }
antiduh
  • 11,853
  • 4
  • 43
  • 66
  • @user3154916 - see my edits. While this will get you further, you should learn how heap vs stack memory works, how to allocate and free memory, and how to make sure you don't leak memory etc. C/C++ have very specific memory models that can be confusing. You should take the time to learn how they work. – antiduh Jan 02 '14 at 20:17
  • You might want to check out my recent answer about stack vs heap memory. It's written in the context of how java works, but a lot of it is universally applicable. http://stackoverflow.com/a/20833010/344638 – antiduh Jan 02 '14 at 20:20
  • there is arror at line of malloc "ivalid conversion from 'void' to 'll*'" – user3154916 Jan 02 '14 at 20:28
  • @user3154916 - My bad; i forgot this is c++. See edits. I/You/We need to cast the result from malloc. – antiduh Jan 02 '14 at 20:30
  • thanks so much. I have another question, after read integers from console, I wrote for (ll i=a; i.next!=NULL; i=*i.next){ cout< – user3154916 Jan 02 '14 at 20:41
  • Sorry, my c++ is a little rusty. One reason i like to participate here, one way to help and keep fresh.. – antiduh Jan 02 '14 at 20:51
  • @user3154916 - see my edits again. Instead of using `malloc`, we're using `new` as we should be in c++. Also, you should probably define a default constructor for your struct to initialize its values to defaults, so that they have sane values after newing them. The reason why your loop where you iterate through the linked list is failing is that ll.next contains garbage until it is initialized, not default values. The last ll you allocate never has its next written to, so it contains garbage that's not NULL, you dereference it, and you crash. – antiduh Jan 02 '14 at 20:59
  • how can I define default constructor? – user3154916 Jan 02 '14 at 21:01
  • @user3154916 - See this post: http://stackoverflow.com/a/1069634/344638 - It's kinda hard to type code in comments :) – antiduh Jan 02 '14 at 21:02
  • @GlennTeitelbaum - I understand, but I do want to walk this person through one thing at a time without modifying their code too much. – antiduh Jan 02 '14 at 21:04
  • I just want to write simple linked list code, read integers and then write, I'm trying this full day, but without success, can you give me this simple program code? – user3154916 Jan 02 '14 at 21:19