Simply creating a data structure, how can it segfault? - c

i have a simple program on data structure but it segfaults and it frustrates me so much because i have absolutely no idea why. can anybody point out anything about this code (even not relevent)? thanks
#include <unistd.h>
#include <stdlib.h>
typedef struct prof
{
struct prof *next;
char c;
int x;
int y;
} profile;
profile *create_profile(char c, int i, int j)
{
profile *new_elem;
new_elem = (profile*)malloc(sizeof(profile));
if (new_elem == NULL)
return (NULL);
new_elem->next = NULL;
new_elem->c = c;
new_elem->x = i;
new_elem->y = j;
return (new_elem);
}
int main()
{
profile **king = NULL;
*king = create_profile('K', 1, 1);
return 0;
}

Your king is a pointer to a pointer to struct.
You need some place to store the pointer to struct in, but that is what you do not allocate.
You can introduce a pointer in order fix this.
int main()
{
/* introduce and NULL-init a pointer to struct */
profile* prince = NULL;
/* actually the init to NULL is not necessary,
because prince gets initialised later indirectly via king
(credits to alk), but it does not hurt and initialising everything
is a good habit. */
/* Introduce a pointer to pointer to struct,
initialised with the address of above pointer to struct,
the address of the above variable "prince" to be precise and clear.
The space for that automatic local variable is not dynamic,
it does not require a malloc. */
profile **king = &prince;
/* what the king is pointing to, i.e. the prince,
gets assigned what the function returns,
which is a cleanly allocated pointer to a new struct. */
*king = create_profile('K', 1, 1);
/* if king were still NULL, above statement would try to write
(the cleanly allocated pointer) into memory by dereferencing NULL ...
segfault!
(Well not necessarily, as alk points out, credits.
But dereferencing NULL is undefibed behaviour,
an almost guaranteed way to trouble in the long run
and in case a seggault is observed, a likely explanation.
*/
return 0;
}

This
profile **king = NULL;
*king = ...
dereferences NULL, which by itself already invokes undefined behaviour. On top of this the code then tries to write there. Not good, as NULL does not point to valid memory.

You have to allocate memory first to profile **king and then allocate memory to *king.
#include <unistd.h>
#include <stdlib.h>
typedef struct prof
{
struct prof *next;
char c;
int x;
int y;
} profile;
profile *create_profile(char c, int i, int j)
{
profile *new_elem;
new_elem = (profile*)malloc(sizeof(profile));
if (new_elem == NULL)
return (NULL);
new_elem->next = NULL;
new_elem->c = c;
new_elem->x = i;
new_elem->y = j;
return (new_elem);
}
int main()
{
profile **king = (profile**) malloc(sizeof(profile*)); // <-- see here
*king = create_profile('K', 1, 1);
return 0;
}

The code segfaults at
*king = create_profile('K', 1, 1);
Because you've defined king to
profile **king = NULL;
Meaning that king is now a pointer to a pointer. But king doesn't actually point anywhere, you say that it points to NULL. Dereferencing a pointer pointing to NULL will cause a segfault.
If you instead define king as
profile * king = NULL;
And assign it like
king = create_profile('K', 1, 1);
The code should no longer segfault.

You are confused in using double pointer. Try using like this
#include <unistd.h>
#include <stdlib.h>
typedef struct prof
{
struct prof *next;
char c;
int x;
int y;
} profile;
profile *create_profile(profile *head,char c, int i, int j)
{
profile *new_elem;
new_elem = (profile*)malloc(sizeof(profile));
if (new_elem == NULL)
return (NULL);
new_elem->next = head;
new_elem->c = c;
new_elem->x = i;
new_elem->y = j;
return (new_elem);
}
int main()
{
profile *king = NULL;
king = create_profile(king,'K', 1, 1);
return 0;
}

Related

Creating, returning, and casting a struct with a char pointer in C

