![]() |
|
#1
|
|||
|
|||
Segmentation error on executing socket codeHi all,
On execution of following socket code with my function my_ctime(),generates segmentation fault,Without it (replacing my_ctime with ctime(&now)),it works fine. I wrote a seperate program to test my_ctime function there is executes properly without any problem.Any suggestion where am i going wrong. My Socket Code where I am using my_ctime() function. CPP / C++ / C Code:
The seperate program where my_ctime() works fine is as below. CPP / C++ / C Code:
Any suggestions?? Thanks, |
|
#2
|
||||
|
||||
|
Quote:
I'm surprised it works .Did you try to call my_time a couple more times in your main? Does it still work?I'm just saying this because when I used to program in C (a long time ago, I'm all for new and delete now ) I used to do this a little like this:CPP / C++ / C Code:
I'm sorry if I'm wrong, I don't know how ctime works.... Hope this helps Regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
|
Quote:
This might show the expected results, but represents a potential memory leak. It puts the responsibility on the calling program to delete the allocated memory after it is used. (Not a good thing.) Every time this routine is called, a new amount of memory (100 bytes) is allocated. It must be un-allocated before the main program exits. Instead of the malloc(), why not just use CPP / C++ / C Code:
The "static" means that the array is allocated once (when the main program is loaded for its executation), and remains in place with whatever value is put into it by this routine. When main() finally exits, all memory associated with this program is returned to the system. This is not true of memory that was obtained from malloc() unless the program specifically deletes it. Regards, Dave |
|
#4
|
|||
|
|||
|
Quote:
A clarification: I am sorry if I created any confusion by my careless use of the word "delete". Memory that is obtained by use of the standard library function malloc() must be returned to the system by a call to the standard library function free(). In C++, recommended functions are new and delete. Dave |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Formatting C / C++ code | WaltP | C Programming Language | 1 | 06-Jan-2008 23:59 |
| [PROGRAM] Winsock Programming | Max Payne | MS Visual C++ / MFC Forum | 1 | 08-Mar-2007 23:38 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The