首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机选择名字时的奇怪字符

随机选择名字时的奇怪字符
EN

Stack Overflow用户
提问于 2015-08-28 20:00:48
回答 1查看 97关注 0票数 1

我有以下代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct persona
{
    char *nombre;
    int edad;
    int sexo;
} Persona;

typedef struct
{
    int size;
    Persona vecPersona[];
} Array;

Array* getArrayInstance()
{
    Array *vec;
    vec = (Array*) malloc (sizeof(Persona));
    vec->size = 0;
    return vec;
}

void push_back(Array ** vec, Persona tipito)
{
    (*vec)->vecPersona[(*vec)->size] = tipito;
    (*vec)->size++;
    printf("%d-", (*vec)->size);
    int newSize = (*vec)->size*2-(*vec)->size+1;
    Array *tmp = realloc((*vec), newSize*sizeof(Persona));
    if(tmp)
        *vec = tmp;
    else
        (*vec)->size--;
}

void mostrarPersonas(Array *vec)
{
    int i;
    printf("\n\n");
    printf("%d", vec->size);
    for(i=0; i<vec->size; i++)
    {
        printf("(%d) Nombre: %s - Edad: %d - Sexo: ", i, vec->vecPersona[i].nombre, vec->vecPersona[i].edad);
        if(vec->vecPersona[i].sexo == 0)
            printf("Masculino");
        else
            printf("Femenino");
        printf("\n");
    }
}

void cargarPersonas(Array **vec)
{
    int i, edad, random;
    int cantPersonas = 3;
    Persona aux;
    char hombres[][20] = {"Ramiro","Pedro","Federico","Jose","Antonio","Pablo","Raul","Gustavo","Gonzalo","Airton"};
    char mujeres[][20] = {"Mariana","Jennifer","Luz","Roxana","Ana","Sol","Micaela","Romina","Melina","Camila"};
    for(i=0; i<cantPersonas; i++)
    {
        edad = rand()%80+1;
        aux.edad = edad;
        if( (random = rand()%10) %2 == 0) 
        {
            aux.nombre = hombres[random];
            aux.sexo = 0;
        }
        else
        {
            aux.nombre = mujeres[random];
            aux.sexo = 1;
        }

        push_back(vec, aux);
    }

}


int main()
{
    srand(time(NULL));
    Array *vecPersonas = getArrayInstance();

    cargarPersonas(&vecPersonas); 
    printf("%d", vecPersonas->size);
    mostrarPersonas(vecPersonas);

    return 0;
}

代码编译时没有错误,但在执行它时会给我带来问题。例如,我在name字段中得到了奇怪的字符。

样本输出:

代码语言:javascript
复制
<0> Nombre: ?ç!:. - Edad: 25 - Sexo: Maculino

更新

显然问题在函数push_back中

代码语言:javascript
复制
void push_back(Array ** vec, Persona tipito)
{
    (*vec)->vecPersona[(*vec)->size] = tipito;
    (*vec)->size++;

    int newSize =  (*vec)->size + 1;
    Array *tmp = realloc(*vec, (newSize*sizeof(Persona))); // here

    if (!tmp)
    {
        printf("Cannot allocate more memory.\n");
        (*vec)->size--;
    }
    else
        *vec = tmp;
}
EN

回答 1

Stack Overflow用户

发布于 2015-08-28 20:05:04

第一个错误:

代码语言:javascript
复制
Array* getArrayInstance()
{
    Array *vec;
    vec = (Array*) malloc (sizeof(Persona));
//                               ^^^^^^^^^
//                    should be sizeof(*vec) == sizeof(Array)    
    vec->size = 0;
    return vec;
}

第二个错误:

代码语言:javascript
复制
void push_back(Array ** vec, Persona tipito)
{
    (*vec)->vecPersona[(*vec)->size] = tipito;
//                            ^^^^^^
//                      access out of range
//                      the last object in array is indexed with size-1
//                      if you wanted the first object, which is indexed with 0
//                      then it is still wrong as your vec->size is 0 when there 
//                      is no memory allocated for the array

标准输出中的奇怪字符是UB的预言家,或者更准确地说是投射到我们的维度中所留下的东西。

我们程序员应该不惜任何代价避免在输出中出现奇怪的字符。每当您看到它们立即采取纠正错误程序的行动时,请注意与访问数组元素有关的代码行,特别是,但不限于。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32278930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档