【问题标题】:Why does the array contain values which I did not specify?为什么数组包含我没有指定的值?
【发布时间】:2020-07-26 22:51:46
【问题描述】:

我正在尝试制作一个跨越二进制数的程序。问题在于cross 函数。它接受两个二进制序列并返回 5 个序列,它们是交叉参数的结果。不知何故,这些序列中的第一个有一堆值,我无法真正解决这个问题。有人有什么想法吗?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define BINARY_LEN 5
#define POPULATION 5
// #define CROSS_BINARY_LIMIT 3

unsigned randrange(unsigned lower, unsigned upper)
{
    return lower + rand() / (RAND_MAX / (upper - lower + 1) + 1);
}

unsigned char *int_to_bin(unsigned number)
{
    unsigned char *binary = malloc(BINARY_LEN);    
    
    unsigned count = 0;
    while (number > 0)
    {
        binary[count] = number % 2;        
        number /= 2;
        count++;
    }

    return binary;
}

unsigned char **cross(unsigned char *parent_1, unsigned char *parent_2)
{
    unsigned char **offspring = malloc(POPULATION);
    unsigned cross_binary_point;

    for (unsigned char i = 0; i < POPULATION; i++)
    {
        cross_binary_point = randrange(0, BINARY_LEN);
        offspring[i] = malloc(BINARY_LEN);

        for (unsigned char j = 0; j < BINARY_LEN; j++)
        {
            if (j < cross_binary_point)
            {
                offspring[i][j] = parent_1[j];
                
            }
            else
            {
                offspring[i][j] = parent_2[j];
            }            
        }
    }

    return offspring;
}    

int main(void)
{
    unsigned char *x = int_to_bin(14);
    unsigned char *y = int_to_bin(18);

    for (unsigned char i = BINARY_LEN; i > 0; i--)
    {
        printf("%hhu", x[i - 1]);
    }

    printf("\n");

    for (unsigned char i = BINARY_LEN; i > 0; i--)
    {
        printf("%hhu", y[i - 1]);
    }

    printf("\n\n");

    unsigned char **ofspr = cross(x, y);

    printf("%s\n", ofspr[0]); // Try to check out what's wrong with the first array

    for (unsigned char i = 0; i < POPULATION; i++)
    {        
        for (unsigned char j = BINARY_LEN; j > 0; j--)
        {
            printf("%hhu", ofspr[i][j]);
        }
        printf("\n");
    }

    free(ofspr);
    free(x);
    free(y);
}

输出是这样的:

01110
10010

`w;
00059119
01011
01001
01111
01011

也许有一些内存冲突的东西,但我没有任何想法

【问题讨论】:

    标签: arrays c pointers memory


    【解决方案1】:
    unsigned char **offspring = malloc(POPULATION);
    

    只分配 5 个字节,你想要 5 个指针

    应该是

    unsigned char **offspring = malloc(POPULATION * sizeof(char*));
    

    【讨论】:

    • 谢谢,我应该更加专心。我还以为 char 和 char* 的大小是一样的
    • 内存分配最好写成malloc(POPULATION * sizeof *offspring),因为这会自动匹配正在分配的对象的类型——它不容易出错,如果offspring的类型每次都改变了,它会自动获得正确的类型,因此只需要一次编辑而不是两次或更多。
    • @EricPostpischil 我发现这里的单星后代有点令人困惑,它是指向内部数组还是指向外部数组的指针?希望我表达正确
    • @Kaiyaha:如果**offspring是元素,那么*offspring是指向元素的指针,offspring是指向元素的指针。
    • @EricPostpischil 明白了,以为*offspring 是指向指针的指针,offspring 是指向元素的指针。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 2011-05-03
    相关资源
    最近更新 更多