Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C] Funzione per stringhe

    Perchè questo programma mi crasha durante la lettura di una stringa da input?
    Codice PHP:
    /*
     * String_Client.c
     *
     *  Created on: 08/nov/2010.
     *      Author: zorgatone.
     */

    #include <stdio.h>
    #include <stdlib.h>
    #include "String_Support.h"

    int main (void) {

        
    string mia_stringa;

        
    fprintf(stdout"Insert a string: ");
        
    get_string(stdin, &mia_stringa);
        if (
    mia_stringa == NULL) {
            
    fprintf(stderr"Error in read_string.\n");
            exit(
    EXIT_FAILURE);
        }
        
    fprintf(stdout"String is: %s"mia_stringa);

        return 
    0;


    Codice PHP:
    /*
     * String_Support.h
     *
     *  Created on: 08/nov/2010.
     *      Author: zorgatone.
     */

    #ifndef STRING_SUPPORT_H_
    #define STRING_SUPPORT_H_

    #ifdef __STDC_VERSION__

        #if __STDC_VERSION__ >= 199901L

            #define BOOL_H <stdbool.h>
            #define BOOL bool
            #define Bool bool

            #ifndef TRUE
                #define TRUE true
            #endif

            #ifndef FALSE
                #define FALSE false
            #endif

        #endif

    #else

        #define BOOL_H "Bool_Type.h"

    #endif

    #ifndef null
        #define null NULL
    #endif

    #include "String_Type.h"
    #include BOOL_H

    void get_string(FILE *streamstring *var);

    #endif /* STRING_SUPPORT_H_ */ 
    Codice PHP:
    /*
     * Bool_Type.h
     *
     *  Created on: 08/nov/2010
     *      Author: zorgatone
     */

    #ifndef Bool_Type_H_
    #define Bool_Type_H_

    #define true TRUE
    #define false FALSE
    typedef enum bool {FALSETRUEbool;
    typedef bool BOOL;
    typedef bool Bool;

    #endif /* Bool_Type_H_ */ 
    Codice PHP:
    /*
     * String_Type.h
     *
     *  Created on: 08/nov/2010.
     *      Author: zorgatone.
     */

    #ifndef STRING_TYPE_H_
    #define STRING_TYPE_H_

    typedef char *string;

    #endif /* STRING_TYPE_H_ */ 
    Codice PHP:
    /*
     * Information_Hiding.h
     *
     *  Created on: 08/nov/2010
     *      Author: zorgatone
     */

    #ifndef __Information_Hiding__
    #define __Information_Hiding__

        #define __Information_Hiding__
        #define PUBLIC /* Public */
        #define PRIVATE static
        #define PRIVATE2 static inline
        #define PRIVATE3 inline

    #endif /* INFORMATION_HIDING_H_ */ 
    Codice PHP:
    /*************************************
     * String_Support.c                  *
     *                                   *
     *  Created on: 08/nov/2010.         *
     *      Author: zorgatone.           *
     *************************************/
    /*
     * Header files includes.
     *
     *************************************************************************/

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include "Information_Hiding.h"
    #include "String_Support.h"

    void get_string(FILE *streamstring *var)  {

        
    unsigned int i 1;
        
    string temp calloc(i++, sizeof(string));
        
    char c getc(stream);

        if (
    temp == NULL) {
            *var = 
    NULL;
            return;
        }

        while(
    == ' ')
            
    getc(stream);
        do {
            
    temp realloc(tempi++ * sizeof(string));
            if (
    temp == NULL) {
                    *var = 
    NULL;
                    return;
                }
            
    temp[3] = c;
        } while((
    getc(stream)) && != '\n' && != '\r');
        
    temp[2] = '/0';           /* Lo so la slesh è al contrario ma solo perchè sul forum diventa un punto interrogativo */

        
    strcpy(*var, temp);
        
    free(temp);

        return;


    Quest'ultimo invece è il makefile (ho usato Eclipse per il progetto):
    Codice PHP:
    ################################################################################
    # Automatically-generated file. Do not edit!
    ################################################################################

    -include ../makefile.init

    RM 
    := rm -rf

    # All of the sources participating in the build are defined here
    -include sources.mk
    -include subdir.mk
    -include objects.mk

    ifneq 
    ($(MAKECMDGOALS),clean)
    ifneq ($(strip $(C_DEPS)),)
    -include $(
    C_DEPS)
    endif
    endif

    -include ../
    makefile.defs

    # Add inputs and outputs from these tool invocations to the build variables 

    # All Target
    allMy

    # Tool invocations
    My: $(OBJS) $(USER_OBJS)
        @echo 
    'Building target: $@'
        
    @echo 'Invoking: MacOS X C Linker'
        
    gcc  -"My" $(OBJS) $(USER_OBJS) $(LIBS)
        @echo 
    'Finished building target: $@'
        
    @echo ' '

    # Other Targets
    clean:
        -$(
    RM) $(OBJS)$(C_DEPS)$(EXECUTABLESMy
        
    -@echo ' '

    .PHONYall clean dependents
    .SECONDARY:

    -include ../
    makefile.targets 

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381

    Re: [C] Funzione per stringhe

    Originariamente inviato da Zorgatone
    Perchè questo programma mi crasha durante la lettura di una stringa da input?
    Perché non allochi lo spazio per mia_stringa e ci fai una strcpy sopra.
    In get_string cambia:
    codice:
     
        strcpy(*var, temp);
        free(temp);
    con
    codice:
        *var = temp
    e non dovresti avere problemi.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3

    Re: Re: [C] Funzione per stringhe

    Originariamente inviato da shodan
    Perché non allochi lo spazio per mia_stringa e ci fai una strcpy sopra.
    HO che cretino!!!! Mi son scordato!
    Grazie!

    Ho aggiunto nella funzione get_string() (me ne ero proprio dimenticato!):
    Codice PHP:
    *var = (string) calloc((unsigned intstrlen(temp), sizeof(string)); 
    Poi ho lasciato la strcpy(),

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    E' corretto anche così, ma fai un passaggio in più.
    Visto che allochi temp, perché non usare direttamente quella?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    Originariamente inviato da shodan
    E' corretto anche così, ma fai un passaggio in più.
    Visto che allochi temp, perché non usare direttamente quella?
    Perchè poi il client invoca la free() sulla stringa e dà errore se viene copiato il puntatore a temp nella stringa.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.