Hi, I'm making a little program that will send email. I am fine with the sockets, but I keep getting a segmentation fault and I don't know why:$. I'm trying to make a loop that will let the user enter the data for the email, ending with a '.' on its own line (like when using an smtp server). I'm on windows using cygwin.
char *data, ch;
char newline;
unsigned int datalen;
data = (char*)calloc(1, sizeof(char));
newline = 1;
datalen = 0;
while (1)
{
ch = fgetc(stdin);
realloc(data, ++datalen * sizeof(char));
*(data + datalen - 1) = ch;
if (newline)
{
if (ch == '.')
{
realloc(data, ++datalen * sizeof(char));
*(data + datalen - 1) = '\n';
break;
}
else if (ch != '\n')
{
newline = 0;
}
}
else
{
if (ch == '\n')
{
newline = 1;
}
}
}
Here's my output using gdb:
GNU gdb 5.2.1
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-mingw32"...(no debugging symbols found)...
(gdb) run
Starting program: g:\C\MailClient/./mail.exe
Program received signal SIGSEGV, Segmentation fault.
0x7c80a6c2 in _cygheap_end1 ()
(gdb)
I get the segmentation fault after I enter my 13th character.