segmentation fault memory dumped - c

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

Related

Dynamic Memory allocation of array inside structure in C

I'm doing dining-philosopher problem in C for assignment. And got stuck very begining of my code.
I decided each philosopher to be structure, and forks to be int array.
But I can't use global variable in this assignment.
So, I have to include shared variable in philosopher structure to pass them for arguments of thread routine.
Here is my problem - how to include int array in structure if I can't know proper size of them when initializing?
My plan is just include pointer variable in structure then allocate array's address using &.
But It doesn't work :
#include <stdlib.h>
/* inside structure*/
typedef struct s_share {
int **forks;
} t_share;
/* outside structure */
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
int *forks;
int i;
i = 0;
/* malloc structure arrary philo, size = 10 */
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
/* malloc int arrary forks, size = 100 */
forks = (int *)malloc(sizeof(int) * 100);
while (i < 10)
{
philo[i].share->forks = &forks; //error
i++;
}
}
Output : segmentation fault
I tested share->forks size like this :
printf("size of forks : %ld\n", sizeof(philo->share->forks));
Output was 8.
It's enough size to store int * pointer.
Through this I know It's not the memory allocation problem.
Then what is problem? Can someone check this for me?
Edit :
When I try to malloc directly philo->share->forks, I got same error.
typedef struct s_share {
int *forks;
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
int *forks;
int i;
i = 0;
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
while (i < 10)
{
philo[i].share->forks = (int *)malloc(sizeof(int) * 100); //error
i++;
}
}
I thought it's because when philo initialized, sizeof operator calculated forks's memroy to be 8 - which required for pointer.
Is there something wrong?
Edit 2 :
To clear my question,
It's easy to solve this problem, if I write size of array in structure definition.
typedef struct s_share {
int forks[100];
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
but according to my assignmet I have to get philosopher's number from cmd. So I can't do that.
Above is simple version of my origin code
Sorry, Edit 2 is wrong :
typedef struct s_share {
int forks[100];
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
t_share *share;
int *forks;
int i;
i = 0;
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
while (i < 10)
{
philo[i].share->forks[i] = 1;
i++;
}
}
Output
zsh: segmentation fault ./a.out
I still got segfault when I write array size in struct definition.
I used calloc to initialize all member in my struct but same error occurs :
typedef struct s_share {
int **forks;
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
t_share *share;
int *forks;
int i;
i = 0;
philo = (t_philo *)calloc(10, sizeof(t_philo));
forks = (int *)calloc(100, sizeof(int));
while (i < 10)
{
philo[i].share->forks = &forks; //error
i++;
}
}
Edit 4:
I finally found error. It's because I didn't malloc 'share' struct in philo struct
typedef struct s_share {
int **forks;
} t_share;
typedef struct s_philo {
t_share *share;
} t_philo;
int main(void)
{
t_philo *philo;
int *forks;
int i;
i = 0;
philo = (t_philo *)malloc(sizeof(t_philo) * 10);
forks = (int *)malloc(sizeof(int) * 100);
while (i < 10)
{
philo[i].share = (t_share *)malloc(sizeof(t_share)); //here
philo[i].share.forks = &forks;
i++;
}
}
That one line -allocating struct share- solved problem.
Or, I can modify philo struct definition like this :
typedef struct s_philo {
t_share share; //not pointer, just struct
} t_philo;
In this way, I can automatically malloc struct share.
I got confused in this point. Thanks for helping!
this line
philo[i].share->forks
Is dereferencing the pointer 'share' which is not set. You called malloc and did not set any values, so the data inside your allocated buffer is 'garbage' data.
// add begin
t_share* new_share = (t_share*)malloc(sizeof(t_share));
philo[i].share = new_share;
// add end
// don't use &forks
philo[i].share->forks = forks; //error
i++;
// need forks++
forks++;

initialize struct from function call

Feel like im taking crazy pills just trying to do literally the simplest stuff I can imagine in C. Any help would be extremely appreciated. why does this work?
#include <stdio.h>
#include <stdlib.h>
#define Q_LIMT 100
typedef struct servers
{
int id;
int num_in_Q;
int server_status;
}SERVER;
void initialize(SERVER *s);
void initialize(SERVER *s)
{
int i=0,j=0;
for(i=0; i<2; i++) { //i=0; i=1
s[i].id = i; // 0, 1
s[i].num_in_Q = i*i + 1; // 1, 2
s[i].server_status = i+i + 2; // 2, 4
} // the bracket was missing
}
int main()
{
int i;
SERVER serv[2];
initialize(serv);
for(i=0; i<2; i++) {
printf("server[%d].id = %d\n", i, serv[i].id);
printf("server[%d].num_in_Q = %d\n", i, serv[i].num_in_Q);
but this throws away the initialized struct?
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
'''
int POINTERS_PER_INODE = 5;
struct Inode {
int valid;/* 0 == invalid, 1 == valid*/
int size;
int Blocks [5];
};
int InodeToString(char * InodeString, struct Inode iNode){
char * blockBuffer;
sprintf(InodeString, "%d", iNode.valid);
int i;
for (i = 0; i < POINTERS_PER_INODE; i++){
blockBuffer = malloc(8);
sprintf(blockBuffer, "%d", iNode.Blocks[i]); //no valid pointers yet
strcat(InodeString,blockBuffer);
free(blockBuffer);
}
return 0;
}
int initializeInode(struct Inode iNode){
int i;
for (i = 0; i < POINTERS_PER_INODE; i++){
iNode.Blocks[i] = -1; //no valid pointers yet
}
iNode.valid = 0; //initialized as invalid inode
return 0;
}
int main() {
struct Inode iNode1;
initializeInode(iNode1);
char * InodeString;
InodeString = malloc(20);
InodeToString(InodeString, iNode1);
printf("%s", InodeString);
free(InodeString);
iNode1.valid = 1;
InodeString = malloc(20);
InodeToString(InodeString, iNode1);
printf("%s", InodeString);
return 0;
}
This is test code btw, so the includes probably dont make sense. stack overflow says I dont have enough details so I guess I have to keep typing sentences. Let me know if theres any details that would make this more clear. its for a basic super simplified file system simulation project. it seemed in a previous version when I initialized the inode outside of the function, I was able to pass the string into the string function, assign it values, not use it as the return value and still end up on the other side of the function with an updated string.
As is normal in C, arguments to a function are passed by value. The object called iNode in initializeInode is local to that function, and changes to it have no effect on any other object in the program. If you want a function to modify an object that's local to the caller, you have to pass a pointer to it, and dereference that pointer to get at the caller's object.
So what you probably want is:
int initializeInode(struct Inode *iNode){
int i;
for (i = 0; i < POINTERS_PER_INODE; i++){
iNode->Blocks[i] = -1; //no valid pointers yet
}
iNode->valid = 0; //initialized as invalid inode
return 0;
}
int main() {
struct Inode iNode1;
initializeInode(&iNode1);
// ...
}

I should make a program to sort struct array but I don't have any idea to do this

after editing
the code is :
#include <stdio.h>
#include <string.h>
struct names//defining struct array with leinght of (3)
{
char firstname[50];
char lastname[50];
int age;
}n[4];
void show(struct names n[],int);//print the struct elements on the screen
void sortbyage(struct names n[],int);//sort the struct elements by age
int main(void)
{
struct names n[4];
int len=3;
strcpy(n[0].firstname,"david");
strcpy(n[0].lastname,"bekham");
n[0].age=18;
strcpy(n[1].firstname,"cristiano");
strcpy(n[1].lastname,"ronaldo");
n[1].age=20;
strcpy(n[2].firstname,"iron");
strcpy(n[2].lastname,"man");
n[2].age=16;
show(n,len);
sortbyage(n,len);
show(n,len);
return 0;
}
void show(struct names n[],int len)
{
for(int i=0;i<3;i++)
{
printf("%d-first name ==>%s\n",i+1,n[i].firstname);
printf("%d-last name ==>%s\n",i+1,n[i].lastname);
printf("%d-age==>%i\n",i+1,n[i].age);
}
}
void sortbyage(struct names n[],int len)
{
// int help=0;
for(int i=len-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
if(n[j].age>n[j+1].age)
{
n[4]=n[j];
n[j]=n[j+1];
n[j+1]=n[4];
}
}
}
}
after Editing it works
note : in run time it types in the last line this message : * process returned -1073741819 *)) how could I fix this and does it a big problem or it's don't matter???
You need to access as follows. Please modify your code some thing as below
if(n[j].age > n[j+1].age)
Hope this helps.
Edit:
Please modify the sortbyage() function as follows and it is working fine.
void sortbyage(struct names n[],int len)
{
struct names temp;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(n[i].age > n[j].age)
{
temp = n[i];
n[i] = n[j];
n[j] = temp;
}
}
}
}
Edit:
if(n[j].age>n[j+1].age)
{
help=n[j].age;
n[j].age=n[j+1].age;
n[j+1].age=help;
}

Bad access in Xcode

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 {

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