elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


  Mostrar Mensajes
Páginas: 1 ... 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [23] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ... 45
221  Programación / Programación C/C++ / Re: (Ayuda) Segmentation fault sin razón aparente en: 15 Junio 2016, 06:29 am
Funciona con fgets, pero me da rabia que crashee con fscanf cuando debería funcionar...

Por cierto, es muy extraño: usando el siguiente fragmento de código se leen más de 64 caracteres, mientras que en los otros ifs que usan la misma sintáxis no lee más de 64 (que es lo esperable):

Código
  1. fscanf(file, "%s[^64]", command);

¿POR QUÉ FUNCIONA SOLO A VECES XDD? Supongo que será un bug, pero es demasiado extraño...



Voy a dar por cerrado el tema, a parte de porque lo solucioné con fgets, dudo que vaya a encontrar el error que produce la función fscanf... Muchas gracias a todos por compartir su sabiduría y tiempo :D



La respuesta al problema... por fin la he hallado... Resulta que por alguna razón fscanf está ignorando el límite de caracteres. Lo he demostrado con el siguiente código:

Código
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. char cadena[16];
  5.  
  6. fscanf(stdin, "%s[^16]", cadena);
  7. fprintf(stdout, cadena);
  8.  
  9. return 0;
  10. }

Este programa crashea si introduces una cadena de más de 16 caracteres...

Haré otra pregunta en el foro para saber por qué ignora el límite de caracteres. Muchas gracias a todos :D
222  Programación / Programación C/C++ / Re: (Ayuda) Segmentation fault sin razón aparente en: 15 Junio 2016, 05:47 am
VAYA. No sé por qué pasa esto:

Código:
00007FFFE975E2A0.
[OK].
Ignoring line...0000676E702E3565.

Sigo depurando y os comento



ACABO DE DESCUBRIR DONDE ESTÁ EL ERROR. POR ALGUNA RAZÓN, EN UNA PARTE DEL CÓDIGO QUE AHORA MOSTRARÉ LA VARIABLE 'file' ESTÁ CAMBIANDO DE VALOR.

El fragmento es este:
Código
  1. if(0 == strcmp(command, "map_Kd")) {
  2.    fprintf(stdout, "%p.\n", file); // AQUÍ VALE 0x00007FFFE975E2A0
  3.    fscanf(file, "%s[^64]", command);
  4.    fprintf(stdout, "%p.\n", file); // AQUÍ VALE 0x0000676E702E3565
  5.    // NO HE HECHO NINGUNA ASIGNACIÓN XD...
  6.    command[63] = '\0';
  7.    size = strlen(command);
  8.    if(64 == size) {
  9.        fprintf(stdout, "Warning: diffuse texture path size is too long.\n");
  10.        fprintf(stdout, "Diffuse texture: %s.\n", command);
  11.        fprintf(stdout, "Material path: %s.\n", path);
  12.    }
  13.  
  14.    (*materials)[*num_materials-1].texture.diff_tex = load_bmp_texture(command);
  15.    if(0 == (*materials)[*num_materials-1].texture.diff_tex) {
  16.        fprintf(stdout, "Error loading and creating diffuse texture.\n");
  17.        fprintf(stdout, "Diffuse texture path: %s.\n", command);
  18.        fprintf(stdout, "Material path: %s.\n", path);
  19.        fclose(file);
  20.        return GL_FALSE;
  21.    }
  22. }

Las direcciones que he marcado son de la variable 'file'



NOTA POR SI ES IMPORTANTE:
Al principio del tema, dije que solo ocurría al analizar map_Kd (el if con map_Kd). Efectivamente, la dirección solo cambia en ese if...
223  Programación / Programación C/C++ / Re: (Ayuda) Segmentation fault sin razón aparente en: 15 Junio 2016, 05:42 am
Sigo obteniendo el mismo error... esto empieza a ser desesperante...
224  Programación / Programación C/C++ / Re: (Ayuda) Segmentation fault sin razón aparente en: 15 Junio 2016, 05:16 am
He probado la siguiente función obteniendo los mismos resultados:

Código
  1. void ignore_line(FILE *file) {
  2. if(NULL != file) {
  3. fprintf(stdout, "Ignoring line...");
  4. while(0 == ferror(file) && 0 == feof(file) && '\n' != fgetc(file));
  5. /* Null instruction */;
  6. fprintf(stdout, "[OK].\n");
  7. }
  8. }
225  Programación / Programación C/C++ / Re: (Ayuda) Segmentation fault sin razón aparente en: 15 Junio 2016, 04:38 am
Este es el código completo. No veo cómo se podría haber cerrado el archivo, o "liberado". Siempre que uso fclose, hay un return justo después, así que no sé...

