Thread: malloc and calloc

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Does e go past MAXLINES? And is getline your own creation, since it doesn't seem to be the GNU version? If so, does it properly null-terminate things?

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Yeah that's my version of getline and it does terminate the string. e does go past MAXLINES though.

    Program received signal SIGSEGV, Segmentation fault.
    0x08048648 in main (argc=2, argv=0xbffff4f4) at exercise.5-13.c:56
    56 strcpy(lines[e++], storeline);
    (gdb) print e
    $1 = 134556832

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by rrc55 View Post
    e does go past MAXLINES though.
    Oh great, so you found the problem!
    A more useful way to fix it would be to make lines a char** and allocate the array of char*'s dynamically. Then when you run out of empty char*s in the array you can malloc an array twice as big and copy the old char*'s across. Basically simulating a C++ vector.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Malloc & Calloc difference in terms of memory allocated
    By swapnaoe in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 12:57 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM