d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Seg Fault During Memory Allocation C
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 5 2013 09:36am
Not sure why it's seg faulting, but I'm guessing it has to do with my memory allocation?.

Code


struct{
char *name;
float shoe_size;
int *age;
}Student;

int main() {
 Student *this_student;

 this_student= new_student("Cory Smalls",  3.0, 30);

 print_student(*this_student);

 return 0;
}

/* Creating a new student and allocating memory for it */

Student *new_student(char *who, float foot_magnitude, int years) {
 Student *new_student = (Student*)malloc(sizeof(Student));

 int* this_age = &years;

 if(new_student == NULL){
   return NULL;
 }

 new_student->name = malloc((strlen(who) + 1) * sizeof(char));
 if(new_student->name == NULL){
   return NULL;
 }
new_student->age = malloc(sizeof(int));
 if(new_student->age == NULL){
   return NULL;
 }

 strcpy(who, new_student->name);
 new_student->shoe_size = foot_magnitude;
 new_student->age = this_age;

 return new_student;
}

/* My print method */

void print_student(Student student) {
 printf("Name: %s\n", student.name);
 printf("Shoe size: %f\n", student.shoe_size);
 printf("Age: %d\n", *student.age);
}


This post was edited by lopelurag on Mar 5 2013 09:57am
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Mar 5 2013 10:07am
strcpy(who, new_student->name);

strcpy takes destination as it's first value and it doesn't limit the input buffer (thousands of buffer overflows are/were caused by this). You should use strncpy. Anyway, you're attempting to write to a string literal, which is an undefined behaviour.
Quote (N1570 @ 6.4.5.7)
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.


Besides that:
1) why are you taking char* instead of char const* as the name parameter? If only you had the correct function prototype, the compiler would warn you about cast from char const* to void*!
2) why are you using the same name for a function and a variable inside of the function? This never ends well :|
3) what is the definition of struct Student? I don't see why age is a pointer.
4) if name malloc fails, you're left with a memory leak. If age malloc fails, you're left with two.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 5 2013 10:14am
Quote (KrzaQ2 @ Mar 5 2013 12:07pm)
strcpy(who, new_student->name);

strcpy takes destination as it's first value and it doesn't limit the input buffer (thousands of buffer overflows are/were caused by this). You should use strncpy. Anyway, you're attempting to write to a string literal, which is an undefined behaviour.


Besides that:
1) why are you taking char* instead of char const* as the name parameter? If only you had the correct function prototype, the compiler would warn you about cast from char const* to void*!
2) why are you using the same name for a function and a variable inside of the function? This never ends well :|
3) what is the definition of struct Student? I don't see why age is a pointer.
4) if name malloc fails, you're left with a memory leak. If age malloc fails, you're left with two.


1) The parameters were predetermined, by our text.
2) Obviously this is bad form, but easily fixable thanks for pointing it out.
3) Added the student struct definition, noticed I missed that
4) Yeah messed up my if statements
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Mar 5 2013 10:17am
1) I feel for you, man :(

Well, if you fix the strcpy call, it should work.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 5 2013 10:21am
Quote (KrzaQ2 @ Mar 5 2013 12:17pm)
1) I feel for you, man :(

Well, if you fix the strcpy call, it should work.


Yeah that actually fixed the seg fault, I feel retarded for not looking up the order of srtcpy, jesus.

Ty for the help
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll