Question:
Dynamic Memory in C programming?
2011-03-12 16:15:10 UTC
Quite simply I am trying to work with dynamic memory in C, I wrote some of my program so far and it compiles but doesn't work. What should I do to fix it, the most common problem is when I try to enter data in it just gives: segmentation fault(core dumped).

#include
#include

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)
{
struct point *pD = ptrF;
while(pD != NULL)
{
ptrF = ptrF->ptrNext; /* update the pointer to the beginning of the list to point to the next */
free(pD); /* this (previous) node is erased! */
pD = ptrF; /* set the pD to point to the next node to be deleted */
}
}
//print list will print out everything
void PrintList(struct point *ptrF)
{
struct point *pC = ptrF;
while(pC != NULL)
{
printf("Label: %s\nx: %d y:%d\n", pC->label, pC->x, pC->y);
pC = pC->ptrNext; /* this is how a list is traversed! */
}
}
//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;
if(ptrF==NULL)
*ptrF=ptrNew;
InputRecord(&ptrNew);
ptrNew.ptrNext=ptrF;
ptrF=&ptrNew;
}
//addtoend will add a number to the end
void AddToEnd(struct point *ptrF, struct point *ptrL)
{
struct point ptrNew;
if(ptrF==NULL)
*ptrF = ptrNew;
else
{
InputRecord(&ptrNew);
ptrL=&ptrNew;
}
}
//reads in input
void InputRecord(struct point *ptrNew)
{
printf("Please enter the 2 Data points(x and y)");
ptrNew=(struct point *)(malloc(sizeof(struct point)));
if(ptrNew==NULL)
printf("No more memory.\n");
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\n"
"(reset/replace current memory content)\n"
"0. Exit\n\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\n\n");
}
else
{
printf("The list is empty\n\n");
}
}
if(input==4)
{
ResetList(ptrFirst,ptrLast);
}
if(input==5)
{
PrintList(ptrFirst);
}
if(input==6)
{
}
if(input==7)
{
}
if(input==0)
{
input=0;
}
if(input<0)
printf("Invalid entry, try again\n\n");
if(input>7)
printf("Invalid entry, try again\n\n");
}
}
Three answers:
Ratchetr
2011-03-12 17:13:21 UTC
You are doing this in a couple of your functions:



struct point ptrNew;

if(ptrF==NULL)

*ptrF = ptrNew;



But ptrNew is a LOCAL variable. It is on the stack, and it will GO AWAY after the function returns.

All of your struct point memory HAS to come from malloc. Don't put pointers to local variables in your list.



I don't think you understand what isEmptyList is supposed to do. It's supposed to answer a question:

Is the list empty?

But what it really does is empty the list. Then doesn't even provide a return value to tell you that...yeah...the list is empty because I just emptied it.

Rethink the logic there.
debrodie
2016-10-28 09:49:04 UTC
I hate to be snide yet i have not, to my understanding, ever helped everybody b y giving them variety code. I shop some workouts on disks to reuse and that i advise you do too. To allocate memory dynamically it is ordinary. To allocate ten integers attempt: Array=malloc(10*sizeof(int)); For floats Array=malloc(10*sizeof(waft)); and obviously a similar for double. sizeof returns the size of a type in bytes -- it may well be consumer defined or predefined. Multiply that circumstances the fashion of aspects you choose, and think ofyou've got a pointer to the first element of the array. very oftentimes, sure, you are able to manage it as an array. in basic terms be certain to do: free(Array) once you're achieved with it because once you employ malloc the device received't %. up your garbage.
ItachisXeyes
2011-03-12 16:20:18 UTC
well what that means is that at some point you are trying to access memory that doesn't belong to your program or the veritable you are accessing.

how far does it get?

maybe add more printf statements to narrow down the line of code that is causing the segfault.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...