This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 4 years ago.
I'm not sure why the following is producing a segmentation fault. I've defined a structure and I'm trying to store a value to it.
typedef struct {
int sourceid;
int destid;
} TEST_STRUCT;
void main( int argc, char *argv[] ) {
TEST_STRUCT *test;
test->sourceid = 5;
}
You declare a pointer to the type. You need to allocate memory for the pointer to point to:
#include <stdlib.h>
typedef struct {
int sourceid;
int destid;
} TEST_STRUCT;
int main(int argc, char *argv[]) {
TEST_STRUCT *test;
test = malloc(sizeof(TEST_STRUCT));
if (test) {
test->sourceid = 5;
free(test);
}
return 0;
}
Alternatively, you could declare the variable on the stack:
typedef struct {
int sourceid;
int destid;
} TEST_STRUCT;
int main(int argc, char *argv[]) {
TEST_STRUCT test;
test.sourceid = 5;
return 0;
}
test pointer is not pointing to any address(pointing some garbage) so it is hitting segV
TEST_STRUCT *test;
it is good practice to initialize NULL and before dereference it, check if (test != NULL) {}
then only dereference.
to solve this, first you need to create variable of TEST_STRUCT and assign address of it to test pointer or allocate memory using malloc/calloc and then try this
Related
I try to malloc a pointer to char nested in few structures, but when I run the code and the main passes an argument that causes a seg fault. I cannot figure how that can happen and why it raises a seg fault in this case.
#include <stdlib.h>
typedef struct s_child t_child;
struct s_child {
char *str;
};
typedef struct s_mother t_mother;
struct s_mother {
t_child *child;
};
// int main() { // work fine
int main(int num, char **arg) { // cause a seg fault
t_mother mother;
// the malloc bellow cause a seg fault when int main() can pass arguments.
if (!(mother.child->str = (char *)malloc(sizeof(char))))
return (0);
return (0);
}
You have declared mother, but its pointer to child is never initialized to anything. Then you dereference that child pointer. That is the cause of the seg fault.
Try the following:
int main(int num, char **arg) { // cause a seg fault
t_mother mother;
t_child child;
mother.child = &child;
// the malloc bellow cause a seg fault when int main() can pass aguments.
if (!(mother.child->str = (char*)malloc(sizeof(char))))
return (0);
return (0);
}
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 2 years ago.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char** thingSize;
} Thing;
typedef struct {
Thing* thing;
} Game;
void load_array(Thing* thing) {
int i, j;
char **emptyThing = malloc(sizeof(char**));
emptyThing = malloc(sizeof(char*)*9);
for(i=0; i<9; i++) {
emptyThing[i] = malloc(sizeof(char)*9);
}
thing -> thingSize = emptyThing;
free(emptyThing);
}
int main(int argc, char* argv[]) {
Game* game;
load_array(game -> thing);
printf("HI");
}
I am getting a segmentation fault, I have found that the problem line is.
thing -> thingSize = emptyThing;
I am trying to set thingSize to be a 2d array equal to emptyThing.
As Fredrik said, the game pointer is not initialized to anything. It hold a garbage value, when dereferencing it, you will get a segfault.
Okay, so the problem concerns adding values through function to structure. Honestly, I couldn't solve the problem (spent a lot of time trying), so I am asking for your help. While executing the program, I get a segmentation fault. It occurs while using the variables from stack stos.
typedef struct e {
int zaglebienie[100];
char *nazwa_funkcji[100];
int poz;
} *stack;
void put_on_fun_stack(int par_level, char *funame, stack stos) {
int i = stos->poz;
stos->zaglebienie[i] = par_level;
char *funkcja = strdup(funame);
stos->nazwa_funkcji[i] = funkcja;
stos->poz++;
}
int main() {
char *p = "makro";
stack stos;
stos->zaglebienie[0] = 0;
put_on_fun_stack(1, p, stos);
return 0;
}
You're declaring a pointer to stack but you're not allocating any memory to it.
And as already mentioned in the comments, using typedef with with a pointer will unnecessarily complicate your life.
So I suggest you create the struct stack and then in main declare a pointer to stack and allocate memory for it, somewhat like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct e {
int zaglebienie[100];
char *nazwa_funkcji[100];
int poz;
} stack;
void put_on_fun_stack(int par_level, char *funame, stack *stos)
{
int i = stos->poz;
stos->zaglebienie[i] = par_level;
char *funkcja = strdup(funame);
stos->nazwa_funkcji[i] = funkcja;
stos->poz++;
}
int main(void)
{
char *p = "makro";
// calloc to initialize stos variables to 0
stack *stos = calloc(sizeof(stack), 1);
printf("stos->poz before: %d\n", stos->poz);
put_on_fun_stack(1, p, stos);
printf("stos->poz after: %d\n", stos->poz);
printf("stos->nazwa_funkcji[0]: %s\n", stos->nazwa_funkcji[0]);
free(stos->nazwa_funkcji[0]);
free(stos);
return 0;
}
Output:
stos->poz before: 0
stos->poz after: 1
stos->nazwa_funkcji[0]: makro
Say I have a simple struct, such as this one:
struct myStruct {
uint8_t arr[10];
};
All I want to be able to do is to modify the contents of that array. However, it seems that I cannot assign the array directly (ie, I can't do something like pointerToThisStruct->arr = anArrayofSizeTen).
So here is my main method:
int main(int argc, char **argv) {
uint8_t test[10] = {0};
myStruct *struc;
struc->arr = test; //can't do this
memcpy(struc->arr, test, sizeof(test));
}
Now, I understand that direct copying over won't work, but why is memcpy also giving me a segfault? How exactly am I supposed to modify the struct array?
You need to declare an actual myStruct. Not a pointer to one. Declaring a pointer to one doesn't actually allocate any memory for the struct.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
struct myStruct {
uint8_t arr[10];
};
int main(int argc, char **argv) {
int i;
uint8_t test[10] = {0,1,2,3,4,5,6,7,8,9};
struct myStruct struc;
memcpy(struc.arr, test, sizeof(struc.arr));
printf("struc.arr[] = ");
for( i=0; i < sizeof(test); i++ )
{
printf("%d ", struc.arr[i]);
}
printf("\n");
return( 0 );
}
You are getting a segmentation fault because you didn't allocate your struct pointer.
int main(int argc, char **argv) {
uint8_t test[10] = {0};
struct myStruct *struct = malloc(sizeof(struct myStruct));
if (!struc)
return -1;
memcpy(struc->arr, test, sizeof(test));
free(struc);
return 0;
}
But, as #Chimera mentioned, you perfectly can not use a point and directly a heap-allocated structure, and access to its inner fields with the . operator
I am new to c programming. Could anyone please tell me what's wrong with
the following program?
typedef struct Person_s
{
int age;
char name[40];
} Person_t;
int process_list(int *countReturned, Person_t **p_list)
{
Person_t *rowPtr=0;
//the actual program will fethc data from DB
int count =1;
if(!((*p_list) = (Person_t *) malloc(sizeof(Person_t))))
{
return -1;
}
rowPtr = *p_list;
rowPtr[count-1].age =19;
strcpy(rowPtr[count-1].name,"Prince Dastan");
*countReturned = count;
return 0;
}
int main(int argc, char *argv[])
{
Person_t *tmpPerson=0;
Person_t **p_list=0;
int *count=0;
int i;
process_list(count,p_list);
tmpPerson = *p_list;
for(i=0; i< *count; i++)
{
printf("Name: %s , age: %d\n",tmpPerson->name,tmpPerson->age);
tmpPerson++;
}
//free(tmpPerson);
return 0;
}
Your problem is that you're setting the pointers to point to NULL (0), then dereferencing them. You are not allowed to dereference NULL. Want you want is more like this:
int main(int argc, char *argv[])
{
Person_t tmpPerson;
Person_t *p_list=0;
int count;
int i;
process_list(&count, &p_list);
tmpPerson = *p_list;
// and so on...
The & is the "address-of" operator, which returns a pointer to the variable's address. So this passes a pointer to count and p_list, which your function then uses to set those variables, which appears to be what you want to do.
You should have in main:
Person_t *p_list=0;
...
process_list(count, &p_list);
The code as written passes in 0 to process_list, and then you have:
*0 = (Person_t *)malloc(...);
This causes 0 to be dereferenced, and your code will crash.
The value of p_list as you enter the function is 0. If you dereference 0 you get a Bus Error.
if(!((*p_list) = (Person_t *) malloc(sizeof(Person_t))))
(90% of C problems are caused by dereferencing a null pointer, just like 90% of Java problems are caused by a misconfigured classpath. :-)