← Design Verification Interviews
Interview Question · Code Writing
Walking an Open Array in C
The Question
The interviewer asks
Write the C body for import "DPI-C" function int checksum(input byte unsigned pkt[], input int count);. It sums the first count bytes in the order the caller wrote them, and must work for pkt[0:63], pkt[63:0] and pkt[1:8].
You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
C receives an svOpenArrayHandle, not a byte pointer. Check count against svSize, and find the direction from svLeft and svRight. Then step from the left bound and read each element by its SystemVerilog index with svGetArrElemPtr1. Check each pointer for null. Never assume index 0 exists or comes first: in pkt[63:0] the first byte written is pkt[63].
extern "C" int checksum(const svOpenArrayHandle pkt, int count) {
if (svDimensions(pkt) != 1 || count < 0 || count > svSize(pkt, 1)) return -1;
const int left = svLeft(pkt, 1);
const int step = (left <= svRight(pkt, 1)) ? 1 : -1; // ascending or descending
int sum = 0;
for (int k = 0; k < count; k++) {
const unsigned char* p =
(const unsigned char*)svGetArrElemPtr1(pkt, left + k * step); // SV index
if (!p) return -1;
sum += *p;
}
return sum & 0xFF;
}Self-Check
Tick each key point you covered in your answer.
What Interviewers Listen For (Mid-Level)
Uses the bound queries before touching data, and walks by SystemVerilog index from the left bound.
What Interviewers Listen For (Senior)
Checks the count against the size and says element pointers live only for the call. Asks which actuals the tool accepts.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
WhyWhat IfBreak ItCompare
- Walking an Open Array in C
-
WhyWhy an Open Array Is a HandleL2 Mid-Level
Why does DPI give C a handle for an open array instead of a plain pointer?
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
The simulator's array may have any bounds, packed elements, four-state values or a layout C does not know. The handle hides that layout behind query and access functions.
svGetArrayPtrreturns a direct pointer only when the layout matches C, and null otherwise.Self-Check
Tick each key point you covered in your answer.
Half-Truth
"
svGetArrayPtralways returns a pointer to the bytes."It may return null, so code that uses it needs the element-by-element path as a fallback.
-
CompareOpen Arrays Compared with Sized ArraysL2 Mid-Level
Compare
input byte unsigned d[]withinput byte unsigned d[64]as a DPI formal.2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
A sized formal crosses as a plain pointer; the generated header gave
const unsigned char* d. Every actual must then have that exact size. An open formal crosses as a handle, so one C function serves any length and any bounds. Use the sized form only when the length is fixed by the protocol.Self-Check
Tick each key point you covered in your answer.
Misconception
"With
d[64], a 16-byte packet still works; C just reads 16."The actual must match the formal's shape. A smaller array is a type error at the call, not a short read.
Source IEEE 1800-2017 §35.5.6 (types of formal arguments)
-
What IfReturning a Variable-Length ResponseL2 Mid-Level
What if the C model must return a response whose length it only knows after computing it?
2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
C cannot grow a SystemVerilog array. The caller passes an output open array big enough for the longest response. C writes up to
svSizeelements and returns the count it used. If the response does not fit, C returns an error status instead of writing past the end.Self-Check
Tick each key point you covered in your answer.
Misconception
"C allocates the response and SystemVerilog adopts it."
Each side owns only its own memory. The array belongs to SystemVerilog, so C fills it but never replaces it.
Source IEEE 1800-2017 §35.5.1.4 (memory management)
-
Break ItKeeping an Element Pointer After the CallL2 Mid-Level
Break it: the C model saves the pointer from
svGetArrElemPtr1and reads the packet again in the next call. What goes wrong?2:30You have two and a half minutes. Speak or type, then press Get Feedback under your answer. No credits needed.
Speech-to-text runs in your browser. Chrome and Edge may send audio to their own speech service.
You can edit your answer before you send it. Feedback uses AI and checks your answer only against our written answer. No credits are used and your answer is not saved.
Reveal the Model Answer
The pointer is valid only during the call that produced it. Afterwards, the simulator may move, reuse or free that storage. The next read can return another packet's data or crash. Copy the bytes into C-owned storage before the call returns.
Self-Check
Tick each key point you covered in your answer.
Misconception
"The array is a module variable, so its address never changes."
The standard does not promise C a stable address. The simulator may pass a temporary copy, as it does for many argument types.
Source IEEE 1800-2017 Annex H (open arrays: pointers are valid only during the call)
What People Get Wrong
Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.
Misconception
"Index 0 is the first byte, as in a C array."
Indexes keep the caller's declaration. For [7:0], an assignment pattern puts its first value at index 7, so index 0 holds the last byte.
Run It
A packet declared [7:0] whose first written byte is the header 8'hA5.
header seen by C=7 FAIL: C read pkt[0], but the first byte written sits at pkt[7], the left indexSee the Fix
module tb;
import "DPI-C" function int c_header(input byte unsigned pkt[]);
// The header is the first byte written: 8'hA5.
byte unsigned pkt[7:0] = '{8'hA5, 1, 2, 3, 4, 5, 6, 7};
int hdr;
initial begin
hdr = c_header(pkt);
$display("header seen by C=%0h", hdr);
if (hdr != 'hA5) $display("FAIL: C read pkt[0], but the first byte written sits at pkt[7], the left index");
else $display("PASS: C started at svLeft and read the header");
$finish;
end
endmodule
Prints: header seen by C=a5 PASS: C started at svLeft and read the header
Tool as Truth
"Pass the queue of bytes straight in; open arrays take any array."
The standard allows a dynamic array or queue as the actual. Verilator 5.052 stops the build with "not yet supported", so use a fixed buffer and a count there.
Source IEEE 1800-2017 §35.5.6.1 (open arrays); ASIC Point engine notes (Verilator 5.052 DPI probe, 2026-11-08: a dynamic array or queue as an open-array actual is rejected)
Missed Corner
"Trust count; the caller knows the length."
A count larger than the array makes C read past its end. Lesson D1.4 refuses such a call with a status instead.
Source Lesson D1.4 review claims (a count of 4 on a three-element array returns status 2)