Thread: program executes but crashes when returning 0

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    8

    program executes but crashes when returning 0

    for some reason, my code does create a random linked dynamic data structure, but whenever i try and terminate it it constatntly crashes....please help.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    int sentinel = 0;
    
    typedef struct list{
    char c;
    struct list *listPtr;
    } List;
    
    void populate (List);
    
    int main(){
    srand(time(NULL));
    
    List list1;
    populate(list1);
    printf("%d\n", sizeof(List)); 
    free(&list1);
    system("PAUSE");
    return 0;
    }
    
    
    void populate(List list){
    List *currentPtr = &list; 
    
    for (sentinel = 0; sentinel <= 10; sentinel++){
    List *newNode = (List *) malloc(sizeof(List));
    currentPtr->listPtr = newNode;
    newNode = ++currentPtr; 
    newNode->c = rand() % 26 + 65;
    newNode->listPtr = NULL;
    
    printf("%c->", newNode->c);
    }
    printf("NULL\n\n");
    }
    i have noticed that if i remove the while loop and just generate 1 node, the thing executes and terminates fine. could it be that i cant use malloc in a while loop? if so, please explain....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    0. You need to indent your code properly.

    1. The populate function should probably have a pointer parameter since you want to change the argument.

    2. This looks wrong since currentPtr points to a lone object, not an element of an array:
    Code:
    newNode = ++currentPtr;
    3. This is wrong since list1 was not allocated by malloc (or the like):
    Code:
    free(&list1);
    Perhaps you should define a depopulate function instead.

    4. sentinel should not be a global variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    Quote Originally Posted by laserlight View Post
    0. You need to indent your code properly.

    1. The populate function should probably have a pointer parameter since you want to change the argument.

    2. This looks wrong since currentPtr points to a lone object, not an element of an array:
    Code:
    newNode = ++currentPtr;
    3. This is wrong since list1 was not allocated by malloc (or the like):
    Code:
    free(&list1);
    Perhaps you should define a depopulate function instead.

    4. sentinel should not be a global variable.
    for 3, well, idk why i slapped it in. im fairly sure the memory used at run time clears as you termiate the process. that, and you can increment pointers to point to neighboring linked structs.

    i mean, go ahead and copypasta the code into your compiler and run it. it works fine, it just doesn't close properly...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by returnNULL
    im fairly sure the memory used at run time clears as you termiate the process.
    That will probably happen in a modern desktop operating system, but nonetheless it is good practice not to rely on that. What you can rely on is the memory for list1 getting deallocated without having to use free (and as stated, you should not be using free), but the memory that you allocated with malloc has no such guarantee.

    Quote Originally Posted by returnNULL
    you can increment pointers to point to neighboring linked structs.
    No, you cannot. Incrementing a pointer makes sense if it points to an element of an array. In the case of a lone object, it is allowed, but then the pointer would not "point to neighboring linked structs", but rather be like a "one past the end" pointer. Rather, you should write something like:
    Code:
    newNode = currentPtr->listPtr;
    but that would be dumb in this case because newNode already points to that.

    Quote Originally Posted by returnNULL
    i mean, go ahead and copypasta the code into your compiler and run it. it works fine, it just doesn't close properly...
    That is an effect of undefined behaviour. So no, contrary to what you may believe, your code does not work fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    8
    yeah that pointer was pointless (no pun intended). thanks for telling me to remove it, cause now the program works fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-19-2009, 03:00 AM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Program executes, but doesn't start
    By Marauder_Pilot in forum C Programming
    Replies: 1
    Last Post: 11-30-2007, 05:28 PM
  4. My program crashes :|
    By Testify in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2007, 11:48 AM
  5. Program crashes
    By fkheng in forum C Programming
    Replies: 12
    Last Post: 06-24-2003, 04:59 AM

Tags for this Thread