I'm pretty bad at remembering C rules with structs. Basically, I have a struct like this:
typedef struct {
char* ptr;
int size;
} Xalloc_struct;
Where the char* ptr will only be one character max.
In my program, I have to allocate and free memory to a fake disk (declared globally as char disk[100];) using my own functions:
char disk[100];
void disk_init() {
for(int i = 0; i < 100; ++i) {
disk[i] = memory[i] = 0;
}
}
struct Xalloc_struct* Xalloc(int size) {
// error checking
// ...
// run an algorithm to get a char* ptr back to a part of the global disk
// array, where index i is the index where content at disk[i] starts
char* ptr = &disk[i];
struct Xalloc_struct *ret = malloc(sizeof(struct Xalloc_struct));
ret->size = size;
ret->ptr = malloc(sizeof(char));
ret->ptr = ptr;
return ret;
}
int Xfree(void* ptr) {
struct Xalloc_struct* p = (struct Xalloc_struct*) ptr;
int size = p->size;
int index = *(p->ptr);
// .. more stuff here that uses the index of where p->ptr points to
free(p->ptr);
free(p);
return 0;
}
int main() {
disk_init();
struct Xalloc_struct* x = Xalloc(5);
Xfree(x);
return 0;
}
When this compiles I get quite a few errors:
error: invalid application of ‘sizeof’ to incomplete type ‘struct Xalloc_struct’
struct Xalloc_struct *ret = malloc(sizeof(struct Xalloc_struct));
^
error: dereferencing pointer to incomplete type
ret->size = size;
^
error: dereferencing pointer to incomplete type
free(x->ptr);
^
error: dereferencing pointer to incomplete type
int size = cast_ptr->size;
^
error: dereferencing pointer to incomplete type
int free_ptr = *(cast_ptr->ptr);
^
So, how should I be allocating and deallocating these structs? And how can I modify / edit what they contain?
First problem is Xalloc_struct is a type, not the name of a struct. You declared that type with this:
typedef struct {
char* ptr;
int size;
} Xalloc_struct;
typedef is of the form typedef <type name or struct definition> <name of the type>. So you declared the type Xalloc_struct to be struct { char *ptr; int size; }.
That means you use it like any other type name: Xalloc_struct somevar = ...;.
Had you declared the struct with a name...
struct Xalloc_struct {
char* ptr;
int size;
};
Then it would be struct Xalloc_struct somevar = ...; as you have.
The rule of thumb when allocating memory for an array (and a char * is an array of characters) is you allocate sizeof(type) * number_of_items. Character arrays are terminated with a null byte, so for them you need one more character.
Xalloc_struct *ret = malloc(sizeof(Xalloc_struct));
ret->ptr = malloc(sizeof(char) * num_characters+1);
But if you're only storing one character, there's no need for an array of characters. Just store one character.
typedef struct {
char letter;
int size;
} Xalloc_struct;
Xalloc_struct *ret = malloc(sizeof(Xalloc_struct));
ret->letter = 'q'; /* or whatever */
But what I think you're really doing is storing a pointer to a spot in the disk array. In that case, you don't malloc at all. You just store the pointer like any other pointer.
typedef struct {
char* ptr;
int size;
} Xalloc_struct;
Xalloc_struct *ret = malloc(sizeof(Xalloc_struct));
ret->ptr = &disk[i];
Then you can read that character with ret->ptr[0].
Since you didn't allocate ret->ptr do not free it! That will cause a crash because disk is in stack memory and cannot be free'd. If it were in heap memory (ie. malloc) it would probably also crash because it would try to free in the middle of an allocated block.
void Xalloc_destroy(Xalloc_struct *xa) {
free(xa);
}
Here's how I'd do it.
#include <stdio.h>
#include <stdlib.h>
char disk[100] = {0};
typedef struct {
char *ptr;
int idx;
} Disk_Handle_T;
static Disk_Handle_T* Disk_Handle_New(char *disk, int idx) {
Disk_Handle_T *dh = malloc(sizeof(Disk_Handle_T));
dh->idx = idx;
dh->ptr = &disk[idx];
return dh;
}
static void Disk_Handle_Destroy( Disk_Handle_T *dh ) {
free(dh);
}
int main() {
Disk_Handle_T *dh = Disk_Handle_New(disk, 1);
printf("%c\n", dh->ptr[0]); /* null */
disk[1] = 'c';
printf("%c\n", dh->ptr[0]); /* c */
Disk_Handle_Destroy(dh);
}
What you are attempting to accomplish is a bit bewildering, but from a syntax standpoint, your primary problems are treating a typedef as if it were a formal struct declaration, not providing index information to your Xalloc function, and allocating ret->ptr where you already have a pointer and storage in disk.
First, an aside, when you are specifying a pointer, the dereference operator '*' goes with the variable, not with the type. e.g.
Xalloc_struct *Xalloc (...)
not
Xalloc_struct* Xalloc (...)
Why? To avoid the improper appearance of declaring something with a pointer type, (where there is no pointer type just type) e.g.:
int* a, b, c;
b and c above are most certainly NOT pointer types, but by attaching the '*' to the type it appears as if you are trying to declare variables of int* (which is incorrect).
int *a, b, c;
makes it much more clear you intend to declare a pointer to type int in a and two integers b and c.
Next, in Xfree, you can, but generally do not want to, assign a pointer type as an int (storage size issues, etc.) (e.g. int index = *(p->ptr);) If you need a reference to a pointer, use a pointer. If you want the address of the pointer itself, make sure you are using a type large enough for the pointer size on your hardware.
Why are you allocating storage for ret->ptr = malloc(sizeof(char));? You already have storage in char disk[100]; You get no benefit from the allocation. Just assign the address of the element in disk to ptr (a pointer can hold a pointer without further allocation) You only need to allocate storage for ret->ptr if you intend to use the memory you allocate, such as copying a string or multiple character to the block of memory allocated to ret->ptr. ret->ptr can store the address of an element in data without further allocation. (it's unclear exactly what you intend here)
You are free to use a typedef, in fact it is good practice, but when you specify a typedef as you have, it is not equivalent to, and cannot be used, as a named struct. That is where your incomplete type issue arises.
All in all, it looks like you were trying to do something similar to the following:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char* ptr;
int size;
} Xalloc_struct;
char disk[100] = "";
Xalloc_struct *Xalloc (int size, int i) {
char *ptr = &disk[i];
Xalloc_struct *ret = malloc (sizeof *ret);
ret->size = size;
// ret->ptr = malloc (sizeof *(ret->ptr)); /* you have a pointer */
ret->ptr = ptr;
return ret;
}
int Xfree (void *ptr) {
Xalloc_struct *p = (Xalloc_struct *) ptr;
// int size = p->size; /* unused */
// int index = *(p->ptr); /* what is this ?? */
// .. more stuff here that uses the index of where p->ptr points to
// free (p->ptr);
free (p);
return 0;
}
int main (void) {
int i = 0;
Xalloc_struct *x = Xalloc (5, i++);
Xfree(x);
return 0;
}
Look at the difference in how the typedef is used and let me know if you have any questions.

