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

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.

Related

Simply creating a data structure, how can it segfault?

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

how can i solve this when i try to return a 2d struct from a function

this is how i declare this struct
typedef struct cache{
int vaild;
char* tag;
char* data;
}cache;
this is part of my main which called this function
struct cache **cacheA = createCache(Setnum,(int)pow(2,blocksize),cachesize);
struct cache **cacheB = createCache(Setnum,(int)pow(2,blocksize),cachesize);
and now this is my called function
struct cache ** createCache(int numset, int blocksize, int cachesize){
int numcache = (int)((cachesize/blocksize)*numset);
struct cache out[numset][numcache];
int i,j;
for (i=0; i < numset; i++){
for (j=0; j < numcache; j++){
out[i][j].tag = "0";
out[i][j].vaild = 0;
out[i][j].data ="0";
}
}
return out;
}
and when i try to compile this, it tells me that
return from incompatible pointer type
function returns address of local variable
(which points to the line "return out;")
I have no idea whats wrong with my code, i mean the type of the function return is the same as how i declear "out", so what causes this problem?
You create struct cache out[numset][numcache];
within the function prototyped as: struct cache ** createCache(...).
Then attempt to return out.
It is because struct cache [][] is typed differently than struct cache ** that you are getting the return errors.
Other comments:
1) If you truly do want a pointer to pointer to struct, then malloc or calloc will need to be used at some point to allocate memory.
2) the char * members of the struct also need to be assigned memory before assigning values. For illustration below, they are changed to char []
3) assigning values to strings does not work by using = assignment operator. Use a string function such as strcpy, sprintf, etc.
4) you've named the struct with the same symbol as that of the new type you have created, i.e. cache. In this application, the name cache is not necessary. Also, purely for style, I show the new type in CAPS. This is not necessary, but just a style I use to make the new type more recognizable in code.
In consideration of the comments above, the struct could be changed to the following:
typedef struct { /// don't need name here when it in this application
int vaild;
//char *tag;
char tag[20];//for illustration, to avoid additional dynamic allocation of memory
//char* data;
char data[80];
}CACHE;//capitalization is style only, not a necessity here.
Note, there is no name, but the new type CACHE was created. Now, you can create the function createCache:
CACHE ** createCache(int ncache, int nset)//note for simplicity of freeing this
//object later, simplify number of arguments
{
CACHE **out;
out = calloc(ncache, sizeof(CACHE *));//create array of pointers to CACHE
if(!out) return NULL;
int i;
for (i=0; i < nset; i++)
{
out[i] = calloc(nset, sizeof(CACHE));//create space for each instance
//of CACHE pointed to by array pointers
}
return out;
}
Anytime memory is created on the heap, it needs to be freed. This method will free the CACHE object memory:
void freeCashe(CACHE **a, int nset)
{
int i;
for(i=0; i<nset; i++)
{
if(a[i])free(a[i]);
}
if(a)free(a);
}
Calling these functions as shown below, will create an array of pointers, each pointing to an instance of CACHE where you can use them as intended, then free all of the memory when finished:
int main(void)
{
int cachesize = 20;
int blocksize = 20;
int numset = 10;
//move the calculation out of creation function
//to simplify freeing object later.
int numcache = (int)((cachesize/blocksize)*numset);
CACHE **a = createCache(numcache, numset);
/// use a, then free a
freeCashe(a, numset);
return 0;
}
Your function needs to allocate the memory on the heap rather than the stack. You will need to allocate space on the heap for your array of pointers, and for what they point too.
struct cache ** createCache(int numset, int blocksize, int cachesize){
cache ** out;
int numcache = (int)((cachesize/blocksize)*numset);
size_t headerSize = sizeof(*out)*numset;
size_t bodySize = sizeof(**out)*numcache;
out = malloc(headerSize + (bodySize*numset));
if (out == NULL) {
/* Should probably output some message about being
* insufficient memory here. */
return NULL;
}
int i,j;
for (i=0; i < numset; i++){
/* need to assign our point */
out[i] = (cache*)(((char*)out)+(headerSize+bodySize*i));
for (j=0; j < numcache; j++){
out[i][j].tag = "0";
out[i][j].vaild = 0;
out[i][j].data ="0";
}
}
return out;
}
/* importantly, you want a way to free your allocated memory */
void destroyCache(cache ** ptr) {
free(ptr);
}
PS: You don't have to typedef your struct if you reference it with the struct keyword.
You are wanting a pointer pointer type to be returned, but in order to do that you need to dynamically allocate it. Local stack allocations (i.e. struct cache[x][y]) won't work. You will either get an error or your program will crash when attempting to use the 2D array.
The solution is to either pre-allocate space and pass it in to the function or allocate in the function itself.
Allocation In Function Example:
struct cache ** createCache(int numset, int blocksize, int cachesize){
int numcache = (int)((cachesize/blocksize)*numset);
struct cache **out = malloc(sizeof(struct cache *) * numset); // This line changed.
int i,j;
for (i=0; i < numset; i++){
out[i] = malloc(sizeof(struct cache) * numcache); // This line added.
for (j=0; j < numcache; j++){
out[i][j].tag = malloc(sizeof(char)); // This line added.
out[i][j].data = malloc(sizeof(char)); // This line added.
strcpy(out[i][j].tag, "0");
out[i][j].vaild = 0;
strcpy(out[i][j].data, "0");
}
}
return out;
}

