Segfault when dereferencing pointer to pointer within struct - c

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.

Related

Accessing char attribute of a struct

in my code I have a function that can initialize new instance of a struct, which has a char attribute, and the same function returns a pointer to an instance.
But when I try to read the char attribute from different scopes, I get also different characters.
Below you can find my code and also the results.
Thanks in advance!
Main function
Here the character is printed correctly.
int main(int argc, char const *argv[]) {
char line[] = "2-11 f: fjdfffmffffrff";
char *pnt = line;
Entry *entry = getEntry(&pnt);
printf("(main) min %d\n", entry->min);
printf("(main) max %d\n", entry->max);
printf("(main) character %c\n", entry->character); <-----
printf("(main) password %s\n", entry->password);
printEntry(entry);
return 0;
}
Function
While here the printed character is some random char.
void printEntry(Entry * entry){
printf("(function) min %d\n", entry->min);
printf("(function) max %d\n", entry->max);
printf("(function) character %c\n", entry->character); <--
printf("(function) password %s\n", entry->password);
}
STDOUT
Here are the results.
(main) min 2
(main) max 11
(main) character f
(main) password fjdfffmffffrff
(function) min 2
(function) max 11
(function) character L
(function) password fjdfffmffffrff
Edit
Entry struct
typedef struct Entry {
int min, max;
char character;
char *password;
} Entry;
char **separateBySpace(char **stringPtr) {
char **ptrArray = (char **) malloc(ARR_PTR_LEN * sizeof(char *));
char delim[] = " ";
char *ptr = strtok(*stringPtr, delim);
ptrArray[0] = ptr;
int x = 1;
while (ptr != NULL) {
if (x >= ARR_PTR_LEN) {
break;
}
ptr = strtok(NULL, delim);
ptrArray[x] = ptr;
x++;
}
return ptrArray;
}
Entry *getEntry(char **stringPtr) {
char **pntArray = separateBySpace(stringPtr);
char *rules = pntArray[0];
char *character = pntArray[1];
char *password = pntArray[2];
int *array = getRange(rules);
Entry entry = {.min = *(array), .max= *(array + 1), .character = *(character), .password= password};
Entry *pntEntry = malloc(sizeof(struct Entry));
pntEntry = &entry;
return pntEntry;
}
Function getEntry returns the address of a non-static local variable. Using this address in main is undefined behavior. Probably the call to printEntry partially overwrites the data, that's why you see different output.
You try to dynamically allocate memory for the returned data with
Entry *pntEntry = malloc(sizeof(struct Entry));
but you throw away the address to this memory and assign the address of your local variable with
pntEntry = &entry;
You probably want to copy the structure instead of the pointer. This would be
*pntEntry = entry;
Not related to your problem:
Your program should free all allocated memory when it is no longer used.
With the code shown in the question it is not necessary to pass the address of the pointer to the input string to getEntry and separateBySpace as a type char** because you don't want to modify the pointer. Passing a char* would be sufficient.
In separateBySpace you return an array of pointers that point to characters of the input string which gets modified by strtok. Later in getEntry you assign the pointer password to a pointer in your Entry structure. This means you should not change the string variable that was passed as an argument to getEntry, otherwise the password´ string referenced in the returned Entry` structure will change.
Edit (to answer a comment):
I think in getEntry you can free the pointer array allocated in separateBySpace because all values have been used or copied to the Entry structure.
At the end of the program you should free the memory pointed to by entry that was allocated in getEntry.
You must not free the memory password because this points to a character in your local variable line. Freeing entry->password before freeing entry would be necessary if you would allocate memory for a copy of the password, for example using strdup. This would also fix the possible problem that entry->password points to an element of line.

Allocating memory to a struct member pointer in C

I have a structure with a member that I need to pass to a function by reference. In that function, I'd like to allocate memory & assign a value. I'm having issues somewhere along the line - it seems that after the code returns from allocateMemory, the memory that I had allocated & the values that I assigned go out of scope (this may not be exactly what is happening, but it appears to be the case).
#include <stdio.h>
#include <stdlib.h>
typedef struct myStruct_t
{
char *myString;
} myStruct;
void allocateMemory(void *str);
int main(void) {
myStruct tmp = {
.myString = NULL
};
myStruct *p = &tmp;
allocateMemory(p->myString);
//allocateMemory(&(p->myString)); //also tried this
printf("%s", p->myString);
return 0;
}
void allocateMemory(void *str)
{
str = malloc(8);
((char *)str)[0] = 'a';
((char *)str)[1] = 0;
}
If I print the value of str inside of allocateMemory, the 'a' is successfully printed, but if I attempt to print p->myString in main, my string is empty.
Can anyone tell me what I'm doing wrong?
You need to pass address of the structure member and then you can change (aka allocate memory) to it. In your version of the function, you are not taking a pointer not reference of a pointer, so you can change the content of memory referenced by the pointer but not the pointer itself.
So change your function to
void allocateMemory(char **ret_str)
{
char *str = malloc(8);
str[0] = 'a';
str[1] = 0;
*ret_str = str;
}
And then call it as
allocateMemory(&p->myString)
An alternative way of writing the same function Rohan did, eliminating the need to define any new variables:
void allocateMemory(char **str, size_t size) {
*str = malloc(size);
(*str)[0] = 'a';
(*str)[1] = '\0';
}
Note that I pass a size parameter to justify using malloc() in the first place.

String turns to garbage after using free()

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.

double pointer typedef struct array

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 .. }
}

C -- Structs and Pointers Basic Questions

So I'm trying to learn C right now, and I have some basic struct questions I'd like to clear up:
Basically, everything centers around this snippet of code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 127
typedef struct {
char name[MAX_NAME_LEN + 1];
unsigned long sid;
} Student;
/* return the name of student s */
const char* getName (const Student* s) { // the parameter 's' is a pointer to a Student struct
return s->name; // returns the 'name' member of a Student struct
}
/* set the name of student s
If name is too long, cut off characters after the maximum number of characters allowed.
*/
void setName(Student* s, const char* name) { // 's' is a pointer to a Student struct | 'name' is a pointer to the first element of a char array (repres. a string)
char temp;
int i;
for (i = 0, temp = &name; temp != '\0'; temp++, i++) {
*((s->name) + i) = temp;
}
/* return the SID of student s */
unsigned long getStudentID(const Student* s) { // 's' is a pointer to a Student struct
return s->sid;
}
/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) { // 's' is a pointer to a Student struct | 'sid' is a 'long' representing the desired SID
s->sid = sid;
}
I've commented up the code in an attempt to solidify my understanding of pointers; I hope they're all accurate.
Also, I have another method,
Student* makeAndrew(void) {
Student s;
setName(&s, "Andrew");
setStudentID(&s, 12345678);
return &s;
}
which I'm sure is wrong in some way... I also think my setName is implemented incorrectly.
Any pointers? (no pun intended)
This is very wrong. If you insist on not using strcpy do something like this (not tested)
int iStringLength = strlen(name);
for (i = 0; i < iStringLength; i++) {
s->name[i] = name[i];
}
but make sure that the length is not longer than your array size.
This is also wrong
Student* makeAndrew(void) {
Student s;
setName(&s, "Andrew");
setStudentID(&s, 12345678);
return &s;
}
because the s object is destroyed when the function exits - it is local to the function scope and yet you return a pointer to it. So if you try to access the struct using this pointer it will not be valid as the instance no longer exists. If you want to do this you should dynamically allocate it using malloc . Alternatively do not return a pointer at all and use the alternative option of #Andrew .
In your "another method" you are locally declaring Student s, which will dynamically allocate space (usually on the stack) and you are returning that address on completion.
However, that stack-space will be released on the return, so there is no guarantee that the data is uncorrupted - in fact the likelyhood is that it will be!
Declare Student s in the call to your method, and pass the pointer to makeAndrew:
void makeAndrew(Student *s) {
setName( s, "Andrew");
setStudentID( s, 12345678);
}
...
Student s;
makeAndrew( &s );
...
Your function makeAndrew returns pointer to a local variable. It is only valid before the scope ends, so as soon as the function finishes, it will change when the memory gets overwritten - i. e. almost instantly. You would have to allocate it dynamically (using Student *s = new Student;, or if you really want to stick to pure C, Student *s = malloc (sizeof Student );, and then free it outside the function after it is not needed to avoid memory leak.
Or do it as Andrew suggested, it's less error-prone.
I would change the makeAndrew() function to just return a struct, not a pointer to a struct to correct the error with respect to returning a pointer to a temporary variable:
Student makeAndrew(void)
{
Student s;
setName(&s, "Andrew");
setStudentID(&s, 12345678);
return s;
}
Student aStudent = makeAndrew();
Your setName does have an error with respect to temp, which should be a char *, since you are incrementing it in your loop to point to another character in the input c-string. I think it was missing the null termination as well. And as you mention in your comment, there should be a check for overflow of the name char array in Student:
void setName(Student* s, const char* name) { // 's' is a pointer to a Student struct |
// 'name' is a pointer to the first element of a char array (repres. a string)
const char *temp;
int i;
for (i = 0, temp = name; *temp != '\0' && i <= MAX_NAME_LEN; temp++, i++)
{
*((s->name) + i) = *temp;
}
s->name[i] = '\0';
}
You can use strncpy to simplify setName:
void setName2(Student *s,const char *name)
{
#include <string.h>
strncpy(s->name, name,MAX_NAME_LEN);
s->name[MAX_NAME_LEN] = '\0';
}

Resources