Código
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define GL_FALSE 0
  6. #define GL_TRUE  1
  7.  
  8. typedef int GLboolean;
  9. typedef char GLchar;
  10. typedef unsigned int GLuint;
  11. typedef float GLfloat;
  12.  
  13. typedef struct {
  14. /* Código innecesario */
  15. } vec3;
  16.  
  17. typedef struct {
  18. /* Código innecesario */
  19. } texture_t;
  20.  
  21. typedef struct {
  22.    /* Código innecesario */
  23. } material_t;
  24.  
  25. GLuint load_bmp_texture(const GLchar *path);
  26. GLboolean load_materials(const char *path, material_t **materials, GLuint *num_materials);
  27. void ignore_line(FILE *file);
  28.  
  29. int main() {
  30. material_t *materials = NULL;
  31. GLuint num_materials = 0, i = 0;
  32.  
  33. if(GL_FALSE == load_materials("material.mtl", &materials, &num_materials)) {
  34. fprintf(stdout, "Error loading material 'material.mtl'.\n");
  35. return -1;
  36. }
  37.  
  38. for(i = 0; i < num_materials; i++) {
  39. fprintf(stdout, "Material name: %s.\n", materials[i].material_name);
  40. fprintf(stdout, "Ambient factor: (%f, %f, %f).\n", materials[i].ambi_factor.x, materials[i].ambi_factor.y, materials[i].ambi_factor.z);
  41. fprintf(stdout, "Diffuse factor: (%f, %f, %f).\n", materials[i].diff_factor.x, materials[i].diff_factor.y, materials[i].diff_factor.z);
  42. fprintf(stdout, "Specular factor: (%f, %f, %f).\n", materials[i].spec_factor.x, materials[i].spec_factor.y, materials[i].spec_factor.z);
  43. fprintf(stdout, "Specular exponent: %f.\n", materials[i].exponent);
  44.  
  45. fputc('\n', stdout);
  46. }
  47.  
  48. return 0;
  49. }
  50.  
  51. GLuint load_bmp_texture(const GLchar *path) {
  52. return 1;
  53. }
  54.  
  55. GLboolean load_materials(const char *path, material_t **materials, GLuint *num_materials) {
  56.    FILE *file = NULL;
  57.    char command[64];
  58.    unsigned int size = 0;
  59.  
  60.    file = fopen(path, "r");
  61.    if(NULL == file) {
  62.        fprintf(stdout, "Error opening material file '%s'.\n", path);
  63.        return GL_FALSE;
  64.    }
  65.  
  66.    while(0 == feof(file) && 0 == ferror(file)) {
  67.        fscanf(file, "%s[^64]", command);
  68.        command[63] = '\0';
  69.        size = strlen(command);
  70.        if('#' != command[0] && 64 == size) {
  71.            fprintf(stdout, "Warning: material command is too long.\n");
  72.            fprintf(stdout, "Material command: %s.\n", command);
  73.            fprintf(stdout, "Material path: %s.\n", path);
  74.        }
  75.  
  76.        if(0 == strcmp(command, "newmtl")) {
  77. if(NULL == *materials) {
  78. *num_materials = 1;
  79. *materials = malloc(sizeof(material_t));
  80. if(NULL == *materials) {
  81. fprintf(stdout, "Error allocating dynamic memory for material.\n");
  82. fclose(file);
  83. return GL_FALSE;
  84. }
  85. } else {
  86. *num_materials += 1;
  87. *materials = realloc(*materials, *num_materials*sizeof(material_t));
  88. if(NULL == *materials) {
  89. fprintf(stdout, "Error reallocating dynamic memory for material.\n");
  90. fclose(file);
  91. return GL_FALSE;
  92. }
  93. }
  94.  
  95.            fscanf(file, "%s[^64]", command);
  96.            command[63] = '\0';
  97.            size = strlen(command);
  98.            if(64 == size) {
  99.                fprintf(stdout, "Warning: material name too long.\n");
  100.                fprintf(stdout, "Material name: %s.\n", command);
  101.                fprintf(stdout, "Material path: %s.\n", path);
  102.            }
  103.  
  104.            (*materials)[*num_materials-1].material_name = malloc(strlen(command));
  105.            if(NULL == (*materials)[*num_materials-1].material_name) {
  106.                fprintf(stdout, "Error allocating dynamic memory for material name ('%s').\n", path);
  107.                fclose(file);
  108.                return GL_FALSE;
  109.            }
  110.            /* Código innecesario */
  111.        } else if(0 == strcmp(command, "Ka")) {
  112.            fscanf(file, "%f%f%f", &(*materials)[*num_materials-1].ambi_factor.x,
  113.                                   &(*materials)[*num_materials-1].ambi_factor.y,
  114.                                   &(*materials)[*num_materials-1].ambi_factor.z);
  115.        } else if(0 == strcmp(command, "Kd")) {
  116.            fscanf(file, "%f%f%f", &(*materials)[*num_materials-1].diff_factor.x,
  117.                                   &(*materials)[*num_materials-1].diff_factor.y,
  118.                                   &(*materials)[*num_materials-1].diff_factor.z);
  119.        } else if(0 == strcmp(command, "Ks")) {
  120.            fscanf(file, "%f%f%f", &(*materials)[*num_materials-1].spec_factor.x,
  121.                                   &(*materials)[*num_materials-1].spec_factor.y,
  122.                                   &(*materials)[*num_materials-1].spec_factor.z);
  123.        } else if(0 == strcmp(command, "Ns")) {
  124.            fscanf(file, "%f", &(*materials)[*num_materials-1].exponent);
  125.        } else if(0 == strcmp(command, "map_Kd")) {
  126.            fscanf(file, "%s[^64]", command);
  127.            command[63] = '\0';
  128.            size = strlen(command);
  129.            if(64 == size) {
  130.                fprintf(stdout, "Warning: diffuse texture path size is too long.\n");
  131.                fprintf(stdout, "Diffuse texture size: %s.\n", command);
  132.                fprintf(stdout, "Material path: %s.\n", path);
  133.            }
  134.  
  135.            (*materials)[*num_materials-1].texture.diff_tex = load_bmp_texture(command);
  136.            if(0 == (*materials)[*num_materials-1].texture.diff_tex) {
  137.                fprintf(stdout, "Error loading and creating diffuse texture.\n");
  138.                fprintf(stdout, "Diffuse texture path size: %s.\n", command);
  139.                fprintf(stdout, "Material path: %s.\n", path);
  140.                fclose(file);
  141.                return GL_FALSE;
  142.            }
  143.        } else if(0 == strcmp(command, "map_Ks")) {
  144.            fscanf(file, "%s[^64]", command);
  145.            command[63] = '\0';
  146.            size = strlen(command);
  147.            if(64 == size) {
  148.                fprintf(stdout, "Warning: specular texture path size is too long.\n");
  149.                fprintf(stdout, "Specular texture path size: %s.\n", command);
  150.                fprintf(stdout, "Material path: %s.\n", path);
  151.            }
  152.  
  153.            (*materials)[*num_materials-1].texture.spec_tex = load_bmp_texture(command);
  154.            if(0 == (*materials)[*num_materials-1].texture.spec_tex) {
  155.                fprintf(stdout, "Error loading and creating specular texture.\n");
  156.                fprintf(stdout, "Diffuse texture size: %s.\n", command);
  157.                fprintf(stdout, "Material path: %s.\n", path);
  158.                fclose(file);
  159.                return GL_FALSE;
  160.            }
  161.        } else if('#' != command[0]) {
  162.            fprintf(stdout, "Warning: unrecognized command '%s'.\n", command);
  163.            fprintf(stdout, "Material file name: %s.\n", path);
  164.            fprintf(stdout, "This model loader is not complete.\n");
  165.        }
  166.  
  167. ignore_line(file);
  168.    }
  169.  
  170.    fclose(file);
  171.    return GL_TRUE;
  172. }
  173.  
  174. void ignore_line(FILE *file) {
  175. if(NULL != file) {
  176. fprintf(stdout, "Ignoring line...");
  177. while(0 == feof(file) && '\n' != fgetc(file));
  178. /* Null instruction */;
  179. fprintf(stdout, "[OK].\n");
  180. }
  181. }