c struct pointer array vs struct pointer pointer malloc

works:
struct data{
int val;
};
int main(void){
struct data *var[2];
(*var)->val = 6;
printf("%d\n", (*var)->val);
return 0;
}
segfault:
struct data{
int val;
};
int main(void){
struct data **var = malloc(3 * sizeof(struct data));
(*var)->val = 6; // <- crash
printf("%d\n", (*var)->val);
return 0;
}
can someone explain why segfault appears and give me an working example with minimal changes to the segfault code that i can understand pls.
The pointer is not malloc'ed, you are dereferencing an invalid pointer because your array is an array of poitners, and it's elements are not pointing to valid memory.
Try this
#include <stdio.h>
#include <stdlib.h>
struct data
{
int val;
};
int main(void)
{
struct data *var[2];
/* You need to malloc before dereferencing `var[0]` */
var[0] = malloc(sizeof(var[0][0]));
if (var[0] != NULL)
{
var[0]->val = 6;
printf("%d\n", var[0]->val);
free(var[0]);
}
return 0;
}
also, using (*var)->val = 6 is absolutely unnecessary and confusing.
In the second case, you should also do almost the same thing, except that the array of pointers is a pointer to an array of poitners and hence needs malloc() too, so your second example accidentally works because there is enough memory malloc()ed but it's also wrong, you should do it this way
#include <stdio.h>
#include <stdlib.h>
struct data
{
int val;
};
int main(void)
{
struct data **var;
var = malloc(2 * sizeof(var[0]));
if (var == NULL)
return -1;
/* You need to malloc before dereferencing `var[0]` */
var[0] = malloc(sizeof(var[0][0]));
if (var[0] != NULL)
{
var[0]->val = 6;
printf("%d\n", var[0]->val);
free(var[0]);
}
free(var);
return 0;
}

