![]() |
|
#1
|
||||
|
||||
Segmentation fault issues with 'realloc'Hello there,
I am trying to implement a custom gets function which would allow me to read a string so as to avaoid the pitfalls associated with the C version of 'gets' as mentioned here. Well...I have been able to get some breakthrough into it. What my function does it that it keeps on extending the buffer (to hold the string) size by 16 if the user enters a string longer than 16 until it encounters the '\n' character. Im doing a char by char read using getchar and i check for the '\n' character and for the number of chars entered as of yet. So if the user string is more than 4(size of the original buffer) but less than 16, it works fine though it only displays the first 4 chars..but if the user string is >16 then issues do arise. Im wondering why...the seg fault occurs at the line preceded by an arrow-head in the program below Heres the whole deal: CPP / C++ / C Code:
Hope to hear from you soon, BR, __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#3
|
||||
|
||||
Re: Segmentation fault issues with 'realloc'thnks for the reply,
what would one do if one needs to call realloc iteratively over a couple of times?. Is it allowed in that case...? __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#4
|
|||
|
|||
Re: Segmentation fault issues with 'realloc'I would change the function signature to not take any parameters and make sure the calling program will always free the memory returned. For instance:
CPP / C++ / C Code:
There could be other options as well, like passing a pointer to memory and an integer of the amount of memory available. This would work similar to fgets() and could be ideal so that the calling program does not have to free() any memory it did not create. Then, though, you could just call fgets() and pass it stdin as the FILE* argument. |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| An Access Violation Segmentation Fault | boschow | C++ Forum | 3 | 31-Jan-2008 14:34 |
| segmentation fault error | vermin1302 | C Programming Language | 7 | 25-Feb-2006 14:38 |
| segmentation fault? in C | micheldenostra | C Programming Language | 1 | 10-Sep-2005 13:27 |
| Please help segmentation fault problem | robsmith | C Programming Language | 1 | 08-May-2005 21:34 |
| segmentation fault in c++ | rushman8282 | C++ Forum | 2 | 26-Jan-2005 04:38 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The