226  Programación / Programación C/C++ / (Ayuda) Segmentation fault sin razón aparente en: 15 Junio 2016, 03:37 am
Hola, muy buenas. Os traigo una pregunta que me intriga demasiado. Tengo esta función:

Código
  1. void ignore_line(FILE *file) {
  2. if(NULL != file) {
  3. fprintf(stdout, "Ignoring line...");
  4. while(0 == feof(file) && '\n' != fgetc(file));
  5. /* Null instruction */;
  6. fprintf(stdout, "[OK].\n");
  7. }
  8. }

Pues resulta que al cabo de algunos usos me tira un segmentation fault (lo sé por el depurador de MinGW-w64). Esta es parte de la consola:

Código:
Ignoring line...[OK].
Ignoring line...[OK].
Ignoring line...[OK].
Ignoring line...[OK].
Warning: unrecognized command 'que'.
Material file name: material.mtl.
This model loader is not complete.
Ignoring line...[OK].
Warning: unrecognized command 'map_Ka'.
Material file name: material.mtl.
This model loader is not complete.
Ignoring line...[OK].
Ignoring line...
Program received signal SIGSEGV, Segmentation fault.
0x00007fffe971c49d in msvcrt!feof () from C:\WINDOWS\system32\msvcrt.dll

Como veis, hay un 'Ignoring line...' que no tiene su OK, por lo que tiene que ser en esta función donde se produce el error. También podéis observar que ya se había usado esta función sin que diera errores.