initialize the struct pointer

typedef struct
{
char *s;
char d;
}EXE;
EXE *p;
For the above struct how do I initialize the structure with pointer? I know for a non-pointer we do EXE a[] = { {"abc",1}, {"def",2} }; . Similarly Is it possible with a pointer after allocating the memory? Say like p[] = { {"abc",1},.. so on} . Basically I want to initialize dynamically. Thanks.
We can initialise the structure with pointer as below
example:
int i;
char e[5]="abcd";
EXE *p=malloc(sizeof(*p));
for(i = 0;i < 5;i++)
*(p+i)=(EXE){e,i+48};
First you need to allocate some memory for that char * and after that use strcpy library function to copy data for structure element.
p->s = strcpy(s,str); //where str source, from where you need to copy the data
I hope this will help. Though I can give you full code for that, But I want you to try.
You can use this
Dynamically allocate C struct?
and it is a duplicate question.
You have to understand how do allocated pointer works:
Suppose you've allocated memory for three structs Ptr = malloc(3*sizeof(EXE)).
Then when you add 1 to Ptr, it comes to the next struct. You have a block of memory divided by 3 (3 smaller blocks of memory for each struct).
So, need to access to the elements of the 1st struct and then move the pointer to the next one.
Here you can understand how it works:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *s;
char d;
} EXE;
int main()
{
int i;
EXE *Ptr;
Ptr = malloc(3*sizeof(EXE)); // dymnamically allocating the
// memory for three structures
Ptr->s = "ABC";
Ptr->d = 'a';
//2nd
Ptr++; // moving to the 2nd structure
Ptr->s = "DEF";
Ptr->d = 'd';
//3rd
Ptr++; // moving to the 3rd structure
Ptr->s = "XYZ";
Ptr->d = 'x';
//reset the pointer `Ptr`
Ptr -= 2; // going to the 1st structure
//printing the 1st, the 2nd and the 3rd structs
for (i = 0; i < 3; i++) {
printf("%s\n", Ptr->s);
printf("%c\n\n", Ptr->d);
Ptr++;
}
return 0;
}
Notice:
- If you have a variable of a struct use . opereator to access to the elements.
- If you have a pointer to a struct use -> operator to access to the elements.
#include <stdio.h>
#include <stdlib.h>
struct EXE {
int a;
};
int main(){
struct EXE variable;
struct EXE *pointer;
pointer = malloc(sizeof(struct EXE)); // allocating mamory dynamically
// and making pointer to point to this
// dynamically allocated block of memory
// like here
variable.a = 100;
pointer->a = 100;
printf("%d\n%d\n", variable.a, pointer->a);
return 0;
}

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.

