473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Memory allocation for Structure Pointer within Structure Pointer

3
typedef struct siblings {
char sibname[20];
char gender;
}sib;


typedef struct employee {
char empid[10];
char empname[20];
char status;
float sal;
sib ** s;
}emp;

typedef struct stackemp {
int size;
int ptrPos;
emp ** e;
}stack;

function myfunction(stack * s) {
(s->e[s->size]) = (emp *) malloc(sizeof(emp)); //this line works without any errors

(s->e[s->size]->s[0]) = (sib *)malloc(sizeof(sib)); //this line displays segmenation fault during run time
.....
....
}

This code compiles without errors but displays "Segmentation Fault" during runtime. How do I assign memory for the double pointer 's'
Dec 2 '11 #1
6 1746
weaknessforcats
9,208 Expert Mod 8TB
Where are you allocating s->e ?

That is, I don't see where you are allocating an array of emp*. Not doing that makes e[s->size] invalid.
Dec 2 '11 #2
johny10151981
1,059 1GB
to add note with weaknessforcats

Expand|Select|Wrap|Line Numbers
  1. s->e[s->size]->s[0]
The length of e is s->size; and remember in c/c++ array always start with zero and end in length-1, i.e. in here s->size-1; s->size th point is definitely invalid
Dec 2 '11 #3
djcm75
3
I did not post my entire code

allocation for s->e ? complies & runs perfectly

s->e[s->size] = (emp *)malloc(sizeof(emp));
//where s->size has already been incremented

I am having problem only with
(s->e[s->size]->s[0]) = (sib *)malloc(sizeof(sib));
no compilation during run time i get sementation fault
Dec 2 '11 #4
weaknessforcats
9,208 Expert Mod 8TB
malloc(sizeof(sib)) should allocate 4 bytes on a 32-bit OS since sib is a pointer.
Dec 2 '11 #5
donbock
2,426 Expert 2GB
Do you verify that none of your malloc calls returned NULL?
You shouldn't dereference any pointer until you have reason to believe it is not NULL.
Dec 3 '11 #6
djcm75
3
Solved

typedef struct siblings {
char sibname[20];
char gender;
}sib;


typedef struct employee {
char empid[10];
char empname[20];
char status;
float sal;
sib ** s;
}emp;

typedef struct stackemp {
int size;
int ptrPos;
emp ** e;
}stack;

function myfunction(stack * s) {
(s->e[s->size]) = (emp *) malloc(sizeof(emp)); //this line works without any errors

(s->e[s->size]->s) = malloc(sizeof(s->e[s->size]->s[0]));
(s->e[s->size]->s[0]) = malloc(sizeof(sib));
(s->e[s->size]->s[1]) = malloc(sizeof(sib));

.....
....
}
Dec 7 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

50
by: arunajob | last post by:
Hi all, If I have a piece of code something like this void main(void) { char * p1="abcdefghijklmn"; ............................................. }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.