Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

uipe

macrumors newbie
Original poster
Nov 13, 2007
4
0
Hi,

I am a new mac user ;), I just recently bought my macbook for school work!

My ploblem is that when I compile a C program in the terminal using a Makefile that I created and tested in linux, the program compiles correctly but it segfaults while running, but if I compile it in xcode it works grate :confused: any tip?

ps: the macbook really rules :D
 

uipe

macrumors newbie
Original poster
Nov 13, 2007
4
0
oki, sorry ;)

Makefile :

Code:
#flags for the compiler
CFLAGS = -Wall -W -g -Wmissing-prototypes

# Libs
LIBS = -lm -lpthread


#++++++++++++++++  files  +++++++++++++++++++++
OBJS = main.o cmdmatrix.o

# exec name
PROGRAM = matrix
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 

# make principal
all: ${PROGRAM}

# compile with debug
debug: CFLAGS += -D SHOW_DEBUG 
debug: ${PROGRAM}

${PROGRAM}: ${OBJS}
	${CC} -o $@ ${OBJS} ${LIBS}


#++++ Dependencies +++++
main.o: main.c cmdmatrix.h
cmdmatrix.o: cmdmatrix.c cmdmatrix.h
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



# convert .c in .o
.c.o:
	${CC} ${CFLAGS} -c $<

here is the main.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <ctype.h>
#include <math.h>

#include "cmdmatrix.h"
#define TAMANHO 200

"..."
static struct gengetopt_args_info params;
struct tm *tempoUtc;
time_t tempo;
char titulo[TAMANHO]="";
char tempoTitulo[20];

"..."
//printf("# matrix – desempenho\n# Parâmetros da execução: ");
strcat(titulo,"# matrix – desempenho\n# Parâmetros da execução:");
if(params.dimensao_given) {
//printf("-n %s ", params.dimensao_orig);
strcat(titulo," -n ");
strcat(titulo,params.dimensao_orig);
}else {
strcat(titulo," -m ");
strcat(titulo,params.minimo_orig);
strcat(titulo," -M ");
strcat(titulo,params.maximo_orig);
strcat(titulo," -p ");
strcat(titulo,params.passo_orig);
} //printf("-m %s, -M %s, -p %s ", params.minimo_orig, params.maximo_orig, params.passo_orig);
if(params.repeat_given){
strcat(titulo," -x ");
strcat(titulo,params.repeat_orig);
//printf("-x %s ",params.repeat_orig);
}
if(params.teste_given) {
strcat(titulo," -t ");
strcat(titulo,params.teste_orig);
//printf("-t %s ",params.teste_orig);
}
if(params.ficheiro_given) {
strcat(titulo," -e ");
strcat(titulo,params.ficheiro_orig);
//printf("-e %s ",params.ficheiro_orig);
}
if(params.grafico_given) {
strcat(titulo," -g ");
strcat(titulo,params.grafico_orig);
//printf("-g %s ",params.grafico_orig);
}
if(params.aviso_given) {
strcat(titulo," -i");
//printf("-i");
}

// cria var com tempo actual tempo actual
tempo = time (NULL);
// formata o tempo em UTC
tempoUtc = localtime (&tempo);
sprintf(tempoTitulo,"\n# @%d-%d-%d %dh%d\n",(tempoUtc->tm_year+1900),tempoUtc->tm_mon,tempoUtc->tm_mday,tempoUtc->tm_hour,tempoUtc->tm_min);
strcat(titulo,tempoTitulo); //<---- PROBLEM
strcat(titulo,"# Dimensao: Numero de execucoes: tempo media (s): desvio-padrao (s)\n");
printf("%s",titulo);
"..."

I have run it in gdb and the segmentation fault is here strcat(titulo,tempoTitulo); and if I tell gdb to print titulo it says its repeats 145 times.

Again, this program is working well in Linux and if compiled in Xcode 3.0, I am using Leopard.
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
tempTitulo is only 20 characters in length and you are trying to write more than that into the string on the line above. Make sure your string is long enough to hold the longest possible value, including a terminating NULL.
 

uipe

macrumors newbie
Original poster
Nov 13, 2007
4
0
thanks a lot ;) it worked, to be honest I was really sceptic when I tested, because I just dont understand why it worked with xcode and linux :S and not when compiled with make in terminal, any ideas ?
 

Gelfin

macrumors 68020
Sep 18, 2001
2,165
5
Denver, CO
thanks a lot ;) it worked, to be honest I was really sceptic when I tested, because I just dont understand why it worked with xcode and linux :S and not when compiled with make in terminal, any ideas ?

It was probably random that it seemed to be fine under certain circumstances. Behavior is not entirely predictable when you make mistakes with memory.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.