____________________________________________________
Nota posiblemente irrelevante:

Creo que el error se produce en esta parte del archivo:
Código:
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 13.12.2013 19:23:54

newmtl Material__29
Ns 10.000000
Ni 1.500000
d 1.000000
Tr 0.000000
Tf 1.000000 1.000000 1.000000
illum 2
Ka 0.588000 0.588000 0.588000
Kd 0.588000 0.588000 0.588000
Ks 0.000000 0.000000 0.000000
que 0.000000 0.000000 0.000000
map_Ka C:\Users\kakamaka\Desktop\TurboSquid\Minecraft\SabieDiamant\Textures\Blue5.png
map_Kd C:\Users\kakamaka\Desktop\TurboSquid\Minecraft\SabieDiamant\Textures\Blue5.png
"<<<<< AQUÍ. LA PARTE ENTRECOMILLADA NO ESTÁ EN EL ARCHIVO"
newmtl Material__27


Espero que haya dado todos los datos relevantes y que me puedan ayudar... Muchas gracias :DD
227  Programación / Programación C/C++ / Re: ayuda con strcpy en kubuntu en: 15 Junio 2016, 01:01 am
C no es tan estricto en ese sentido, así que no tiene sentido que te salte esa advertencia. Por ejemplo, he compilado el siguiente código diciéndole al compilador que me de alertas (-Wall), y no me ha dado ninguna:

Código
  1. #include <string.h>
  2.  
  3. int main() {
  4.    char nombre[] = "Nombre";
  5.    char copia[64];
  6.  
  7.    strcpy(copia, nombre);
  8.  
  9.    return 0;
  10. }
228  Programación / Programación C/C++ / Re: ayuda con strcpy en kubuntu en: 15 Junio 2016, 00:38 am
El 'warning' ese se produce porque no has incluido 'string.h', sino que has incluido 'strings.h'.

Otra cosa, cuando trabajas con los arreglos no accedes a estos con los corchetes. ¿Error al copiar o que simplemente no los has puesto?

Código
  1. scanf("%c", nombres); // Tendría que ser scanf("%c", nombres[i]);
Hay más casos en los que no pones los corchetes.
229  Programación / Programación C/C++ / Re: Error de ejecucion en: 14 Junio 2016, 18:10 pm
No sé muy bien cómo tenías planteado resolver el problema. Yo solo me voy a limitar a señalar el por qué te crashea el programa (segmentation fault). Este error se produce cuando intentas acceder a un área de la memoria que no está reservada para tu programa.

No sé el error exactamente pues no has puesto suficiente código, pero probablemente se esté produciendo porque no has inicializado correctamente la variable cabecera. En el constructor tendrías que haber hecho algo así:

Código
  1. Lista::Lista() {
  2.    cabecera = NULL;
  3. }


Y en la función insertar, algo así:

Código
  1. void insertar(string cadena){
  2.    Celda *puntero;
  3.    puntero = cabecera;
  4.  
  5.    if(NULL != puntero) { // Tienes que comprobar si hay datos o no
  6.        while(NULL != puntero->siguiente)
  7.            puntero = puntero->siguiente;
  8.  
  9.        puntero->siguiente = new Celda;
  10.        puntero->siguiente->siguiente = NULL; // Tienes que inicializar
  11.        puntero->sigueinte->datos = cadena;
  12.    } else {
  13.        cabecera = new Celda;
  14.        cabecera->siguiente = NULL; // Tienes que inicializar
  15.        cabecera->datos = cadena;
  16.    }
  17. }
230  Foros Generales / Foro Libre / Re: Nuevo Compilador en: 13 Junio 2016, 06:44 am
El primer lenguaje en español. ¡YUJU! No sé que tan óptimo será el lenguaje y/o el compilador, pero ya de por sí hacer un compilador está muy bien ;).

Un consejo:
* Veo que en el código usas la 'ñ'. La 'ñ' no está en la codificación ASCII, entonces si guardas el código fuente en una codificación que el compilador no espera, puede que tengas errores. Lo mismo pasa con las vocales acentuadas
Páginas: 1 ... 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [23] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ... 45
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines