Dynamic Memory Allocation

This is a discussion on Dynamic Memory Allocation within the C Programming forums, part of the General Programming Boards category; I am creating my first program using dynamic memory allocation, I have started it but can't get past how far ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    I am creating my first program using dynamic memory allocation, I have started it but can't get past how far I am now. I was just looking for advice on how to fix up what I have, don't need any tips for the file part yet, just looking to understand what I have now. Yes this is homework but I have honestly tried and done a lot myself, I just need a push in the right direction. This is my code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct point
    {
    	int x; //point x
    	int y; //point y
    	char label[21]; //label of 2 points
    	struct point *ptrNext;		// self referential pointer
    };
    
    int isEmptyList(struct point *ptrF);
    void PrintList(struct point *ptrF);
    void ResetList(struct point *ptrF, struct point *ptrL);
    void AddToEnd(struct point *ptrF, struct point *ptrL);
    void AddToBeginning(struct point *ptrF, struct point *ptrL);
    void InputRecord(struct point *ptrNew); // used by Add to interactively get the values from the user
    //emptylist will determine if the list is empty
    int isEmptyList(struct point *ptrF)
    {
    	if(ptrF=NULL)
    		return 0;
    	else
    		return 1;
    }
    //print list will print out everything
    void PrintList(struct point *ptrF)
    {
    	if(ptrF!=NULL)
    	{
    		printf("label: %s\nx: %d y: %d\n",ptrF->label,ptrF->x,ptrF->y);
    		PrintList(ptrF->ptrNext);
    	}	
    }
    //reset list will make everything equal to NULL
    void ResetList(struct point *ptrF, struct point *ptrL)
    {
    	ptrF= NULL;
    	ptrL= NULL;
    }
    //addtobeginning will add a number to the beginning
    void AddToBeginning(struct point *ptrF, struct point *ptrL)
    {
    	struct point ptrNew;
    	InputRecord(&ptrNew);
    	ptrNew.ptrNext=ptrF;
    	ptrF=&ptrNew;
    }
    //addtoend will add a number to the end
    void AddToEnd(struct point *ptrF, struct point *ptrL)
    {
    	if(ptrF==NULL)
    		InputRecord(ptrF);
    	else
    	{
    		struct point New;
    		InputRecord(&New);
    		ptrL=&New;
    	}	
    }
    //reads in input
    void InputRecord(struct point *ptrNew)
    {
    	printf("Please enter the 2 Data points(x and y)");
    	scanf("%d%d", ptrNew->x, ptrNew->y);
    	printf("Please enter the label");
    	scanf("%s", ptrNew->label);
    	ptrNew->ptrNext=NULL;
    }
    
    struct point *ptrFirst = NULL;
    struct point *ptrLast = NULL;
    
    void main()
    {
    	int empty=0;//helps determine if list is empty
    	int input=1;//reads in user input
    	while(input!=0)
    	{
    		printf(
    		"1. Add a point at the END of the list.\n"
    		"2. Add a point at the BEGINNING of the list.\n"
    		"3. Is the list empty?\n"
    		"4. Erase all points from the list (reset).\n"
    		"5. Display the list.\n"
    		"6. Save the list to a sequential file (reset/replace file contents)\n"
    		"7. Read the list back from a sequential file (reset/replace current memory content)\n"
    		"0. Exit\n"
    		);
    		scanf("%d", &input);
    		if(input==1)
    		{
    			AddToEnd(ptrFirst,ptrLast);
    		}
    		if(input==2)
    		{
    			AddToBeginning(ptrFirst,ptrLast);
    		}
    		if(input==3)
    		{
    			empty=isEmptyList(ptrFirst);
    			if(empty==1)
    			{
    				printf("The list is not empty");
    			}
    			else
    			{
    				printf("The list is empty");
    			}
    		}
    		if(input==4)
    		{
    			ResetList(ptrFirst,ptrLast);
    		}
    		if(input==5)
    		{
    			PrintList(ptrFirst);
    		}
    		if(input==6)
    		{
    		}
    		if(input==7)
    		{
    		}
    		if(input==0)
    		{
    			input=0;
    		}
    		else
    			printf("Invalid entry, try again\n");
    	}
    }
    My first problem is that when asking to scan in the first "input" from the user it gives segmentation fault(core dumped)
    Last edited by JamesD; 03-11-2011 at 11:00 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Really?! You can enter any number e.g. 99 and it seg faults?

    I find this bit rather amusing:
    Code:
    		if(input==0)
    		{
    			input=0;
    		}
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    I guess the seg fault was only for 1, but yea I guess that code is useless too lol. However it won't print correctly and will always say the list is empty, not sure what to do there.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nothing you do there is dynamic.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    Isn't this dynamic? Its a way to avoid making a "set" amount of spaces in an array. This is my first time working with it, maybe I screwed up hard, but that was my understanding of it. Do you have any advice on how to continue with it?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void AddToBeginning(struct point *ptrF, struct point *ptrL)
    {
    	struct point ptrNew;
    	InputRecord(&ptrNew);
    	ptrNew.ptrNext=ptrF;
    	ptrF=&ptrNew;
    }
    No, that's not dynamic. You are creating a local structure, which is destroyed as soon as that function returns. Therefore, anything that points to it, now points to an invalid memory location.

    Dynamic memory allocation usually involves something like malloc setting aside a spot in memory for you to treat as a specific type of data. Then if you need more, you use malloc again to get more memory. When you're all done, you use something like free to get rid of it all.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A good tutorial:
    Eternally Confuzzled - Linked List Tutorial

    Really goes into it, imo.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    This is embarrassing... I have written the whole program incorrectly so far. I realize that I need to use malloc(). Any chance you could give me an example of that, just a small one so I could use it to make changes to all this? If someone could help with that that would be AMAZING.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's waiting for you at the tutorial, James.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct foo *newnumber( int v )
    {
        struct foo *n = malloc( sizeof *n );
        if( n != NULL )
        {
            n->value = v;
        }
    
        return n;
    }
    ...
    
    struct foo *p = NULL;
    
    p = newnumber( 4 );
    if( p != NULL )
    {
        printf( "p->value = %d", p->value );
        free( p );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Location
    Windsor, Ontario
    Posts
    44
    Yea saw the tutorial after I posted, I am reading through it thanks Adak and thanks for the code too quzah. I'm gonna read through some stuff and re-write this whole thing tomorrow hopefully, I will re-post my new code tomorrow night hopefully around the same time.
    Last edited by JamesD; 03-12-2011 at 12:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 06:04 PM
  4. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21