Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
It just shows me: [1] 10531 Segmentation fault.
I don't what is the problem of my code!(I have defined a list and add an element into the list. But it just failed and I don't know how to change it.)
#include <stdio.h>
#include <stdlib.h>
typedef struct ll {
int val;
struct ll *next;
} ll_t;
void copylist(ll_t* list, int* array, int length) {
int i;
for (i = 0; i < length; i++) {
array[i] = list->val;
list = list->next;
}
}
int main() {
ll_t *list = NULL;
int *array = NULL;
list = malloc(sizeof (ll_t));
list->val = 1;
list->next = NULL;
ll_t *add1 = NULL;
add1 = malloc(sizeof (ll_t));
add1->val = 2;
add1->next = NULL;
list->next = add1;
int j;
copylist(list, array, 2);
for (j = 0; j < 2; j++) {
printf("the content of array is: %d\n", array[j]);
}
return 1;
}
Currently all that you're passing as an array is a null pointer because you instantiate it as
int *array = NULL;
instead of
int *array = malloc(2 * sizeof(int));
which allocates enough space for two integers on the heap. (two being the amount of items you're using in this test).
Also, You should use
int i = 0;
while(list != NULL) {
array[i] = list->val;
list = list->next;
i++;
}
instead of passing the length value, as you know when you reach the end of a linked list when you reach a null value. If someone were to input the wrong length by accident in your program it would lead to another segmentation fault.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to compile my code and I am getting the Expected ';' identifier or '(' token before 'void' error on C:10:1.. can't seem to find the typo if someone can help me out I would appreciate it! I have provided my code down below I am sure it is just a silly mistake I made somewhere.
#include <stdio.h>
#include <stdlib.h>
//setting up my binary converter struct
struct bin
{
int data;
struct bin *link;
}
void append(struct bin **, int); //setting up append value
void reverse(struct bin**); //reverse values to convert to binary
void display(struct bin *); //so we can display
int main(void)
{
int nu,i; //global vars
struct bin *p;
p = NULL; //setting up p pointer to NULL to make sure it has no sense of being some other value
printf("Enter Value: ");
scanf("%d", &nu);
while (nu != 0)
{
i = nu % 2;
append (&p, i);
nu /= 2;
}
reverse(&p);
printf("Value in Binary: ");
display(p);
}
//setting up our append function now
void append(struct bin **q, int nu)
{
struct bin *temp,*r;
temp = *q;
if(*q == NULL) //making sure q pointer is null
{
temp = (struct bin *)malloc(sizeof(struct bin));
temp -> data = nu;
temp -> link = NULL;
*q = temp;
}
else
{
temp = *q;
while (temp -> link != NULL)
{
temp = temp -> link;
}
r = (struct bin *) malloc(sizeof(struct bin));
r -> data = nu;
r -> link = NULL;
temp -> link = r;
}
//setting up our reverse function to show in binary values
void reverse(struct bin **x)
{
struct bin *q, *r, *s;
q = *x;
r = NULL;
while (q != NULL)
{
s = r;
r = q;
q = q -> link;
r -> link = s;
}
*x = r;
}
//setting up our display function
void display(struct bin *q)
{
while (q != NULL)
{
printf("%d", q -> data);
q = q -> link;
}
}
You need to add semicolon (;) after declaration of your struct:
struct bin
{
int data;
struct bin *link;
};
Also, your main() function should return something. Add return 0; at the end of it.
And one more thing I noticed. Here:
int nu,i; //global vars
The comment does not make any sense, as nu and i are not global variables. They are local variables, because they are declared in the scope of your main() function.
EDIT:
The latter two comments obviously did not cause the compiling error, but I thought it would be a good think to mention them anyway.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
typedef struct{
int *sols;
int rest;
int fitness;
int num;
struct lista* next;
}lista;
lista* gere_lista(lista *solucoes, int *sol, int *grafo, int objs, int rests){
int i;
int *nova_sol;
lista *temp = solucoes;
nova_sol = malloc(sizeof(int)*objs);
nova_sol = update_sol(sol, nova_sol, objs);
for(i=0; i<objs; i++){
nova_sol = gera_vizinho3(nova_sol, i);
temp->sols = malloc(sizeof(int)*objs);
temp->sols = update_sol(nova_sol, temp->sols, objs);
temp->rest = calcula_restricoes(nova_sol, grafo, objs, rests);
temp->fitness = calcula_max(nova_sol, grafo, objs);
temp->num = i;
temp = temp->next;
}
return solucoes;
}
int* pesquisa_tabu(int sol[], int *grafo, int objs, int rests, int num_iter){
int fitness, fitness_viz, i, memoria[objs*2/8];
lista *solucoes, *temp;
solucoes = malloc(sizeof(lista));
temp = solucoes;
for(i=1; i<objs; i++){
temp->next = malloc(sizeof(lista));
}
temp->next = NULL;
solucoes = gere_lista(solucoes, sol, grafo, objs, rests);
return sol;
}
I'm trying to do a tabu search algorithm to my school assignment, but it's not really working.
This code is supposed to create a linked list with the same number of links as the objects that are in the pesquisa_tabu function, then it generates the same number in neighbors and add one neighbor to each link, to then process all the information in the pesquisa_tabu function.
And i can't find what is wrong in this code, it is always crashing on the second iteration...
for(i=1; i<objs; i++){
temp->next = malloc(sizeof(lista));
}
temp->next = NULL;
What you have done is allocated memory for objs number of lista's and lost links to all of them because you are always making temp->next point to a new memory and then later making it point to NULL.You need to maintain a head of the list while allocating memory for all nodes.
lista *solucoes, *temp;//solucoes will act as head of list
temp = NULL;
solucoes = NULL;
for(i=1; i<objs; i++)
{
if(solucoes==NULL)
{
temp = malloc(sizeof(lista));
solucoes = temp;
}
else
{
temp->next = malloc(sizeof(lista));
temp = temp->next;
}
}
temp->next = NULL;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
i tried to print linked list in reverse order via recursion.
this is code:
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int value;
struct list* next;
} list_s;
static int g_size=sizeof(list_s);
void printRe(list_s *node) {
if (node = NULL)
return;
printRe(node->next); // this is where error happens
printf("%d", node->value);
}
int main() {
list_s *head;
head = (list_s*)malloc(g_size);
int n;
scanf("%d",&n);
int i = 0;
int x;
scanf("%d", &x);
head->value = x;
head->next = NULL;
list_s *current;
current = head;
for (i; i < n - 1; ++i) {
current->next = (list_s*)malloc(g_size);
current = current->next;
scanf("%d", &x);
current->value = x;
current->next = NULL;
};
printRe(head);
return 0;
}
So as you see, the error occurs when I tried printing node->next. why did error occur? did i passed list to a function wrong way?
Thank you!
if condition in printRe() function should be like
if(node==NULL) instead of if(node=NULL)
i hope it will help you.
Thanks
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm working on C language.
I have a pointer and I wanna create a new struct beginning with the address. Here is my way to do it and I can pass the compile but when I run it, it gets bus error and segment error.
struct node{
int value;
int value2
struct node *next;
}
int main(){
struct node a = {0, 0, NULL};
void *p = (void*)(&a + 1);
struct node *ptr = (struct node *)(p);
//These two statements below cause the problem.
(*ptr).value = 100;
(*ptr).next = NULL;
}
Could someone help me?
Try this, assuming you can only use statically defined memory and want a linked list:
struct node{
int value;
int value2
struct node *next;
};
#define LEN 10
static struct node arr[LEN] = {0};
int main(){
// Initialize the array of nodes
for (int i = 0; i < LEN; i++) {
struct node * const ptr = &a[i];
ptr->value = 100;
ptr->next = (i == (LEN-1)) ? NULL : &a[i+1];
}
}
In your code p points to a memory location that passes the address of a which is an integer, it is undefined behavior to access it anyway.
What you want is allocating the memory instead, e.g.:
struct node *ptr = malloc(sizeof *ptr);
ptr->value = 100;
ptr->next = NULL;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In order to understand tries I am creating this very simple C program that takes from user 10 nums from 0 to 9 as children of the trie. The final step is to print this nums with the function print, but I am getting a segmentation fault:
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list *ar[10];
} list;
void insert(list *head);
void print(list *head);
int main(void)
{
printf("hello\n");
list *root = malloc(sizeof(list));
insert(root);
print(root);
}
void insert(list *head)
{
int a, i;
if (head == NULL) {
return;
}
for (i = 0; i < 10; i++) {
printf("Give Num 0-9\n");
scanf("%i", &a);
head->ar[a] = malloc(sizeof(head));
head = head->ar[a];
head->data = a;
}
}
void print(list *head)
{
if (head == NULL) {
return;
}
while (head != NULL) {
for (int i = 1; i < 10; i++) {
if (head->ar[i] != NULL) {
printf("%i", i);
head = head->ar[i];
break;
}
}
}
printf("\n");
}
There are several issues with your code:
The first mention of malloc doesn't actually initialize the memory (the ar field). You should initialize it properly
list *root = malloc(sizeof(list));
Is missing initialization, e.g.
root->data = 0;
for (size_t ii = 0; ii < 10; ii++) {
root->ar[ii] = NULL;
}
When you are actually gathering input, you allocate only enough memory for the pointer, not for the actual list itself.
head->ar[a] = malloc(sizeof(head));
should be initialized as above (head = malloc(sizeof(list)); for (size_t ...
There seems to be an infinite loop when actually running your program (after correcting all these issues).
EDIT: Remove calloc...