This suggest to me since I can see what ptr_str is, that its strlen would be 3... so don't see nothing wrong with that...Code:(gdb) down #2 0x08053734 in madis_wx_intensity (pres_weath=0x81163bb "-RA", ' ' <repeats 21 times>, auto_stn_type=0x813ddb2 " ", pass=1, precip1=0x2d, precip2=0x2d, precip3=0x2d, stn_type=0xbfbfe6a4, pcp_occurrence=0xbfbfe6a8) at sfc_oban.c:2623 2623 strcpy(wx_second, ptr_str); (gdb) print ptr_str $1 = 0x88087ff0 "-RA" (gdb) print wx_second $2 = 0x0
Any more ideas? I am adding a malloc/error returns NULL statement to see what happens...
Testing for the error message w/
it now prints...Code:wx_second = malloc(strlen(ptr_str) + 1); if(wx_second == NULL){ printf("** Error in madis_wx_intensity. **\n"); printf("** Unable to allocate space for wx_second char pointer. **\n"); printf("** Halting execution with error code 2b. **\n\n"); return 2; }
So malloc returned NULL... And it now it fails on the next strcpy call that follows after another malloc ( im assuming that one fails.) ...Code:** Error in madis_wx_intensity. ** ** Unable to allocate space for wx_second char pointer. ** ** Halting execution with error code 2b. ** Program received signal SIGSEGV, Segmentation fault. madis_wx_intensity (pres_weath=0x81163d4 ' ' <repeats 24 times>, auto_stn_type=0x813ddb8 " ", pass=1, precip1=0x0, precip2=0x0, precip3=0x0, stn_type=0xbfbfe6a4, pcp_occurrence=0xbfbfe6a8) at sfc_oban.c:2187 2187 strcpy(string, " ");
Once you're out of memory, you're really out of memory -- only thing left is to shut it down.
The next thing to look at is: how much memory are you using anyway? Are you freeing any of these calls to malloc when you are done with the data? Are you ever done with the data? How much data do you have running around when this happens?
Interesting... Sadly, I never thought this could/would occurred. I always (that I remember) am freeing memory after every malloc (including in this function since it is called MANY times. Which again seems odd, as I do free() in this function. Thus, subsequent calls to it shouldn't be a problem, I would think.
So a memory leak is the diagnose you think? I thought gdb looks for those as well :-/.
---
PS... Scratch that, found some char points I never'd free'd. Freeing them now and re-running prog.
Last edited by towed; 12-29-2010 at 12:41 AM.
gdb has no way of looking for memory leaks. For that, maybe you could look at valgrind or another similar tool. I'm not completely sure how well it works in this situation, but if you do an exit() when malloc gives up instead of going on to let the next malloc kill you, then you should get a report about any unfreed blocks that are still hanging around.
(EDIT: The other way you can get NULL from malloc is to pass it 0 for the size, but it seems fairly clear that at least in this instance that isn't happening.)
Well the aforementioned freeing seems like it worked, given that it now has passed that point in the program. Thanks tabstop ... I think I learned a thing or two with this discussion.
strlen(ptr_str)+1 can become 0. but I guess it's just because of memory leak.(EDIT: The other way you can get NULL from malloc is to pass it 0 for the size, but it seems fairly clear that at least in this instance that isn't happening.)
@OP you can use valgrind to find out leaks.
How can strlen(ptr_str)+1 become 0. Doesn't strlen have to be positive?
In theory, if the length of the string is the maximum value of size_t, then strlen(ptr_str)+1 == 0.Originally Posted by towed
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)