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 *stream, string *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 {FALSE, TRUE} bool;
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 *stream, string *var) {
unsigned int i = 1;
string temp = calloc(i++, sizeof(string));
char c = getc(stream);
if (temp == NULL) {
*var = NULL;
return;
}
while(c == ' ')
c = getc(stream);
do {
temp = realloc(temp, i++ * sizeof(string));
if (temp == NULL) {
*var = NULL;
return;
}
temp[i - 3] = c;
} while((c = getc(stream)) && c != '\n' && c != '\r');
temp[i - 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
all: My
# Tool invocations
My: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: MacOS X C Linker'
gcc -o "My" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) My
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets