C
Chad
In the following code, 'free(temp->name); ' in the function cleanup()
causes the program to crash and I don't see why either.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct new {
char *name;
char *pword;
struct new *next;
};
void add_person(struct new** );
void printme(struct new* );
void cleanup(struct new* );
char *strddup(char *s, size_t n)
{
char *p;
p = malloc(strlen(s) + 1);
if (p != NULL)
(void)strncpy(p, s, n);
return p;
}
void add_person(struct new** head_ref)
{
char name[MAX];
char pword[MAX];
struct new *person;
if ((person = malloc(sizeof(struct new))) == NULL ) {
(void)fprintf(stderr, "Invalid name\n");
exit(1);
}
(void)printf("Enter a name: ");
fflush(stdout);
if ((scanf("%s", name)) == 1) {
if ((person->name = strddup(name, MAX)) == NULL) {
(void)fprintf(stderr, "Invalid name\n");
exit(1);
}
} else {
(void)fprintf(stderr, "Invalid input\n");
exit(1);
}
/*Need OS specific functions to disable echoing*/
(void)printf("Enter a pasword: ");
fflush(stdout);
if ((scanf("%s", pword)) == 1) {
if ((person->pword = strddup(pword, MAX)) == NULL ) {
(void)fprintf(stderr, "Invalid password\n");
exit(1);
}
} else {
(void)fprintf(stderr, "Invalid input\n");
exit(1);
}
person->next = *head_ref;
*head_ref = person;
}
void printme(struct new* head)
{
struct new* current = head;
printf("\n");
while (current != NULL) {
printf("The name is: %s\n", current->name);
printf("The password is: %s\n", current->pword);
current = current->next;
}
}
void cleanup(struct new* head)
{
struct new* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp->name); /*<---the problem line of code*/
free(temp);
}
}
int main(void)
{
struct new* head= NULL;
int add = 1;
int c;
(void)printf("Do you want to add a name? ");
(void)printf("Enter either y|Y to enter one or n|N to exit: ");
fflush(stdout);
while(((c = getchar()) != EOF) && add != 0) {
switch(c)
{
case 'Y': case 'y':
add_person(&head);
break;
case 'N': case 'n':
add = 0;
break;
default:
(void)printf("Do you want to add another person? ");
fflush(stdout);
break;
}
}
printme(head);
cleanup(head);
exit(0);
}
And the partial output...
[cdalten@localhost oakland]$ gcc -Wall -Wextra -Wshadow bored.c -o
bored
[cdalten@localhost oakland]$ ./bored
Do you want to add a name? Enter either y|Y to enter one or n|N to
exit: y
Enter a name: chad
Enter a pasword: no
Do you want to add another person? n
The name is: chad
The password is: no
*** glibc detected *** ./bored: free(): invalid next size (fast):
0x0896e018 ***
======= Backtrace: =========
causes the program to crash and I don't see why either.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct new {
char *name;
char *pword;
struct new *next;
};
void add_person(struct new** );
void printme(struct new* );
void cleanup(struct new* );
char *strddup(char *s, size_t n)
{
char *p;
p = malloc(strlen(s) + 1);
if (p != NULL)
(void)strncpy(p, s, n);
return p;
}
void add_person(struct new** head_ref)
{
char name[MAX];
char pword[MAX];
struct new *person;
if ((person = malloc(sizeof(struct new))) == NULL ) {
(void)fprintf(stderr, "Invalid name\n");
exit(1);
}
(void)printf("Enter a name: ");
fflush(stdout);
if ((scanf("%s", name)) == 1) {
if ((person->name = strddup(name, MAX)) == NULL) {
(void)fprintf(stderr, "Invalid name\n");
exit(1);
}
} else {
(void)fprintf(stderr, "Invalid input\n");
exit(1);
}
/*Need OS specific functions to disable echoing*/
(void)printf("Enter a pasword: ");
fflush(stdout);
if ((scanf("%s", pword)) == 1) {
if ((person->pword = strddup(pword, MAX)) == NULL ) {
(void)fprintf(stderr, "Invalid password\n");
exit(1);
}
} else {
(void)fprintf(stderr, "Invalid input\n");
exit(1);
}
person->next = *head_ref;
*head_ref = person;
}
void printme(struct new* head)
{
struct new* current = head;
printf("\n");
while (current != NULL) {
printf("The name is: %s\n", current->name);
printf("The password is: %s\n", current->pword);
current = current->next;
}
}
void cleanup(struct new* head)
{
struct new* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp->name); /*<---the problem line of code*/
free(temp);
}
}
int main(void)
{
struct new* head= NULL;
int add = 1;
int c;
(void)printf("Do you want to add a name? ");
(void)printf("Enter either y|Y to enter one or n|N to exit: ");
fflush(stdout);
while(((c = getchar()) != EOF) && add != 0) {
switch(c)
{
case 'Y': case 'y':
add_person(&head);
break;
case 'N': case 'n':
add = 0;
break;
default:
(void)printf("Do you want to add another person? ");
fflush(stdout);
break;
}
}
printme(head);
cleanup(head);
exit(0);
}
And the partial output...
[cdalten@localhost oakland]$ gcc -Wall -Wextra -Wshadow bored.c -o
bored
[cdalten@localhost oakland]$ ./bored
Do you want to add a name? Enter either y|Y to enter one or n|N to
exit: y
Enter a name: chad
Enter a pasword: no
Do you want to add another person? n
The name is: chad
The password is: no
*** glibc detected *** ./bored: free(): invalid next size (fast):
0x0896e018 ***
======= Backtrace: =========