what is causing segmentation fault in c code, dynamic allocation accross functions

I am trying to have dynamically allocate arrays of structures and perform operations on them but i keep running into segmentation faults. could someone help me out?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *malloc(size_t size);
typedef struct {
double x;
double y;
} coords;
struct figure {
char fig_name[128];
int coordcount, size_tracker;
coords *pointer;
} fig;
void init_fig(int n, struct figure **point)
{
printf("%u\n", sizeof(coords));
point[n]->pointer = malloc(sizeof(coords) * 20); <-------SEGFAULT
if (point[n]->pointer == NULL){
exit(-1);
}
point[n]->pointer[19].x = 2;
point[n]->pointer[0].x = 1;
point[n]->pointer[0].y = 2;
point[n]->pointer[7].x = 100;
}
int main()
{
int numfigs = 1;
struct figure * point;
point = malloc(sizeof(struct figure) * 16);
point = &fig;
point[1].coordcount = 1;
init_fig(numfigs, &point);
return 0;
}
I labelled where the first seg fault occurs, (used ddd). what i dont get is that i can manipulate point[1] in main but not in any other function.
I agree with #Maxim Skurydin.
Nevertheless I'd like to explain your mistake in some more details.
Reading your init_fig one assumes that the parameter you pass struct figure **point - is actually array of pointers to struct figure. And this function accesses its n'th element.
However in your main you do something else. You allocate an array of struct figure, and your point variable points to its head. Then you take the address of this local variable and call your init_fig.
Here's the problem. init_fig assumes that you pass it an array of pointers, whereas actually this "array" consists of a single element only: the local point variable declared in main.
EDIT:
How to do this properly.
Leave main intact, fix init_fig.
This means that actually there's an array of figure structs. Means - a single memory block, interpreted as an array of consequent structs.
void init_fig(int n, struct figure *point)
{
printf("%u\n", sizeof(coords));
point[n].pointer = malloc(sizeof(coords) * 20); <-------SEGFAULT
if (point[n].pointer == NULL){
exit(-1);
}
point[n].pointer[19].x = 2;
point[n].pointer[0].x = 1;
point[n].pointer[0].y = 2;
point[n].pointer[7].x = 100;
}
Leave init_fig intact. Fix main.
This means that we actually should allocate an array of pointers, every such a pointer should point to an allocated point structure.
int main()
{
int numfigs = 1;
struct figure ** point;
point = malloc(sizeof(struct figure*) * 16);
for (i = 0; i < 16; i++)
point[i] = malloc(sizeof(struct figure));
point[1].coordcount = 1;
init_fig(numfigs, &point);
return 0;
}
You allocate memory and store the pointer in point but then you forget that pointer when you assign &fig to it.
point = malloc(sizeof(struct figure) * 16);
point = &fig;
So, you are essentially trying to write fig[1], that does not make sense.
struct figure * point;
point = malloc(sizeof(struct figure) * 16);
here point is pointer pointing to memory of 16 structures in heap
but in the next line you have done this
point = &fig;
so its memory leak and also point is not pointing to that allocated region anymore
and also init_fig should be like this
void init_fig(int n, struct figure **point)
It's the problem of segfault
Eliminate this line point = &fig;
and modify the function:
void init_fig(int n, struct figure *point)
{
...
point[n].pointer = (coords*) malloc(sizeof(coords) * 20);
...
}
since you should pass an array of structs and not an array of pointers.
Also, add a third parameter to the init_fig function so you can pass the size of the array of points that you want to create. Like :
void init_fig(int n, struct figure *point, int size)
{
...
point[n].pointer = (coords*) malloc(sizeof(coords) * size);
...
}
Therefore, making the function more reusable.
Modify also the call to that function:
init_fig(numfigs, &point); to init_fig(numfigs, point);

Resources