Not understanding seg faults when I'm using pointers with structs

I keep getting seg faults and I can't for the life of me figure out why! I tried posting just the simplest example I could write up in code (what you see) to try and figure out the problem but I am stuck. Anything will help!!!!
int main()
{
int i1, i2;
struct intPtrs
{
int *p1;
int *p2;
};
struct intPtrs *myIntPtr;
i1 = 100;
i2 = 200;
myIntPtr->p1 = &i1;
myIntPtr->p1 = &i2;
printf("\ni1 = %i, i2 = %i\n",myIntPtr->p1,myIntPtr->p2);
return 0;
}
You haven't allocated memory for your struct. You need malloc (don't forget to free).
So, your code should look like this (there are others problems, check my code):
#include <stdio.h> // printf()
#include <stdlib.h> // malloc()
// declare the struct outside main()
struct intPtrs {
int *p1;
int *p2;
};
// typedef the struct, just for less typing
// afterwards. Notice that I use the extension
// _t, in order to let the reader know that
// this is a typedef
typedef struct intPtrs intPtrs_t;
int main() {
int i1, i2;
// declare a pointer for my struct
// and allocate memory for it
intPtrs_t *myIntPtr = malloc(sizeof(intPtrs_t));
// check if allocation is OK
if (!myIntPtr) {
printf("Error allocating memory\n");
return -1;
}
i1 = 100;
i2 = 200;
myIntPtr->p1 = &i1;
myIntPtr->p2 = &i2; // here you had p1
// you want to print the numbers, thus you need what the p1 and p2
// pointers point to, thus the asterisk before the opening parenthesis
printf("\ni1 = %d, i2 = %d\n", *(myIntPtr->p1), *(myIntPtr->p2));
// DON'T FORGET TO FREE THE DYNAMICALLY
// ALLOCATED MEMORY
free(myIntPtr);
return 0;
}
When I use structs, I also use a typedef, as you can see in my example here.

C free an instance, which is pointed by other pointers

I have the following C code. I have two pointers pointing to the same object.
It says double free error. Can someone help to see what the problem is? Thanks.
#include <stdlib.h>
#include <stdio.h>
typedef struct edge {
int head;
} edge_t;
typedef struct edge_list_t {
edge_t *edge;
} edge_list_t;
int main() {
edge_list_t *p1;
edge_list_t *p2;
edge_t *c;
p1 = malloc(sizeof(edge_list_t));
p2 = malloc(sizeof(edge_list_t));
c = malloc(sizeof(edge_t));
p1->edge = c;
p2->edge = c;
free(c);
if (p2->edge) {
printf("not freed\n");
free(p2->edge);
} else {c
printf("freed\n");
}
return 1;
}
p2->edge = c;
free(c);
if (p2->edge) {
printf("not freed\n");
free(p2->edge);
^ the last free is a double free. Remember that after the first free call, the value of c is an invalid value.
Here:
p2->edge = c;
free(c);
when you free c the value of c does not change and even if it did the value of p2->edge would stay the same. It would hold the original value of c of course.
So you always free both c and p2->edge which both hold the same value.
To avoid that set c to NULL if you called free() on it and later check if(c) ,which will return false and not free c again.
Note: free() does not change the pointer in any way. It trust you that the pointer points to correct memory that was before never free()d.
You shouldn't free c once you've transferred control of it to p1, and you should not have both p1 and p2 sharing a single edge pointer, and you should release p1 and p2 via a function that also releases the edge pointer. These observations lead to:
#include <stdio.h>
#include <stdlib.h>
typedef struct edge
{
int head;
} edge_t;
typedef struct edge_list_t
{
edge_t *edge;
} edge_list_t;
// Will acquire a loop when you have an actual list of edges
static void free_edge_list(edge_list_t *e)
{
free(e->edge);
free(e);
}
int main(void)
{
edge_list_t *p1;
edge_list_t *p2;
edge_t *c;
p1 = malloc(sizeof(edge_list_t));
p2 = malloc(sizeof(edge_list_t));
c = malloc(sizeof(edge_t));
p1->edge = c;
c = malloc(sizeof(edge_t));
p2->edge = c;
free_edge_list(p1);
free_edge_list(p2);
return 0;
}
In general, you should check that memory allocations succeed; this code (still) doesn't do that.

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