I make a person in a person struct with typedef person_t:
int main(int argc, char* argv[]) {
person_t a;
memset(&a, 0, sizeof(person_t));
person_set_name(&a, "Konrad Hoppenstauffer");
person_set_age(&a, 42);
void person_set_name(person_t* person, char* name) {
if(person->name) {
free(person->name);
}
person->name = malloc(sizeof(char) * strlen(name) + 1);
strcpy(person->name, name);
}
The above works just fine.
Problem happens when I use this function:
person_t* string_to_person(char* str) {
person_t* person = malloc(sizeof(person_t));
int len = 0;
while(str[len] != '\t') {
len++;
}
char* name = malloc(len + 1);
int i;
for(i = 0; i < len; i++) {
name[i] = str[i];
}
name[len] = '\0';
person_set_name(person, name);
person_set_age(person, atoi(str+len+1));
return person;
}
Here str is something like: "Name Nameson\t22". That is name seperated by tab. And then I separate the two and put characters in char* name.
person_t is a typedef for a struct.
If I remove the free(person->name) from person_set_name, everything works fine. But if I leave it in, name becomes garbage, for example: "É8>".
I assume that something wrong happens in the for loop where I copy each character. But with my limited experience with C I can't see what. Help is appreciated.
You're trying to free a garbage pointer.
After:
person_t* person = malloc(sizeof(person_t));
malloc doesn't initialize the new memory block with any particular data, so your program must treat *person as containing garbage at this point (since it could contain any data). In particular, person->name (i.e. (*person).name) might not be NULL.
A short time later, this code runs:
if(person->name) {
free(person->name);
}
- if person->name was not NULL, then you free it. Since person->name doesn't point to something you allocated with malloc, at this point you're well and truly in Undefined Behaviour Land™.
One possible fix is to set person->name = NULL; immediately after allocating the person.
Related
I'm able to assign values with strdup and print the values with this: (*test + index)->email but I have no idea how to free the memory allocated to the variable email. I thought about freeing test+index but I guess this would cause a memory leak, right? Taking into account that the struct has allocated memory and each of the pointer inside it have memory allocated with strdup.
Edit:
The code is roughly like this:
struct random {
char *email;
} Random;
void function(Random **struct) {
char *temp = calloc(100, sizeof(char));
*struct = calloc(5, sizeof(Random));
for (int i = 0; i < 5; i++) {
scanf("%s", temp);
(*struct + i)->email = strdup(temp); //This works
}
free((*struct + 3)->email); //Gives segmentation fault
}
int main() {
Random *struct;
function(&struct)
}
The posted code does not compile:
you cannot use struct as the name of a variable. struct is a keyword.
Random is a global variable, not a type.
It is idiomatic and much simpler in C to return the result instead of passing its address as an argument.
Following these remarks, and adding basic checks, the code should be simplified as:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Random {
char *email;
} Random;
Random *function(void) {
char *temp = calloc(100, sizeof(char));
if (temp == NULL)
return NULL;
Random *s = calloc(5, sizeof(Random));
if (s != NULL) {
for (int i = 0; i < 5; i++) {
if (scanf("%99s", temp) != 1)
*temp = '\0';
(s + i)->email = strdup(temp); //This works
}
free((s + 3)->email); //Gives segmentation fault
}
free(temp);
return s;
}
int main() {
Random *s = function();
// ...
}
This code, semantically equivalent to your posted fragment, does not have undefined behavior where you indicate, your actual code must be doing something else.
If I'm not mistaken, wouldn't it this?
free(*(test+index)->email);
free(text+index);
please read/understand: precedence of C operators
Then note that the de-reference operator * has a lower precedence than the + operator,
Therefore, The posted code needs to be modified to use something like:
(*mystruct)+i = ...
etc. Otherwise, the + will be executed before the *
I have a struct called Person, that contains two attributes - first and last name.
After successfully dynamic allocation of memory for a variable of Person type, giving values to the attributes I would like to free the memory, but I keep getting a runtime error (the program window just crashes)
this it the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char firstName[15];
char lastName[15];
} Person;
void main(){
int len = 0;
char firstName[]="danny", lastName[]="johnes";
Person *temp = (Person*)malloc(sizeof(Person));
if (temp == NULL)
return;
len = strlen(firstName);
temp->firstName[len] = (char*)malloc(sizeof(char)*(len));
if (temp->firstName == NULL)
return;
strcpy(temp->firstName, firstName);
len = strlen(lastName);
temp->lastName[len] = (char*)malloc(sizeof(char)*(len));
if (temp->firstName == NULL)
return;
strcpy(temp->lastName, lastName);
freePerson(temp);
system("pause");
return;
}
This is the function I use to free the memory:
void freePerson(Person* ps) {
if (ps != NULL) {
free(ps->firstName);
free(ps->lastName);
free(ps);
}
}
All I want the code to do - is to store the name in a dynamically allocated structure, and free it.
Later on, I plan to replace the hard-coded names with values inputed from file.
Any ideas about the error? Thank you.
You have already space allocated for firstName, so you have to copy the name within the size constraits (15 bytes). You can do this best with snprintf like this:
snprintf(temp->firstName, sizeof(temp->firstName), "%s", firstName);
Same goes for lastName. Mind that both might be truncated if the length exceeds the size of the field.
The other option is to allocate the fields dynamically. Then your struct members should be pointers, not char arrays:
typedef struct {
char *firstName;
char *lastName;
} Person;
You can then allocate and assign the names like this:
temp->firstName = strdup(firstName); // (same for lastName)
But mind that you have to free these fields seperately if you want to free the whole item.
If you don't want to specify a maximum size for the names in the structure, you need to declare them as pointers, not arrays.
typedef struct {
char *firstName;
char *lastName;
} Person;
Then you should assign the result of malloc() to the member, without indexing it. You also need to add 1 to strlen(firstName), to make space for the null terminator.
temp->firstName = malloc(strlen(firstName)+1);
if (temp->firstName == NULL) {
return;
}
strcpy(temp->firstName, firstName);
This is how I would write this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FIRSTNAME_MAXLEN 15
#define LASTNAME_MAXLEN 15
typedef struct
{
char firstName[FIRSTNAME_MAXLEN+1];
char lastName[LASTNAME_MAXLEN+1];
} person_t;
void freePerson(person_t *ps) {
if (ps) {
free(ps); ps=NULL;
}
}
int main(){
const char *firstName="danny";
const char *lastName="johnes";
person_t *temp = calloc(1, sizeof(person_t));
if (!temp) return 1;
strncpy(temp->firstName, firstName, FIRSTNAME_MAXLEN);
strncpy(temp->lastName, lastName, LASTNAME_MAXLEN);
printf("test: firstname: %s\n", temp->firstName);
printf("test: lastname: %s\n", temp->lastName);
freePerson(temp);
return 0;
}
You allocate enough room on the heap and cleanup things with calloc(), then you copy your string with strncpy() limiting to the bytes reserved and avoiding buffer overflow. At the end you need to free() the memory returned by calloc().
Since you allocated char firstName[] and char lastName[] inside your struct you don't need to reserve other memory with malloc() for those members, and you also don't need to free() them.
At least 5 issues:
To duplicate a string, insure allocation includes enough room for the characters including the null character.
Otherwise the strcpy() writes outside the allocation which is undefined behavior (UB).
len = strlen(firstName);
// temp->firstName[len] = (char*)malloc(sizeof(char)*(len ));
temp->firstName = (char*)malloc(sizeof(char)*(len + 1));
// + 1
...
strcpy(temp->firstName, firstName);
Same for lastName.
Also assign to the pointer, not the char. #Barmar
Person members are arrays. For dynamic allocation, they should be pointers. #NthDeveloper
typedef struct {
// char firstName[15];
// char lastName[15];
char *firstName;
char *lastName;
} Person;
2nd test is wrong
// if (temp->firstName == NULL)
if (temp->lastName == NULL)
int vs. size_t.
int len = 0; assumes the string length fits in a int. Although this is exceedingly common, the type returned from strlen() is size_t. That unsigned type is right-sized for array indexing and sizing - not too wide, not too narrow. Not a key issue in this learner code.
// int len = 0;
size_t len = 0;
Tip: cast not needed. Allocate to the referenced object, not the type. Easier to code right, review and maintain.
// Person *temp = (Person*)malloc(sizeof(Person));
Person *temp = malloc(sizeof *temp);
// temp->firstName[len] = (char*)malloc(sizeof(char)*(len + 1));
temp->firstName = malloc(sizeof *(temp->firstName) * (len + 1));
Tip: Although not C standard, many platforms provide strdup() to allocated and copy strings. Sample strdup() code.
temp->firstName = strdup(firstName);
Tip: Likely the most valuable one: A good compiler with warnings well enabled should have warned about temp->firstName[len] = (char*)malloc(sizeof(char)*(len)); as it is a questionable type mis-match in the assignment. These warnings save you and us all time. Insure your next compilation has all warning enabled.
I have a structure which includes a pointer to a pointer as one of its members. I keep getting a segfault when trying to dereference this pointer.
Create a person in person_init and give it a name (John). Name is a pointer to a character string. I can printf() no problem in this function. Returning to the main() function, again I can printf() the name no problem. But then when
I enter a new function and try to printf() I get a segfault. I'm really confused because I'm pretty sure name is being allocated on the heap.
What am I missing here?
code:
#include <stdio.h>
#include <stdlib.h>
/* structure with a pointer to pointer member */
struct person {
char **name;
};
/* allocate space for the strucutre */
int person_init(struct person **p)
{
struct person *newp = malloc(sizeof(struct person));
/* give a name, allocated on the heap */
char *name = malloc(sizeof(char) * 5);
*name = 'J';
*(name + 1) = 'o';
*(name + 2) = 'h';
*(name + 3) = 'n';
*(name + 4) = '\0';
newp->name = &name;
*p = newp;
printf("Name in init: %s\n", *(*p)->name); /* this works */
return 0;
}
void print_name(struct person *p)
{
printf(*p->name);
}
int main()
{
struct person *person;
person_init(&person);
printf("Name in main: %s\n", *person->name); /* works */
print_name(person); /* segfault */
}
Here's the problem:
newp->name = &name;
newp->name now points to name, which is a local variable in person_init. As soon as person_init returns, name is gone and newp->name is an invalid pointer. Any attempt to use it afterwards results in undefined behavior.
Fix:
struct person {
char *name;
};
And initialize it as
newp->name = name;
Now newp->name is a copy of name, i.e. it points to the allocated string.
I'm new to C and having trouble wrapping my head around double pointers and keep getting segmentation fault errors. I've debugged the program a bit and located where things go wrong, but can't for the life of me figure out why. I'll post my code first:
int main() {
printf("Enter string to be split: \n");
a = readline();
String *st = newString(a);
String **split;
int num;
num = string_split(st, ',', split);
for (i=0; i<num; i++) { print_string(*(split+i)); }
}
readline() produces a pointer to an array of chars (entered by the user) and appends '\0' to it. newString and print_string definitely work. Here's the struct for string:
typedef struct {
char *chars;
int length;
int maxSize;
} String;
And here is the code for string_split which is causing me all this trouble.
int string_split(String *s, char delim, String **arrayOfStructs) {
char *c = getCharacters(s);
int len = length(s);
int begin = 0;
int end;
int arraycount = 0;
String **temp = (String**)malloc(sizeof(String*));
for (end=0; end<len+1; end++) {
if ((*(c+end) == delim || *(c+end) == '\0') && begin != end) {
String *st = substring(s,begin,end-1);
*(temp + arraycount) = st;
begin = end + 1;
arraycount++;
temp = (String**)realloc(temp, 1+arraycount*sizeof(String*));
}
}
arrayOfStructs = temp;
return arraycount;
}
In main, when I get back split, all the String*'s that it points too are gone. When print_string gets an individual String* and tries to grab one of its members, a segmentation fault occurs. I don't understand why, because I feel like I allocate memory every time it is necessary, but I feel like I'm missing something. Also, when debugging, if I step through string_split, temp is produced exactly like I expect, so I think I'm just not malloc'ing somewhere where I'm supposed to and it's not a problem with the logic of the function. Here is the code in substring, although I'm pretty sure it works since I've been able to return String* from substring and pass them to print_string just fine.
String *substring(String *s1, int begin, int end) {
String *s = (String*)malloc(sizeof(String));
int length = 0;
s->maxSize = 20;
char *temp = (char*)malloc(20*sizeof(char));
char *arr = s1->chars;
int i;
for (i=begin; i <= end; i++) {
*(temp+length) = *(arr+i);
length++;
if (length == s1->maxSize-1) {
s1->maxSize = s1->maxSize+20;
temp = (char*)realloc(temp, s1->maxSize*sizeof(char));
}
}
*(temp+length) = '\0';
s->length = length;
s->chars = temp;
return s;
}
Any help is greatly appreciated!
You need to pass the argument arrayOfStructs by reference and not by value. As C doesn't actually have proper references, you have to pass a pointer to the variable:
int string_split(String *s, char delim, String ***arrayOfStructs) {
...
*arrayOfStructs = temp;
return arraycount;
}
Call it using the address-of operator &:
num = string_split(st, ',', &split);
As it is now, you pass the argument by value, which means that the variable arrayOfStructs is just a local copy inside the function. Any changes to it is only made to the copy, and are lost once the variable goes out of scope when the function returns.
String **temp = (String**)malloc(sizeof(String*));
*(temp + arraycount) = st;
temp+arraycount is going to give you a random address in memory. temp contains the pointer you just malloced, which should point to another pointer.(which you have not initialised), but you are incrementing the pointer so you loose the location you just malloced.
temp is not pointing to consecutive memory, it specifically points to another pointer(which is 8bytes on a 64bit machine)
I am new to c programming and I am stuck with this one its a typedef struct and what I would like to do is that I want to create an array from the double pointer from this structure
typedef struct
{
char* firstname;
float price;
}Name,*pName,**ppName;
typedef struct
{
ppName Names;
unsigned int numPerson;
}Book;
And my main which always give me segmentation fault dont mind the loop it is looping until the use says to quit.
int main(void)
{
Book D;
setUpCollection(&D);
while(..)
{
scanf(...);
switch(...)
{
case 1:
if(!AddNewPerson(&D))
return 1;
break;
case 2:
....
case 3:
....
default:
printf("Please enter a valid choice");
}
}
return 0;
}
void setUpCollection(Book* data){
Name name;
pName pname;
pname= malloc(MAX_PERSON* sizeof(pName));
pname= &name;
data->Names= &pname;
data->numPerson= 0;
}
BOOL AddNewPerson(Book* data){
char *title = malloc(sizeof(char));
int len;
Name name;
pName pname;
scanf(...);
len = strlen(firstname);
name.firstname = malloc(len * sizeof(char*));
name.firstname = firstname;
pname= malloc(1);
pname= &name;
data->DVDs[data->numPerson++] = pname;
printf("%0.2f", data->Names[(data->numPerson)-1]->price);
return TRUE;
}
My main problem is that I cant print all the added names and also getting segmentation fault.
There are quite a few errors in your program but let me mention a few:
Doesn't this seem odd to you:
pname= malloc(MAX_PERSON* sizeof(pName));
pname= &name;
you are creating a memory leak by first letting pname point to the array of pName then assigning to &name.
What is this:
char *title = malloc(sizeof(char)); // ?
here you allocate too less space
name.firstname = malloc(len * sizeof(char*));
it should be
name.firstname = malloc(len * sizeof(char) + 1);
or more readable:
name.firstname = malloc(len+1);
this makes no sense again:
pname= malloc(1);
pname= &name;
again you created a memory leak by first letting pname point to a heap block of 1 byte then assigning it to a local variable which you include in data - the local variable is freed up once you leave AddNewPerson() so data will point to garbage.
Instead do something like this (I am no fan of having
typedefs for pointers), also try avoiding naming types
the same way you name variables for clarity:
typedef struct
{
char *firstname;
float price;
} Name;
typedef struct
{
Name** names;
unsigned int numPerson;
} Book;
Now allocate the initial size of your array, the whole point
of having it on the heap is that the array can grow if more
records are added than MAX_PERSONS so you need to keep track
of the number of used records in the array as well as the number
of records allocated
int allocated = MAX_PERSONS;
Book D;
D.names = malloc( allocated * sizeof(Name*) );
D.numPerson = 0;
then loop over user input and add records keeping
track of how many records have been read. Since names
is an array of pointers, you need to allocate a Name
struct each time you add an entry
e.g.
D.names[i] = malloc( sizeof(Name) );
D.names[i]->firstname = strdup(userInputName);
D.names[i]->price = userInputPrice;
then at each iteration check if there is allocated memory left
++i;
if ( i == allocated )
{
// if yes you need to get more memory, use realloc for that
// get e.g. 10 more records
Name* tmp = realloc( D.names, (allocated + 10)*sizeof(Name) );
if ( tmp != NULL )
{
D.names = tmp;
allocated += 10;
}
else
{ .. some error msg .. }
}