#Error Message in Sorting Function

1 messages · Page 1 of 1 (latest)

dreamy kiln
#

Hello, I have my code for a selection sort in Zig attached below.

const std = @import("std");
pub extern fn scanf(noalias [*c]const u8, ...) c_int;
const print = std.debug.print;

pub fn main() !void {
    var arr = [_]u8{ 0, 0, 0, 0, 0, 0 };

    // Prompt the user to enter 6 values
    print("Please enter 6 values.\n", .{});

    // Read the values from the user
    _ = scanf("%d %d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5]);

    // Sort the values using the selection sort algorithm
    // var sorted_arr = arr;
    // _ = sorted_arr;
    // sortVal(&sorted_arr);

    sortVal(&arr);

    // Copy the sorted values back to the original array
    // var i: u8 = 0;
    // for (sorted_arr) |elem| {
    //     arr[i] = elem;
    //     i += 1;
    // }

    // Print the sorted values
    print("The sorted values are: {}, {}, {}, {}, {}, {}\n", .{ arr[0], arr[1], arr[2], arr[3], arr[4], arr[5] });
}

// Sorts an array of 6 unsigned 8-bit integers in ascending order
pub fn sortVal(arr_sort: *[6]u8) void {
    const n: u8 = 6;

    var min_idx: u8 = 0;
    var temp: u8 = 0;
    var i: u8 = 0;

    while (n - 1 > i) : (i += 1) {
        min_idx = i;
        var j: u8 = i + 1;

        // Find the index of the minimum element in the unsorted portion of the array
        while (n > j) : (j += 1) {
            if (arr_sort[j] < arr_sort[min_idx]) {
                min_idx = j;
            }
        }

        // Swap the minimum element with the first element in the unsorted portion of the array
        temp = arr_sort[min_idx];
        arr_sort[min_idx] = arr_sort[i];
        arr_sort[i] = temp;
    }
}

Running this gives me the following output:

$ zig run sorting_mess.zig
Please enter 6 values.
1 2 3 4 8 6
The sorted values are: 1, 2, 3, 4, 6, 8
[1]    33396 abort      zig run sorting_mess.zig

This doesn't happen when I copy the array to modify it. Why? Not thread safe? Is it a memory safety thing? I'm a beginner

supple sandal
#

yooooo @dreamy kiln fancy meeting you here

#

can't reproduce? worksonmymachine

#

Did you link libc?

dreamy kiln
dreamy kiln
#

I'm on 0.11 if that makes any difference

supple sandal
#

hmmmm

#

might be a macos issue? I'm unsure

#

what does valgrind say?

dreamy kiln
#

valgrind?

subtle trench
#

What is the problem? Does it just crash end of program?

supple sandal
#

valgrind is a tool for finding errors in low-level programs

supple sandal
#

you can install it to get more information on what went wrong

dreamy kiln
supple sandal
#

oh

#

I see your issue

dreamy kiln
#
const std = @import("std");
pub extern fn scanf(noalias [*c]const u8, ...) c_int;
const print = std.debug.print;

pub fn main() !void {
    var arr = [_]u8{ 0, 0, 0, 0, 0, 0 };

    print("Please enter 6 values.\n", .{});

    // Read the values from the user
    _ = scanf("%d %d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5]);

    // Sort the values using the selection sort algorithm
    var sorted_arr = arr;
    sortVal(&sorted_arr);

    var i: u8 = 0;
    for (sorted_arr) |elem| {
        arr[i] = elem;
        i += 1;
    }

    print("The sorted values are: {}, {}, {}, {}, {}, {}\n", .{ arr[0], arr[1], arr[2], arr[3], arr[4], arr[5] });
}

pub fn sortVal(arr_sort: *[6]u8) void {
    const n: u8 = 6;

    var min_idx: u8 = 0;
    var temp: u8 = 0;
    var i: u8 = 0;

    while (n - 1 > i) : (i += 1) {
        min_idx = i;
        var j: u8 = i + 1;

        while (n > j) : (j += 1) {
            if (arr_sort[j] < arr_sort[min_idx]) {
                min_idx = j;
            }
        }

        temp = arr_sort[min_idx];
        arr_sort[min_idx] = arr_sort[i];
        arr_sort[i] = temp;
    }
}

this code works fine

supple sandal
#

the correct format specifier for u8 is %hhd

dreamy kiln
#

:woag:

supple sandal
#

not %d, which is for int

dreamy kiln
#

ahhhh

supple sandal
#

h stands for "half"

#

sorry, this is truly a C moment

dreamy kiln
#

is there a built-in alternative to scanf as part of zig std?

supple sandal
#

also, if you'd like, use std.c.scanf

dreamy kiln
supple sandal
#

to read a line

#

then std.mem.tokenize to iterate over the fields and set accordingly

#

would you like to write something like that? if you have issues you can ask here

dreamy kiln
#

main.zig:11:10: error: root struct of file 'c' has no member named 'scanf'

dreamy kiln
supple sandal
#

huh, interesting

#

I could have sworn...

dreamy kiln
#

you're on 0.12 I'm guessing

supple sandal
#

nah it straight up doesn't exist

#

my bad I lied to you

dreamy kiln
#

oh lol

#

no that's aight

supple sandal
#

fun fact: in C (and not in Zig) this is fine for printf (but not scanf), since the default argument promotions apply and unsigned char gets promoted to int

dreamy kiln
#

thinkies huh

#

& a u8 is the same as an unsigned char in C?

supple sandal
#

not necessarily, but usually

#

basically C promotes everything to int and double when it can

#

when it doesn't know the types

dreamy kiln
#

Gotcha, okay. Are ints the same as i16?

supple sandal
#

no

#

c_int

dreamy kiln
#

and that is a 16 bit integer correct?

supple sandal
#

why don't you check and see

#
comptime {
    @compileLog(@typeInfo(c_int));
}
dreamy kiln
#

yeah tbh I should just look into it deeper ig

supple sandal
dreamy kiln
#

ohhh gotcha

#

yeah I started learning C in mid october, and I'm trying to learn zig alongside it

supple sandal
supple sandal
#

enjoy

#

somehow I was under the impression you wrote C++

dreamy kiln
#

no this is really my first real dive into programming really at all (don't tell AV1 lmao)

#

I've enjoyed dicking around with Bash & Python but most of my projects have been essentially scripts

supple sandal
#

:3 bash was my introduction to programming too

#

ah well

dreamy kiln
#

the linux pipeline

supple sandal
#

true!

#

hope you have a pleasant journey

dreamy kiln
#

thanks :)

#

you & p7 working on VPXL I hear?

supple sandal
#

mostly p7
I'm just helping a bit with simd stuff

dreamy kiln
#

Ah nice

dreamy kiln
# supple sandal ```ts comptime { @compileLog(@typeInfo(c_int)); } ```
zig run src/main.zig  
/usr/lib/zig/std/start.zig:559:45: error: root struct of file 'main' has no member named 'main'
    switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
                                        ~~~~^~~~~
/usr/lib/zig/std/start.zig:508:12: note: called from here
    return @call(.always_inline, callMain, .{});
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/zig/std/start.zig:458:36: note: called from here
    return initEventLoopAndCallMain();
           ~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/lib/zig/std/start.zig:414:17: note: called from here
    std.os.exit(@call(.always_inline, callMainWithArgs, .{ argc, argv, envp }));
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compile Log Output:
@as(builtin.Type, .{ .Int = .{.signedness = .signed, .bits = 32} })

This is what happens when I run this

#

I think it is giving me the desired answer at the end, saying a c_int is a signed 32-bit int, but what's up with the error there?

subtle trench
#

Do it in a test or something

#

And then zig test file

#

Otherwise you need a main function

#

Since you can't have code floating in the global scope