Q1 Consider the following structure declaration: struct my_structure { char c1;
ID: 3563534 • Letter: Q
Question
Q1
Consider the following structure declaration:
struct my_structure { char c1; int i1, i2; double d1; short a1[3]; }
Assuming every type is aligned to its natural width (e.g. 1 byte types to 1 byte boundaries, 2 byte types to 2 byte boundaries, etc.), what are the offsets of each element in this structure? What is the size of the structure itself?Could the order of the elements in the structure be rearranged to reduce the size of the structure? Justify your answers.
Q2
Consider a function exttt{foo} in the standard x86 calling convention, including the use of a frame pointer, that has three integer arguments, Calls another a function exttt{bar} with two integer arguments, has 3 local variables, an integer and two character pointers, and uses only the registers %ebx and %ecx.
What items will be stored in the activation record for this function, ignoring alignment requirements and return addresses and what actions would the function foo have to perform prior to calling the function bar?
Explanation / Answer
C arrays allow you to define type of variables that can hold several data items of the same kind but structure is another user defined data type available in C programming, which allows you to combine data items of different kinds.
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. The format of the struct statement is this:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
For Example
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s ", Book1.title);
printf( "Book 1 author : %s ", Book1.author);
printf( "Book 1 subject : %s ", Book1.subject);
printf( "Book 1 book_id : %d ", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s ", Book2.title);
printf( "Book 2 author : %s ", Book2.author);
printf( "Book 2 subject : %s ", Book2.subject);
printf( "Book 2 book_id : %d ", Book2.book_id);
return 0;
}
and output:
Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700
This might help you:
http://www.tutorialspoint.com/cprogramming/c_structures.htm
http://stackoverflow.com/questions/3884382/memory-allocation-for-structures
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.