Reputation: 11
I keep getting a segmentation fault and i know its from the char pointer. But I cant figure out why?
Whiskey* createWhiskey(int a, double p, char* n){
Whiskey* whiskey = malloc(sizeof(Whiskey));
whiskey->age = a;
whiskey->proof = p;
whiskey->name = malloc((strlen(n)+1) * sizeof(char));
strcpy(whiskey->name, n);
return whiskey;
}
int main(){
Whiskey* burbon;
burbon = createWhiskey(12, 90.0, "MakersMark");
free(burbon);
return 0;
}
In a comment from Alex (see below) the following information is added:
typedef struct{ int age; double proof; char* name; }Whiskey;
Upvotes: 0
Views: 152
Reputation: 16540
the following code
free()
the code:
#include <string.h> // malloc()
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <stdio.h> // perror()
typedef struct
{
int age;
double proof;
char* name;
} Whiskey;
Whiskey* createWhiskey(int age, double proof, char* name)
{
Whiskey* whiskey = NULL;
if( NULL == (whiskey = malloc(sizeof(Whiskey)) ) )
{ // then, malloc failed
perror( "malloc for Whiskey failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
whiskey->age = age;
whiskey->proof = proof;
whiskey->name = NULL;
if( NULL == (whiskey->name = malloc( strlen(name)+1) ) )
{ // then malloc failed
perror( "malloc for name field failed" );
free( whiskey );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
strcpy(whiskey->name, name);
return whiskey;
} // end function: createWhiskey
int main( void )
{
Whiskey* burbon;
burbon = createWhiskey(12, 90.0, "MakersMark");
free( burbon->name );
free( burbon );
return 0;
} // end function: main
Upvotes: 0
Reputation: 998
I hope definition of your Whiskey structure is fine. Following code works fine for me:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Whisk {
int age;
double proof;
char *name;
} Whiskey;
Whiskey* createWhiskey(int a, double p, char* n){
Whiskey* whiskey = malloc(sizeof(Whiskey));
whiskey->age = a;
whiskey->proof = p;
whiskey->name = malloc((strlen(n)+1) * sizeof(char));
strcpy(whiskey->name, n);
return whiskey;
}
int main(){
Whiskey* burbon;
burbon = createWhiskey(12, 90.0, "MakersMark");
if (!burbon)
{
printf("Some error... \n");
}
// code....
if (burbon)
{
free( burbon->name);
free(burbon);
}
return 0;
}
Upvotes: 1
Reputation: 44274
As discussed in comments the program shown is fine.
However, you should add some checks to avoid problems. Something like:
typedef struct{ int age; double proof; char* name; } Whiskey;
Whiskey* createWhiskey(int a, double p, char* n){
Whiskey* whiskey = malloc(sizeof(Whiskey));
if (whiskey)
{
whiskey->age = a;
whiskey->proof = p;
if (strlen(n) > SOME_MAXIMUM)
{
free(whiskey);
printf("Some error... maybe\n");
return NULL;
}
whiskey->name = malloc((strlen(n)+1) * sizeof(char));
if (whiskey->name)
{
strcpy(whiskey->name, n);
}
else
{
free(whiskey);
printf("Some error... \n");
return NULL;
}
}
return whiskey;
}
int main(){
Whiskey* burbon;
burbon = createWhiskey(12, 90.0, "MakersMark");
if (!burbon)
{
printf("Some error... \n");
}
// code....
if (burbon)
{
free( burbon->name);
free(burbon);
}
return 0;
}
Upvotes: 1