Trouble concatenating strings using an allocator
ziggit.dev·3d
📖ONScripter
Preview
Report Post

December 24, 2025, 1:39am 1

Hello Everyone,

newbie here coming to zig from a high level language like python and I am trying to concatenate strings at the runtime using an allocator and have the below function, however the call is running into error{OutOfMemory}![]u8.

const std = @import("std");

fn cocat_strings(str1: []const u8, str2: []const u8) u8 {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();
const combined = std.mem.concat(allocator, u8, &.{ str1, str2 });
defer allocator.free(combined);
return combined;
}

the caller looks like below

pub fn main() void {

const name: *const [3:0]u8 = "Som";
cocat_strings(name, name);  // looking for a result like "SomSom"
}

Any help with some explanation w…

Similar Posts

Loading similar posts...