Building a struct within a struct correctly

B

bernd

Hi folks,

the code below "runs" with the following message:

Before structure assignment
Bus Error(coredump)

What I am trying to do is to define an "outer" structure containing an
inner one and then pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.

Cheers


Bernd

#include <stdio.h>

main () {

int debug = 1 ;

typedef struct inner {

long var1 ;
char text[5] ;

} instruct ;


typedef struct outer {

long count ;
struct instruct *toinstruct ;

} outstruct ;


instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;

in = malloc( in_size ) ;

outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }

out->toinstruct = in ;

if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }

int out_size ;
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;

if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }

}
 
F

Fred

Hi folks,

the code below "runs" with the following message:

Before structure assignment
Bus Error(coredump)

What I am trying to do is to define an "outer" structure containing an
inner one and then pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.

Cheers

Bernd

#include <stdio.h>

main () {

  int debug = 1 ;

   typedef struct inner {

     long var1 ;
     char text[5] ;

   } instruct ;

   typedef struct outer {

     long count ;
     struct instruct *toinstruct ;

'instruct' is a typedef - no need fot the struct keyword here.
This should be
instruct *toinstruct;

Also, better to place the typdefs outside the main function.
   } outstruct ;

   instruct *in ;
   int in_size ;
   in_size = sizeof( *in ) ;

   in = malloc( in_size ) ;

   outstruct *out ;

You have declared 'out' to be a pointer to a outstruct.
You have not pointed it to anything yet.
   if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }

   out->toinstruct = in ;

Now you try to dereference the uninitialized 'out' pointer.
Crash!
 
B

Ben Bacarisse

bernd said:
What I am trying to do is to define an "outer" structure containing an
inner one and then...

This confused me at first. The one structure just contains a pointer
to the other. One structure contained in another would look
different.
pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure).

I can't understand this. I can tell what is wrong with your code, but
the description of what you want is confusing.
But obviously I am
doing something wrong.
main () {

int debug = 1 ;

typedef struct inner {
long var1 ;
char text[5] ;
} instruct ;

typedef struct outer {
long count ;
struct instruct *toinstruct ;
} outstruct ;

instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;

in = malloc( in_size ) ;

It is a good idea to test the result. It is unlikely to matter here,
but it is a good habit to get into in C.
outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }

out->toinstruct = in ;

Bang. At this point you can't use 'out' at all. It is an
uninitialised variable. You must set it to something before you do
anything with it. Since it is a pointer to a struct, you should set
it to point to one. This you go on to do...
if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }

int out_size ;
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;

Now, provided the malloc worked (out != NULL) you could do the

out->toinstruct = in ;

but not earlier.
 
T

Tarique

bernd said:
Hi folks,

the code below "runs" with the following message:

Before structure assignment
Bus Error(coredump)

What I am trying to do is to define an "outer" structure containing an
inner one

Ok

and then pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.

This is Quite confusing !
Cheers


Bernd

#include <stdio.h>

main () {

int debug = 1 ;

typedef struct inner {

long var1 ;
char text[5] ;

} instruct ;


typedef struct outer {

long count ;
struct instruct *toinstruct ;

} outstruct ;


instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;

in = malloc( in_size ) ;

outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure
assignment\n" ) ; }

out->toinstruct = in ;

if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }

int out_size ;
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;

if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }

}
From - Wed


struct stackelement{

int etype;/*etype equals INT,FLOAT,STRING*/
/*depending on the type of the */
/*corresponding element */
union{
int iVal;
float fVal;
char pVal[ARRAYSIZE];
}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE]; /*Array Of Structures */
};


void pop( struct stack *p, struct stackelement *se )
{
if( p->top == -1 )
puts( "Underflow\n" );
else
{
if( se->etype == INT )
printf( "Deleted Integer is %d\n", p->item[(p->top)--].element.iVal );

else if( se->etype == FLOAT )
printf( "Deleted Float is %f\n" , p->item[(p->top)--].element.fVal );

else if( se->etype == STRING )
printf( "Deleted String is %s\n" , p->item[(p->top)--].element.pVal );

else
puts("Unknown Error");
}
return;
}



You are probably trying to do something similar.Its an unrelated code
segment but hope you can get an idea.
 
B

Barry Schwarz

Hi folks,

the code below "runs" with the following message:

Before structure assignment
Bus Error(coredump)

What I am trying to do is to define an "outer" structure containing an
inner one and then pass the address of the inner structure to the
pointer within the outer structure pointing to it's inner structure
(so that I can read in data into the outer structure and being able to
read it immediately from the inner structure). But obviously I am
doing something wrong.

You apparently don't want the inner structure inside the outer
structure. Your discussion about a pointer above and your code are
consistent in that the outer structure contains a pointer to the inner
structure, not the structure itself.
#include <stdio.h>

main () {

int main(void) please.
int debug = 1 ;

typedef struct inner {

long var1 ;
char text[5] ;

} instruct ;


typedef struct outer {

long count ;
struct instruct *toinstruct ;

Did your compiler not issue a diagnostic here? If so, you need to up
the warning level or get a better one.

There is no type known as struct instruct. There is a type known as
struct inner and it has an "alias" known as insrtuct. Use one or the
other but do not try to combine them.
} outstruct ;


instruct *in ;
int in_size ;
in_size = sizeof( *in ) ;

Not a real problem here but be aware that sizeof returns a size_t, not
an int.
in = malloc( in_size ) ;

You should check that malloc succeeded before using the value in
variable in.
outstruct *out ;
if ( debug == 1 ) { ( void ) fprintf(stderr, "Before structure

Why are you casting the return from fprintf?
assignment\n" ) ; }

out->toinstruct = in ;

You are not allowed to evaluate a variable whose value is
indeterminate. It invokes undefined behavior. An immediate bus error
is actually one of the preferable manifestations of undefined
behavior.
if ( debug == 1 ) { ( void ) fprintf(stderr, "After structure
assignment\n" ) ; }

int out_size ;

Unless you have a C99 compiler, you need to put your declarations
before your statements. Even if you do have one, sticking to C90
rules will let more people help you.
out_size = sizeof( *out ) ;
out = malloc( out_size ) ;

This is a little late. If you move it in front of your first use of
out->, things will work a lot better.
if ( debug == 1 ) { ( void ) fprintf(stderr, "Sizes: inner: %d -
outer: %d\n", in_size, out_size ) ; }

}


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top