← Design Verification Interviews
Interview Question · Code Reading
A Packet Passed as a String
The Question
The interviewer asks
A colleague passes packet bytes to C as a string. What does this print?
import "DPI-C" function int c_length(input string s); // C: return strlen(s);
string payload = "";
byte unsigned bytes[3] = '{8'h41, 8'h00, 8'h42};
initial begin
foreach (bytes[i]) payload = {payload, string'(bytes[i])};
$display("%0d", c_length(payload));
endYou 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
It prints 2, not 3. A SystemVerilog string cannot hold a zero byte, so the 0x00 is dropped as the string is built. Even if it got through, C's strlen stops at the first zero. A string crosses as a borrowed const char* and is for text, such as names and file paths. Send binary data as a byte array with a count.
Self-Check
Tick each key point you covered in your answer.
What Interviewers Listen For (Junior)
Predicts 2 and says strings are for text, with binary data in a byte array.
What Interviewers Listen For (Mid-Level)
Explains both losses, the SystemVerilog rule and strlen, and mentions that C only borrows the string pointer.
Cross-Questions
Where the interviewer goes next. Open a node to practise that follow-up.
WhyWhat IfBreak ItCompare
- A Packet Passed as a String
-
WhyWhy a String Has No Zero ByteL1 Junior
Why can a SystemVerilog
stringnot hold a zero byte?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 standard says so: a string never contains the 0 character, and writing 0 into one character is ignored. That keeps it compatible with C strings, which end at the first zero.
Self-Check
Tick each key point you covered in your answer.
Misconception
"
s[1] = 8'h00;cuts the string short at index 1."The write is ignored instead. Our probe kept "AB" with length 2.
Source ASIC Point engine notes (Verilator 5.052 DPI probe, 2026-11-08: s = "AB"; s[1] = 8'h00 left s as AB, length 2)
-
Break ItA C Model That Kept a String PointerL2 Mid-Level
A C model stores the
const char*it receives for a test name and prints it at the end of the test. Sometimes it prints garbage. Why?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 string argument reaches C as a pointer to memory the simulator owns, valid only while the call runs. After the call returns, the simulator may reuse or free that memory, so the stored pointer can point at anything. Copy the text into storage the C side owns during the call.
The Life of a String Across the CallArrows show where the pointer points and what C does with it. The text lives in the simulator's buffer; the copy in keptis the only thing that survives the call.Illustrative sequenceCallValueReturnWaiting- Pass
The testbench calls
protocol_code(cfg). C gets a pointer to the bytesA X Iand a terminating zero, nothing more.Follow the calls, values and waits
- data AXI: string cfg → const char* name
- Read during the call
strcmpreads the bytes and the function returns 2. The pointer has done its job.Follow the calls, values and waits
- data AXI: const char* name → protocol_code
- return 2: protocol_code → const char* name
- Copy what you must keep
remember_namecopies the bytes intokept, storage the model owns. After this the model never touchesnameagain.Follow the calls, values and waits
- data AXI: const char* name → kept[32]
- The call returns
The testbench assigns a new value to
cfg. A keptnamepointer would now read whatever the simulator put there;keptstill says AXI.
Read the diagram as text
- string cfg: "AXI" in the simulator's own buffer
- const char* name: points at that buffer while the call lasts
- protocol_code: strcmp during the call, returns 2
- kept[32]: remember_name copies the bytes; the model keeps this
- cfg = "changed…": after the call the simulator reuses its buffer
- string cfg → const char* name: passes
- const char* name → protocol_code: read now
- const char* name → kept[32]: copied now
- cfg = "changed…" → const char* name: stale pointer
Borrow during the call, copy to keep. A pointer that outlives the call points at memory the simulator may have changed, moved or freed.
Self-Check
Tick each key point you covered in your answer.
Misconception
"The string lives as long as the SystemVerilog variable does."
The pointer refers to the simulator's copy for that call, not to the variable, so a later change or reuse invalidates it.
Source IEEE 1800-2017 Annex H.7.2 (argument passing: ownership of string storage)
- Pass
-
What IfCopying an Object That Holds a ChandleL2 Mid-Level
Asked if the C object sits in a transaction class
What if a model class holds a
chandle, and someone copies the object withnew thisor acopy()method?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 copy gets the same address, so both objects drive one C model. An update through either is seen by both. If the copy needs its own state, call a C clone function that allocates a new struct. Then decide which object deletes which model.
Self-Check
Tick each key point you covered in your answer.
Misconception
"The copied object now has its own C model."
A shallow copy copies field values, and a chandle's value is an address. The twin adds 100 through the copy and the original shows 105.
Run It
A model object with a C accumulator, copied after adding 5.
RUNVerilator + C/C++ over DPI · simulator serviceExpected:a=105 b=105FAIL: the copy shares one C model, because copying a chandle copies only the addressSee the Fix
module tb; import "DPI-C" function chandle acc_new(); import "DPI-C" function chandle acc_clone(input chandle h); import "DPI-C" function int acc_add(input chandle h, input int v); class model; chandle h; function new(); h = acc_new(); endfunction function model copy(); model c = new this; // a shallow copy of every field c.h = acc_clone(h); // the fix: the copy gets its own C state return c; endfunction endclass model a = new, b; initial begin void'(acc_add(a.h, 5)); b = a.copy(); void'(acc_add(b.h, 100)); $display("a=%0d b=%0d", acc_add(a.h, 0), acc_add(b.h, 0)); if (acc_add(a.h, 0) != 5) $display("FAIL: the copy shares one C model, because copying a chandle copies only the address"); else $display("PASS: each object owns its own C state"); $finish; end endmodulePrints:
a=5 b=105PASS: each object owns its own C state -
CompareWhy a Chandle and Not an IntegerL2 Mid-Level
Why store the C pointer in a
chandlerather than in alongint?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
chandlealways has the size of a pointer on the machine that runs the simulation. The language only lets you assign it, compare it with null and pass it to C. That stops SystemVerilog code from doing arithmetic on it or inventing values. Alongintwould work by accident on a 64-bit host and offer none of those guarantees.Self-Check
Tick each key point you covered in your answer.
Misconception
"You can read the struct fields through the
chandlein SystemVerilog."It is opaque: only C code can dereference it, through accessor functions you import.
Source IEEE 1800-2017 §6.14
What People Get Wrong
Common wrong answers and why they fail. Run It shows the wrong belief failing on a real simulator.
Misconception
"A string carries any bytes, and C finds the length with strlen."
The zero byte is lost before the call. The twin shows SystemVerilog length 2 for three appended bytes.
Run It
Three bytes, one of them zero, appended to a string and measured in C.
SystemVerilog length=2, C length=2 FAIL: the zero byte never reached C; a string cannot carry binary dataSee the Fix
module tb;
import "DPI-C" function int c_count(input byte unsigned d[], input int n);
byte unsigned bytes[3] = '{8'h41, 8'h00, 8'h42};
string payload = "";
int got;
initial begin
// The fix: binary data travels as a byte array with a count.
got = c_count(bytes, 3);
$display("C received %0d bytes", got);
if (got != 3) $display("FAIL: the zero byte never reached C; a string cannot carry binary data");
else $display("PASS: all three bytes reached C, zero included");
$finish;
end
endmodule
Prints: C received 3 bytes PASS: all three bytes reached C, zero included
Misconception
"C should free the const char* when it is done with it."
The simulator owns that memory. Freeing it corrupts the simulator's heap, often with a crash much later.
Source IEEE 1800-2017 §35.5.1.4 (memory management)
Half-Truth
"A string and a chandle are both pointers, so either can hold a C object."
A string is text that C only borrows during the call. A chandle is the only type that carries a C pointer SystemVerilog may keep.