Adding to linked list struct using a while loop doesn't save the data (IN C)
up vote
0
down vote
favorite
This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:
struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};
I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:
struct process * add(int prio,char* pat, char* par, struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}
So the main algorithm gets the lines from a file using fgets in a while loop :
while (fgets(line, sizeof(line), file))
then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.
This is my while loop and main algo :
FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;
while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs
We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():
void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}
}
the method prints nonsense :
prio : 2
2 , ./printchars , a 12 b
prio : 15
15 , ./printchars , c 23 d
2 , ,
prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f
it is supposed to print :
7 , ./printchars , e 34 f
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b
I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help
EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !
c pointers struct while-loop linked-list
|
show 8 more comments
up vote
0
down vote
favorite
This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:
struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};
I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:
struct process * add(int prio,char* pat, char* par, struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}
So the main algorithm gets the lines from a file using fgets in a while loop :
while (fgets(line, sizeof(line), file))
then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.
This is my while loop and main algo :
FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;
while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs
We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():
void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}
}
the method prints nonsense :
prio : 2
2 , ./printchars , a 12 b
prio : 15
15 , ./printchars , c 23 d
2 , ,
prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f
it is supposed to print :
7 , ./printchars , e 34 f
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b
I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help
EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !
c pointers struct while-loop linked-list
2
head = NULL;
... Doing this inside the algorithm you provided just after youmalloc()
-ed it... is in fact, flat out wrong!
– Ruks
Nov 22 at 6:27
@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30
1
No!, you should not usehead = NULL;
for that!memset(head, 0, 1);
is the way... You are trying to leak the object by assigningNULL
to it!
– Ruks
Nov 22 at 6:33
However, it's wrong.struct process *head = ( struct process *)malloc(sizeof(struct process));
allocates memory and stores it's address inhead
.head = NULL;
overrideshead
. Now, the just allocated memory is lost - it's a memory leak.
– Scheff
Nov 22 at 6:33
path
andparameters
are pointers. What are they pointing at? Dopath
andparameters
from differentstruct process
point at different places? What are these places?
– n.m.
Nov 22 at 6:35
|
show 8 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:
struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};
I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:
struct process * add(int prio,char* pat, char* par, struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}
So the main algorithm gets the lines from a file using fgets in a while loop :
while (fgets(line, sizeof(line), file))
then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.
This is my while loop and main algo :
FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;
while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs
We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():
void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}
}
the method prints nonsense :
prio : 2
2 , ./printchars , a 12 b
prio : 15
15 , ./printchars , c 23 d
2 , ,
prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f
it is supposed to print :
7 , ./printchars , e 34 f
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b
I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help
EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !
c pointers struct while-loop linked-list
This is one of my first questions so I hope I formulate it well. I checked some other similar questions on StackOverflow but I got no real answers from them. I have the following struct:
struct process {
int priority;
char* path;
char* parameters;
struct process *next;
};
I am reading each line from a file and adding the string tokens I get in my struct linked list using a while loop. Here is the adding method:
struct process * add(int prio,char* pat, char* par, struct process *head) {
struct process *new_node;
new_node = ( struct process *) malloc(sizeof( struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
head = new_node;
return head;
}
So the main algorithm gets the lines from a file using fgets in a while loop :
while (fgets(line, sizeof(line), file))
then I tokenize all the strings I need and use the add method to add them to my linked list. I convert the first string to int to respect the types.
This is my while loop and main algo :
FILE *file = fopen(filename , "r");
char line[124];
// can be put outside and passed as argument.
struct process *head = ( struct process *)malloc(sizeof(struct process));
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
new_node->next= head;
char* priority,*path,*parameters;
while (fgets(line, sizeof(line), file)) {
priority=strtok(line," ");
// The fix here is the following :
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1);
strcpy(path, path_token);
char *par_token = strtok(NULL, "n");
parameters = malloc(strlen(par_token) + 1);
strcpy(parameters, par_token);
// End of edit Thanks to the answers bellow
char* junk;
int p = strtol(priority,&junk,10);
printf("prio : %d ",p);
head = add(p, path, parameters,head);
printf("n");
pront(head);
printf("n");
}
\ free the mallocs
We notice here that I use the pront() method to print my linked list at every step. I also do it after my while loop. Here is the code for pront():
void pront(struct process *head) {
struct process *current_node = head;
while ( current_node != NULL) {
printf("%d , %s , %s ", current_node->priority, current_node->path, current_node->parameters);
printf("n");
current_node = current_node->next;
}
}
the method prints nonsense :
prio : 2
2 , ./printchars , a 12 b
prio : 15
15 , ./printchars , c 23 d
2 , ,
prio : 7
7 , ./printchars , e 34 f
15 , /printchars , 34 f
2 , ./printchars , e 34 f
it is supposed to print :
7 , ./printchars , e 34 f
15 , ./printchars , c 23 d
2 , ./printchars , a 12 b
I am sure that the problem comes from the while loop since when using the adding method outside of the loop and then printing, I get valid results. But as soon as it gets in the loop, the Head linked list doesn't store the values properly!
Thanks for the help
EDIT : The issue is fixed and the issue was my awfully wrong use of strtok and pointers to char's without malloc. Lesson learned !
c pointers struct while-loop linked-list
c pointers struct while-loop linked-list
edited Nov 22 at 7:01
asked Nov 22 at 6:23
Mamoun Debbarh
84
84
2
head = NULL;
... Doing this inside the algorithm you provided just after youmalloc()
-ed it... is in fact, flat out wrong!
– Ruks
Nov 22 at 6:27
@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30
1
No!, you should not usehead = NULL;
for that!memset(head, 0, 1);
is the way... You are trying to leak the object by assigningNULL
to it!
– Ruks
Nov 22 at 6:33
However, it's wrong.struct process *head = ( struct process *)malloc(sizeof(struct process));
allocates memory and stores it's address inhead
.head = NULL;
overrideshead
. Now, the just allocated memory is lost - it's a memory leak.
– Scheff
Nov 22 at 6:33
path
andparameters
are pointers. What are they pointing at? Dopath
andparameters
from differentstruct process
point at different places? What are these places?
– n.m.
Nov 22 at 6:35
|
show 8 more comments
2
head = NULL;
... Doing this inside the algorithm you provided just after youmalloc()
-ed it... is in fact, flat out wrong!
– Ruks
Nov 22 at 6:27
@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30
1
No!, you should not usehead = NULL;
for that!memset(head, 0, 1);
is the way... You are trying to leak the object by assigningNULL
to it!
– Ruks
Nov 22 at 6:33
However, it's wrong.struct process *head = ( struct process *)malloc(sizeof(struct process));
allocates memory and stores it's address inhead
.head = NULL;
overrideshead
. Now, the just allocated memory is lost - it's a memory leak.
– Scheff
Nov 22 at 6:33
path
andparameters
are pointers. What are they pointing at? Dopath
andparameters
from differentstruct process
point at different places? What are these places?
– n.m.
Nov 22 at 6:35
2
2
head = NULL;
... Doing this inside the algorithm you provided just after you malloc()
-ed it... is in fact, flat out wrong!– Ruks
Nov 22 at 6:27
head = NULL;
... Doing this inside the algorithm you provided just after you malloc()
-ed it... is in fact, flat out wrong!– Ruks
Nov 22 at 6:27
@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30
@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30
1
1
No!, you should not use
head = NULL;
for that! memset(head, 0, 1);
is the way... You are trying to leak the object by assigning NULL
to it!– Ruks
Nov 22 at 6:33
No!, you should not use
head = NULL;
for that! memset(head, 0, 1);
is the way... You are trying to leak the object by assigning NULL
to it!– Ruks
Nov 22 at 6:33
However, it's wrong.
struct process *head = ( struct process *)malloc(sizeof(struct process));
allocates memory and stores it's address in head
. head = NULL;
overrides head
. Now, the just allocated memory is lost - it's a memory leak.– Scheff
Nov 22 at 6:33
However, it's wrong.
struct process *head = ( struct process *)malloc(sizeof(struct process));
allocates memory and stores it's address in head
. head = NULL;
overrides head
. Now, the just allocated memory is lost - it's a memory leak.– Scheff
Nov 22 at 6:33
path
and parameters
are pointers. What are they pointing at? Do path
and parameters
from different struct process
point at different places? What are these places?– n.m.
Nov 22 at 6:35
path
and parameters
are pointers. What are they pointing at? Do path
and parameters
from different struct process
point at different places? What are these places?– n.m.
Nov 22 at 6:35
|
show 8 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Your linked list does not store the values at all.
The issue isn't the loop, but strtok
- you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.
So, to save the values, you must allocate memory for your strings (path
and parameters
) and use strcpy
to copy the strings to the allocated memory.
Remember to free
each item fields before freeing list items!
Example:
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
up vote
2
down vote
Issue is with below code.
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
Your structure has a character pointer for path
and parameters
What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat
or par
changes value or has some garbage value, the structure element will also have garbage value.
What you need to do is to allocate memory for each element and strcpy
the data
Additionally, as pointed out by @Ruks -
struct process *head = ( struct process *)malloc(sizeof(struct process));
head = NULL;
is wrong. You are loosing the pointer returned by malloc
. If you want to ensure that the linked list is empty initially, you should initialise all elements of head
.
head-> priority = 0;
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your linked list does not store the values at all.
The issue isn't the loop, but strtok
- you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.
So, to save the values, you must allocate memory for your strings (path
and parameters
) and use strcpy
to copy the strings to the allocated memory.
Remember to free
each item fields before freeing list items!
Example:
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
up vote
1
down vote
accepted
Your linked list does not store the values at all.
The issue isn't the loop, but strtok
- you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.
So, to save the values, you must allocate memory for your strings (path
and parameters
) and use strcpy
to copy the strings to the allocated memory.
Remember to free
each item fields before freeing list items!
Example:
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your linked list does not store the values at all.
The issue isn't the loop, but strtok
- you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.
So, to save the values, you must allocate memory for your strings (path
and parameters
) and use strcpy
to copy the strings to the allocated memory.
Remember to free
each item fields before freeing list items!
Example:
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));
Your linked list does not store the values at all.
The issue isn't the loop, but strtok
- you are using it incorrectly: it does not allocate memory to save the tokens it parses, but instead uses a single internal static buffer.
So, to save the values, you must allocate memory for your strings (path
and parameters
) and use strcpy
to copy the strings to the allocated memory.
Remember to free
each item fields before freeing list items!
Example:
char *path_token = strtok(NULL, " ");
path = malloc(strlen(path_token) + 1); /* remember to allocate space for terminator! */
strcpy(path, path_token));
answered Nov 22 at 6:38
Lev M.
44918
44918
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
This worked and Your explanation was clear! thanks
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
up vote
2
down vote
Issue is with below code.
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
Your structure has a character pointer for path
and parameters
What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat
or par
changes value or has some garbage value, the structure element will also have garbage value.
What you need to do is to allocate memory for each element and strcpy
the data
Additionally, as pointed out by @Ruks -
struct process *head = ( struct process *)malloc(sizeof(struct process));
head = NULL;
is wrong. You are loosing the pointer returned by malloc
. If you want to ensure that the linked list is empty initially, you should initialise all elements of head
.
head-> priority = 0;
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
up vote
2
down vote
Issue is with below code.
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
Your structure has a character pointer for path
and parameters
What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat
or par
changes value or has some garbage value, the structure element will also have garbage value.
What you need to do is to allocate memory for each element and strcpy
the data
Additionally, as pointed out by @Ruks -
struct process *head = ( struct process *)malloc(sizeof(struct process));
head = NULL;
is wrong. You are loosing the pointer returned by malloc
. If you want to ensure that the linked list is empty initially, you should initialise all elements of head
.
head-> priority = 0;
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
up vote
2
down vote
up vote
2
down vote
Issue is with below code.
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
Your structure has a character pointer for path
and parameters
What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat
or par
changes value or has some garbage value, the structure element will also have garbage value.
What you need to do is to allocate memory for each element and strcpy
the data
Additionally, as pointed out by @Ruks -
struct process *head = ( struct process *)malloc(sizeof(struct process));
head = NULL;
is wrong. You are loosing the pointer returned by malloc
. If you want to ensure that the linked list is empty initially, you should initialise all elements of head
.
head-> priority = 0;
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;
Issue is with below code.
new_node->priority = prio;
new_node->path = pat;
new_node->parameters = par;
Your structure has a character pointer for path
and parameters
What you are doing here is just assigning the pointer in the structure to the pointer in the function, passed. Later on when pat
or par
changes value or has some garbage value, the structure element will also have garbage value.
What you need to do is to allocate memory for each element and strcpy
the data
Additionally, as pointed out by @Ruks -
struct process *head = ( struct process *)malloc(sizeof(struct process));
head = NULL;
is wrong. You are loosing the pointer returned by malloc
. If you want to ensure that the linked list is empty initially, you should initialise all elements of head
.
head-> priority = 0;
head-> path = NULL;
head-> parameters = NULL;
head-> next = NULL;
edited Nov 22 at 6:50
answered Nov 22 at 6:35
Rishikesh Raje
5,4501826
5,4501826
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
I'm trying a solution right now with what you suggest here! I'll be back in 5!
– Mamoun Debbarh
Nov 22 at 6:41
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
This helped a lot. Thanks man!
– Mamoun Debbarh
Nov 22 at 6:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424977%2fadding-to-linked-list-struct-using-a-while-loop-doesnt-save-the-data-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
head = NULL;
... Doing this inside the algorithm you provided just after youmalloc()
-ed it... is in fact, flat out wrong!– Ruks
Nov 22 at 6:27
@Ruks I do this to make sure the linked list is empty at the start of the process. removing it doesn't change anything except that I have an additional object with null values (that I don't want).
– Mamoun Debbarh
Nov 22 at 6:30
1
No!, you should not use
head = NULL;
for that!memset(head, 0, 1);
is the way... You are trying to leak the object by assigningNULL
to it!– Ruks
Nov 22 at 6:33
However, it's wrong.
struct process *head = ( struct process *)malloc(sizeof(struct process));
allocates memory and stores it's address inhead
.head = NULL;
overrideshead
. Now, the just allocated memory is lost - it's a memory leak.– Scheff
Nov 22 at 6:33
path
andparameters
are pointers. What are they pointing at? Dopath
andparameters
from differentstruct process
point at different places? What are these places?– n.m.
Nov 22 at 6:35