943,813 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 669
  • C RSS
Jul 21st, 2009
0

Segmentation fault

Expand Post »
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.

  1. char *data, ch;
  2. char newline;
  3. unsigned int datalen;
  4.  
  5. data = (char*)calloc(1, sizeof(char));
  6. newline = 1;
  7. datalen = 0;
  8.  
  9. while (1)
  10. {
  11. ch = fgetc(stdin);
  12. realloc(data, ++datalen * sizeof(char));
  13. *(data + datalen - 1) = ch;
  14.  
  15. if (newline)
  16. {
  17. if (ch == '.')
  18. {
  19. realloc(data, ++datalen * sizeof(char));
  20. *(data + datalen - 1) = '\n';
  21. break;
  22. }
  23. else if (ch != '\n')
  24. {
  25. newline = 0;
  26. }
  27. }
  28. else
  29. {
  30. if (ch == '\n')
  31. {
  32. newline = 1;
  33. }
  34. }
  35. }

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.
Similar Threads
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Jul 21st, 2009
0

Re: Segmentation fault

Why are you mixing calloc's with realloc's.

Primarily Realloc() does not always stretch the same memory!!!! It returns the pointer to the new (or stretched) memory. In your case it has stretched it as much as it could around character 13 the reallocated a new block of memory, copied your data then destroyed your memory. You never reset your pointer by...
  1. data = realloc( data, +datalen *sizeof(char) );
...so you accessed memory that nolonger existed!


Why are you chewing on memory one character at a time.
Either use a large enough static buffer, or allocate a single array buffer malloc or new, NOT a two dimensional buffer using calloc!

Also note that you are leaving no room for a possible ASCIIz terminator when you've finished copying your ASCII.

And granted your buffer allocations are pretty short, but realloc CAN fail! So why aren't you checking for NULL return?
Last edited by wildgoose; Jul 21st, 2009 at 3:12 pm.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 21st, 2009
0

Re: Segmentation fault

Thanks. I had to do data = realloc(data, ++datalen * sizeof(char)). I forgot the "data = ". I just looked it up in my little c reference book.

void* realloc(void* p, size_t size);
Returns pointer to newly-allocated space for an object of size size, initialised, to
minimum of old and new sizes, to existing contents of p (if non-null), or NULL on
error. On success, old object deallocated, otherwise unchanged.

Forgot it returns a pointer :/. I also got rid of the original calloc. I'll do error checking on realloc also. I didn't want to use a static buffer because I don't know how much data the user will enter. I could just ask for the amount of bytes I guess. I thought about leaving room for a null character at the end, but I'm going to be sending it to an smtp server, so does it matter?
Last edited by TheBeast32; Jul 21st, 2009 at 3:53 pm.
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Jul 21st, 2009
0

Re: Segmentation fault

Fair enough.
When I have data that grows with unknown maximum length I tend to track the memory usage and grow (realloc) when I reach the buffer size. Kind of what realloc does but more like growth spurts of 256 or more bytes. Not one at a time (though it appears your memory system is probably growing around 16 bytes at a time, since it did a memory exchange on your 13th realloc).
Last edited by wildgoose; Jul 21st, 2009 at 3:53 pm. Reason: typo
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reading Line By Line question
Next Thread in C Forum Timeline: programming with DLL's functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC