Bad access in Xcode - c

I wrote a code which produces the following error:
Thread 1: EXC_BAD_ACCESS (code=1, adress=0x7.....)
I want the program to link the countries, states, cities and shops within an structure. But when I try to run my program it gives me the error you see above.
I already tried deleting the strcpy and the for but the error still occurs. So the error must be within the structures. What is it I'm doing wrong?
#include <stdio.h>
#include <string.h>
#define SMAX 16
#define CMAX 256
#define SHMAX 300
int main() {
struct country {
char cname[50];
struct state {
char sname[50];
struct city {
char cityname[50];
struct shop {
char shopname[50];
int countshop;
} shop[SHMAX];
int countcity;
} city[CMAX];
int countstate;
} state[SMAX];
} country;
// country = Germany;
strcpy(country.state[0].sname, "bayern");
strcpy(country.state[1].sname, "berlin");
strcpy(country.state[0].city[0].cityname, "ingolstadt");
strcpy(country.state[0].city[0].shop[0].shopname, "westpark");
strcpy(country.state[0].city[0].shop[1].shopname, "edeka");
for (int i = 0; i < SHMAX; i++) {
printf("%s\n", country.state[0].city[0].shop[i].shopname);
}
return 0;
}

The size of the struct is 69043124 bytes which is too much to fit on the stack.
As thread safety is no concern, the struct could be made static:
int main(void) {
static struct country {

Related

segmentation fault memory dumped

I have following code. I got a error that 'segmentation fault memory dumped' when I write an array 'ADDRESS.Person' to any value. any one please help me to solve a problem.
#include <stdio.h>
typedef struct
{
char Person[15];
} stName;
typedef struct
{
stName Name;
} stSociety;
stSociety* SOCIETY;
#define ADDRESS SOCIETY->Name
int main()
{
int i;
for (i=0; i<32; i++)
{
ADDRESS.Person[i] = 0;
}
printf("ADDRESS.Person=%s\n", ADDRESS.Person);
printf("Finished");
return 0;
}
You have just declared the structs , you need to create them as well and hence,SOCIETY is pointing to nothing.Also you are iterating through 32 values whereas there are only 15 in the char array.I have modifed the code ,hopefully you will get an idea here
#include <stdio.h>
typedef struct
{
char Person[15];
} stName;
typedef struct
{
stName Name;
} stSociety;
#define ADDRESS SOCIETY->Name
int main()
{
stSociety* SOCIETY,sample;
SOCIETY = &sample;
int i;
for (i=0; i<15; i++)
{
ADDRESS.Person[i] = '0';
}
printf("ADDRESS.Person=%s\n", ADDRESS.Person);
printf("Finished");
return 0;
}

sprintf with struct array field - gets segmentation fault

This idea is to format text info messages bellowing to a structure within a module.
It works like a charm when trying to define the message with (cf module.c):
/*this works*/
module_text3.info_text[0] = "toto[0]";
module_text3.info_text[1] = "toto[1]";
But when using sprintf, I got segmentation fault (cf module.c):
/*this gives segmentation fault*/
for(cpt=0; cpt < 2; cpt++)
{
sprintf(module_text3.info_text[cpt], "info[%u]", cpt);
}
3 different files: main.c, module.h and module.c
/*main.c*/
/*gcc -o test main.c module.c*/
#include <stdio.h>
#include "module.h"
int main(int argc, char **argv)
{
int i;
struct message3 *ptext3 = moduleFcn3();
for (i= 0; i < ptext3->info_nb; i++)
{
printf("ptext3->info_text[%u]: %s\n", i, ptext3->info_text[i]);
}
printf("ptext3->error_text: %s\n", ptext3->error_text);
printf("ptext3->id: %u\n", ptext3->id);
printf("ptext3->info_nb: %u\n", ptext3->info_nb);
printf("ptext3->info_nb_max: %u\n", ptext3->info_nb_max);
return 0;
}
/*------------------------------------------------------*/
/*module.h*/
#define NB_LINE_MAX 10
struct message3
{
char *info_text[NB_LINE_MAX]; /*a few info lines.*/
char *error_text; /*only one line for error.*/
int id;
int info_nb_max;
int info_nb;
};
extern struct message3* moduleFcn3(void);
/*------------------------------------------------------*/
/*module.c*/
#include <stdio.h>
#include "module.h"
/*static is in "Stack".*/
static struct message3 module_text3;
struct message3* moduleFcn3(void)
{
int cpt = 0;
struct message3 *ptext;
/*this gives segmentation fault*/
for(cpt=0; cpt < 2; cpt++)
{
sprintf(module_text3.info_text[cpt], "info[%u]", cpt);
}
/*this works*/
// module_text3.info_text[0] = "toto[0]";
// module_text3.info_text[1] = "toto[1]";
// cpt = 2;
module_text3.error_text = "This is error";
module_text3.id = 4;
module_text3.info_nb_max = NB_LINE_MAX;
module_text3.info_nb = cpt;
ptext = &module_text3;
return ptext;
}
I would appreciate any advises on how to format my information messages (with our without using sprintf).
Thank you,
You have not allocated space for the strings in the info_text field. The simplest thing to do would be to change the struct:
/*module.h*/
#define NB_LINE_MAX 10
#define INFO_MAX 25
struct message3
{
char info_text[NB_LINE_MAX][INFO_MAX]; /*a few info lines.*/
char *error_text; /*only one line for error.*/
int id;
int info_nb_max;
int info_nb;
};
extern struct message3* moduleFcn3(void);
You did not allocate any memory for the info_text strings. You either have to use malloc() first, or if your C library supports it (the GNU one does), use asprintf() instead of sprintf() to have it allocate enough memory to hold the whole output string for you:
for(cpt = 0; cpt < 2; cpt++)
asprintf(&module_text3.info[cpt], "info[%u]", cpt);
Don't forget that you also have to free the memory again at some point.
The reason that the following line works:
module_text3.info_text[0] = "toto[0]";
Is that the compiler ensures the string "toto[0]" is stored in memory somewhere, and you just make the pointer module_text3.info_text[0] point to that string.

Filling in data for a struct that holds an array of of pointers to a different struct

In my code below there are two structs. One called person and one called person_list which holds by reference a list of person structs or 'people'.
I want to fill in (or reference) 10 person structs within person_list but upon running this code I am getting a segmentation fault. How can I handle or declare the memory for each person so this works?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 50
#define MAX_PEOPLE_ALLOWED 10
struct person_list {
struct person *people[MAX_PEOPLE_ALLOWED];
};
struct person
{
char name[MAX_LENGTH];
//int age;
};
int main(int argc, char *argv)
{
struct person_list list;
struct person pers[10];
int i;
char name[MAX_LENGTH];
for (i = 0; i < MAX_PEOPLE_ALLOWED; i++) {
sprintf(descrip, "I am person number: %d", i);
strcpy( &pers[i].name, name);
list.people[i] = &pers[i];
}
}
#BDillan ,I have made some simple corrections to your code, hope that you are looking for something similar to this
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 50
#define MAX_PEOPLE_ALLOWED 10
struct person_list {
struct person *people[MAX_PEOPLE_ALLOWED];
};
struct person
{
char name[MAX_LENGTH];
//int age;
};
int main()
{
struct person_list list;
struct person pers[10];
char descrip[MAX_LENGTH];
int i;
char name[MAX_LENGTH];
for (i = 0;i < MAX_PEOPLE_ALLOWED; i++)
{
sprintf(descrip, "I am person number: %d", i);
strcpy(pers[i].name,descrip);
//puts(pers[i].name);
list.people[i] = &pers[i];
}
//to display the details of persions entered above
for (i = 0;i < MAX_PEOPLE_ALLOWED; i++)
printf("%s \n",list.people[i]->name);
}

Class in C (not C++)

I've discovered this hack in a website in Spanish (http://trucosinformaticos.wordpress.com/2010/12/12/programacion-orientado-a-objetos-en-c/).
I want create a "class" in C (not C++), but when I compile, I obtain the next errors:
source.c(25): warning C4047: 'function' : 'Car' differs in levels of indirection from 'Car *'
source.c(25): warning C4024: 'changeYears' : different types for formal and actual parameter 1
This is my code:
#include <string.h>
typedef struct Car* Car;
// class Car
// {
struct Car
{
int years;
//char model[100];
};
void changeYears(Car this, int years)
{
this->years = years;
}
// }
int main(void)
{
Car my_cars[10];
//nombrar(mis_alumnos[0], "Pepito");
changeYears(&my_cars[0], 6); // My car has now 6 years
return 0;
}
I would agree with #Oli Charlesworth that hiding a pointer behind a typedef is a very easy way to confuse yourself and others.
However, to make your code compile and work, you can just remove the & operator in front of my_cars. You also need to allocate memory for those pointers. I would say the reason why you made this mistake in the first place was that you confused yourself with the pointer hiding.
#include <string.h>
typedef struct Car* Car;
struct Car
{
int years;
//char model[100];
};
void changeYears(Car this, int years)
{
this->years = years;
}
int main(void)
{
// An array of struct char*
Car my_cars[10];
int i;
for (i = 0; i < 10; i++)
my_cars[i] = malloc(sizeof(struct Car));
changeYears(my_cars[0], 6); // My car has now 6 years
return 0;
}
Here is a more reasonable way to implement this without hiding pointers.
#include <string.h>
typedef struct
{
int years;
//char model[100];
} Car;
void changeYears(Car* this, int years)
{
this->years = years;
}
int main(void)
{
Car my_cars[10];
changeYears(&my_cars[0], 6); // My car has now 6 years
return 0;
}
I think this is what you are looking for:
(A much cleaner implementation, of what you want)
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int years;
} Car;
void changeYears(Car *this, int years)
{
this->years = years;
}
int main(void)
{
Car *car = malloc(sizeof(Car));
changeYears(car, 2014);
printf("car.years = %d\n", car->years);
free(car);
return 0;
}
OUTPUT:
car.year = 2014

C struct example, errors during compilation

I am trying to get familiar with struct and pointers in C and I am running into a bunch of syntax errors like "missing ';' before type", "missing ')' before type" and "undeclared identifier: 'i'". Everything seems fine, I know i is declared and I don't seem to be missing any ; or ).
#include <stdlib.h>
#include <stdio.h>
#pragma warning(disable: 4996)
struct Room;
struct House;
struct Room
{
float width;
float length;
float height;
char *name;
};
struct House
{
char *address;
struct Room *rooms[10];
};
int main(int argc, char* argv[])
{
struct House h;
h.address = "10 Palace Road";
for(int i = 0; i < 10; i++) // 6 errors occur here
{
h.rooms[i] = NULL;
}
struct Room hall;
hall.width = 10;
hall.length = 12;
hall.height = 9;
hall.name = "Hall";
h.rooms[0] = &hall;
printHouse(h);
system("PAUSE");
return 0;
}
void printHouse(struct House house)
{
printf(house.address);
printf("\n\n\n");
for (int i=0; i<10; i++)
{
if (house.rooms[i] != NULL)
{
struct Room r = *house.rooms[i];
printf("Room # %d: %s", i+1, r.name);
}
}
}
printf(house.address);
should be
printf("%s",house.address);
Also you must declare your function printhouse, since you have defined it after main.
#include <stdlib.h>
#include <stdio.h>
#pragma warning(disable: 4996)
struct Room; //you don't need this
**EDIT**
struct House
{
char *address;
struct Room *rooms[10];
};
void printHouse(struct House house);
Declare House first then the function.
int i;
for (i = 0; i < 10; i++){
//...
}
In earlier versions of C, you cannot declare I inside a loop.
Some versions of C compilers do not allow 'i' to be declared in the loop. Try declaring 'i' separately at the beginning of 'main()'. That should work.

Resources