# Const pointer with large array size

**URL:** <https://ziggit.dev/t/const-pointer-with-large-array-size/4294>\
**Category:** Help\
**Tags:** language, zig-0-12\
**Created:** [May 12, 2024, 7:04pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294 "2024-05-12T19:04:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmamud](https://ziggit.dev/user_avatar/ziggit.dev/fmamud/32/2352_2.png) [@fmamud](https://ziggit.dev/u/fmamud)\
**Post date:** [May 12, 2024, 7:04pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/1 "2024-05-12T19:04:47Z")

</div>

Hey, please could you help me to understand what is the expected behaviour of code snippet bellow?

```zig
const t: *const [1000]u8 = "asdf";
std.debug.print("string: {s}\n", .{t});

```

I’m using a scaffold build file from `zig init`, and using `zig build && ./zig-out/bin/main` to test it.

Compilation is fine with no errors. Should I get a panic instead or it’s expected?

I do see different kind of overflow data 😀 depends on release mode. Please see few examples:

| Buil type cmd | Output |
| --- | --- |
| `zig build` | string: asdfinteger overflowattempt to cast negative value to unsigned integerh�Fstring: {s} invalid error codethread {} panic: {s} Panicked during a panic. Aborting. (msg truncated)@memcpy arguments aliasswitch on corrupt valueunable to install segfault handler, maybe adjust have\_segfault\_handling\_support in std/debug.zigreached unreachable codeDeadlock detectedincorrect alignment’noreturn’ function returnedattempt to use null valueinteger cast truncated bitscast causes pointer to be null\_\_TEXT\_\_unwind\_info\_\_eh\_frame{s}:{d}:{d}???:?:?: 0x{x} in {s} ({s}) ^???for loop over objects with non-equal lengths@memcpy arguments have non-equal lengths\_\_DWARF\_\_debug\_info\_\_debug\_abbrev\_\_debug\_str\_\_debug\_str\_offsets\_\_debug\_line\_\_debug\_line\_str\_\_debug\_ranges\_\_debug\_loclists\_\_debug\_rnglists\_\_debug\_addr\_\_debug\_names\_\_debug\_frame\_\_eh\_frame\_h |
| `zig build --release=small` | string: asdf00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupported 8888@`��0`��04X���� t |
| `zig build --release=fast` | string: asdf(msg truncated)failed to set noop SIGPIPE handler: {s}00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupportedDiskQuotaFileTooBigInputOutputNoSpaceLeftDeviceBusyInvalidArgumentAccessDeniedBrokenPipeSystemResourcesOperationAbortedNotOpenForWritingLockViolationWouldBlockConnectionResetByPeerUnexpectedOverflowInvalidUtf8Utf8ExpectedContinuationUtf8OverlongEncodingUtf8EncodesSurrogateHalfUtf8CodepointTooLargeUtf8InvalidStartByteTruncatedInputUtf8CannotEncodeSurrogateHalfCodepointTooLargeMissingDebugInfoUnsupportedOperatingSystemInvalidDebugInfoOutOfMemorySharingViolationPathAlreadyExistsFileNotFoundPipeBusyNameTooLongInvalidWtf8BadPathNameNetworkNotFoundAntivirusInterferenceSymLinkLoopProcessFdQuotaExceededSystemFdQuotaExceededNoDeviceIsDirNotDirFileLocksNotSupportedFileBusy |
| `zig build --release=safe` | string: asdf\_\_TEXT\_\_unwind\_info\_\_eh\_frame\_\_DWARF\_\_debug\_info\_\_debug\_abbrev\_\_debug\_str\_\_debug\_str\_offsets\_\_debug\_line\_\_debug\_line\_str\_\_debug\_ranges\_\_debug\_loclists\_\_debug\_rnglists\_\_debug\_addr\_\_debug\_names\_\_debug\_frame\_\_eh\_frame\_hdr(msg truncated)failed to set noop SIGPIPE handler: {s}00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupportedDiskQuotaFileTooBigInputOutputNoSpaceLeftDeviceBusyInvalidArgumentAccessDeniedBrokenPipeSystemResourcesOperationAbortedNotOpenForWritingLockViolationWouldBlockConnectionResetByPeerUnexpectedMissingDebugInfoUnsupportedOperatingSystemInvalidDebugInfoOutOfMemorySharingViolationPathAlreadyExistsFileNotFoundPipeBusyNameTooLongInvalidUtf8InvalidWtf8BadPathNameNetworkNotFoundAntivirusInterferenceSymLinkLoopProcessFdQuotaExc |

Many thanks in advance.

Best,  
Felipe

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [May 12, 2024, 7:50pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/2 "2024-05-12T19:50:07Z")

</div>

It should give you a compiler error, something like

```zig
test.zig:10:29: error: expected type '*const [1000]u8', found '*const [4:0]u8'
 const t: *const [1000]u8 = "asdf";
                            ^ ~~~~~

```

This looks like it could be a compiler bug.  
What version of zig you are using?

---

<div class="post-metadata">

**Author:** ![fmamud](https://ziggit.dev/user_avatar/ziggit.dev/fmamud/32/2352_2.png) [@fmamud](https://ziggit.dev/u/fmamud)\
**Post date:** [May 12, 2024, 7:52pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/3 "2024-05-12T19:52:06Z")

</div>

@IntegratedQuantum I’m using `0.12.0` installed by homebrew.

```zig
$ zig version
0.12.0

$ sw_vers
ProductName: macOS
ProductVersion: 14.4.1
BuildVersion: 23E224

```

---

<div class="post-metadata">

**Author:** ![fmamud](https://ziggit.dev/user_avatar/ziggit.dev/fmamud/32/2352_2.png) [@fmamud](https://ziggit.dev/u/fmamud)\
**Post date:** [May 12, 2024, 7:59pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/4 "2024-05-12T19:59:59Z")

</div>

btw I was expecting an compiler error like you’ve sent. Currently I’m using on MacOS ARM M1 arch.

Have you using same version but diff arch probably?

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [May 12, 2024, 8:02pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/5 "2024-05-12T20:02:17Z")

</div>

I was using a slightly older version (0.12.0-dev.3518+d2be725e4).  
I just tried zig 0.12 and I get the same behavior.

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [May 12, 2024, 8:04pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/6 "2024-05-12T20:04:18Z")

</div>

It’s probably best if you report this on github.

---

<div class="post-metadata">

**Author:** ![fmamud](https://ziggit.dev/user_avatar/ziggit.dev/fmamud/32/2352_2.png) [@fmamud](https://ziggit.dev/u/fmamud)\
**Post date:** [May 12, 2024, 8:05pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/7 "2024-05-12T20:05:06Z")

</div>

Yeah, I’m actually doing that right now… afterwards I’ll post the link over here.

Many thanks for the confirmation @IntegratedQuantum .

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [May 12, 2024, 8:16pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/8 "2024-05-12T20:16:55Z")

</div>

Yes, it’s a bug.

This compiles and panics (since 995 bytes are undefined):

```zig
    const ts: *const [4:0]u8 = &[4:0]u8{ 'a', 's', 'd', 'f' };
    const t: *const [1000]u8 = ts;

```

This does not compile:

```zig
    const ts: *const [4]u8 = &[4]u8{ 'a', 's', 'd', 'f' };
    const t: *const [1000]u8 = ts;

```

error: `expected type '*const [1000]u8', found '*const [4]u8'`

---

<div class="post-metadata">

**Author:** ![fmamud](https://ziggit.dev/user_avatar/ziggit.dev/fmamud/32/2352_2.png) [@fmamud](https://ziggit.dev/u/fmamud)\
**Post date:** [May 12, 2024, 8:26pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/9 "2024-05-12T20:26:28Z")

</div>

Many thanks for confirming this @dimdin and to catch it by other example, which doesn’t not compile here as well.

---

<div class="post-metadata">

**Author:** ![fmamud](https://ziggit.dev/user_avatar/ziggit.dev/fmamud/32/2352_2.png) [@fmamud](https://ziggit.dev/u/fmamud)\
**Post date:** [May 12, 2024, 8:27pm UTC](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/10 "2024-05-12T20:27:11Z")

</div>

Bug reported it.

> <https://github.com/ziglang/zig/issues/19952>
>
> \### Zig Version
> 
> 0.12.0
> 
> \### Steps to Reproduce and Observed Behavior
> 
> I was tes…ting the code snippet bellow and I was expecting a compiler error, but it doesn't happened.
> 
> \`\`\`zig
> const t: \*const \[1000\]u8 = "asdf";
> std.debug.print("string: {s}\\n", .{t});
> \`\`\`
> 
> My current targeting is \`arm64\`:
> 
> \`\`\`
> $ sw\_vers
> ProductName: macOS
> ProductVersion: 14.4.1
> BuildVersion: 23E224
> \`\`\`
> 
> I do see different kind of overflow data depending on release mode. Please see few examples:
> 
> | Build type cmd | Output |
> |--- | ---|
> |\`zig build\` | string: asdfinteger overflowattempt to cast negative value to unsigned integerh�Fstring: {s} invalid error codethread {} panic: {s} Panicked during a panic. Aborting. (msg truncated)@memcpy arguments aliasswitch on corrupt valueunable to install segfault handler, maybe adjust have\_segfault\_handling\_support in std/debug.zigreached unreachable codeDeadlock detectedincorrect alignment'noreturn' function returnedattempt to use null valueinteger cast truncated bitscast causes pointer to be null\_\_TEXT\_\_unwind\_info\_\_eh\_frame{s}:{d}:{d}???:?:?: 0x{x} in {s} ({s}) ^???for loop over objects with non-equal lengths@memcpy arguments have non-equal lengths\_\_DWARF\_\_debug\_info\_\_debug\_abbrev\_\_debug\_str\_\_debug\_str\_offsets\_\_debug\_line\_\_debug\_line\_str\_\_debug\_ranges\_\_debug\_loclists\_\_debug\_rnglists\_\_debug\_addr\_\_debug\_names\_\_debug\_frame\_\_eh\_frame\_h|
> |\`zig build --release=small\` | string: asdf00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupported 8888@\`��0\`��04X���� t|
> |\`zig build --release=fast\` | string: asdf(msg truncated)failed to set noop SIGPIPE handler: {s}00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupportedDiskQuotaFileTooBigInputOutputNoSpaceLeftDeviceBusyInvalidArgumentAccessDeniedBrokenPipeSystemResourcesOperationAbortedNotOpenForWritingLockViolationWouldBlockConnectionResetByPeerUnexpectedOverflowInvalidUtf8Utf8ExpectedContinuationUtf8OverlongEncodingUtf8EncodesSurrogateHalfUtf8CodepointTooLargeUtf8InvalidStartByteTruncatedInputUtf8CannotEncodeSurrogateHalfCodepointTooLargeMissingDebugInfoUnsupportedOperatingSystemInvalidDebugInfoOutOfMemorySharingViolationPathAlreadyExistsFileNotFoundPipeBusyNameTooLongInvalidWtf8BadPathNameNetworkNotFoundAntivirusInterferenceSymLinkLoopProcessFdQuotaExceededSystemFdQuotaExceededNoDeviceIsDirNotDirFileLocksNotSupportedFileBusy|
> |\`zig build --release=safe\` | string: asdf\_\_TEXT\_\_unwind\_info\_\_eh\_frame\_\_DWARF\_\_debug\_info\_\_debug\_abbrev\_\_debug\_str\_\_debug\_str\_offsets\_\_debug\_line\_\_debug\_line\_str\_\_debug\_ranges\_\_debug\_loclists\_\_debug\_rnglists\_\_debug\_addr\_\_debug\_names\_\_debug\_frame\_\_eh\_frame\_hdr(msg truncated)failed to set noop SIGPIPE handler: {s}00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupportedDiskQuotaFileTooBigInputOutputNoSpaceLeftDeviceBusyInvalidArgumentAccessDeniedBrokenPipeSystemResourcesOperationAbortedNotOpenForWritingLockViolationWouldBlockConnectionResetByPeerUnexpectedMissingDebugInfoUnsupportedOperatingSystemInvalidDebugInfoOutOfMemorySharingViolationPathAlreadyExistsFileNotFoundPipeBusyNameTooLongInvalidUtf8InvalidWtf8BadPathNameNetworkNotFoundAntivirusInterferenceSymLinkLoopProcessFdQuotaExc|
> 
> 
> \### Expected Behavior
> 
> A compiler error, like before on \[slightly older version 0.12.0-dev.3518+d2be725e4\](https://ziggit.dev/t/const-pointer-with-large-array-size/4294/5?u=fmamud).
> 
> \`\`\`
> test.zig:10:29: error: expected type '\*const \[1000\]u8', found '\*const \[4:0\]u8'
> const t: \*const \[1000\]u8 = "asdf";
> ^ ~~~~~
> \`\`\`
