Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Quiz 3: Structs and Pointers - Prof. Richard Ord, Quizzes of Computer Science

A c++ quiz focusing on struct definitions, memory alignment, and pointer usage. Students are required to identify compiler errors, answer memory offset questions, and determine if constant folding can be performed. The document also includes reduced-c code examples.

Typology: Quizzes

2010/2011

Uploaded on 06/06/2011

koofers-user-bqs-1
koofers-user-bqs-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download C++ Quiz 3: Structs and Pointers - Prof. Richard Ord and more Quizzes Computer Science in PDF only on Docsity! Login name _________________ Quiz 3 Name ______________________ CSE 131 Signature ___________________ Winter 2011 Student ID __________________ 1. Given the following C++ definitions (similar to Reduced-C) struct S1 { int a; }; struct S2 { int a; }; void foo ( struct S2 &b ) { } struct S1 a; a call to foo( a ) passing in a as the actual argument will cause a compiler error. Why? Fix the function call foo( a ) below to pass a to foo() without causing a C++ compiler error. foo( _________________________________________ a ); Using Reduced-C syntax, define a variable named myPtr that is pointer to an array of 5 ints. This will take two lines of code. Show the memory layout of the following C struct definition taking into consideration the SPARC data type memory alignment restrictions discussed in class. Fill bytes in memory with the appropriate struct member/field name. For example, if member/field name p takes 4 bytes, you will have 4 p's in the appropriate memory locations. If the member/field is an array, use the name followed by the index number. For example, some number of p[0]s, p[1]s, p[2]s, etc. Place an X in any bytes of padding. Structs and unions are padded so the total size is evenly divisible by the most strict alignment requirement of its members. lower memory struct foo { char a; fubar: short b; short *c; short d[3]; double e; short f; char g; }; struct foo fubar; What is the offsetof( struct foo, f )? ________ What is the offsetof( struct foo, a )? ________ What is the sizeof( struct foo )? ________ If struct foo had been defined as union foo instead, what would be the sizeof( union foo )? ________ higher memory
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved