Thread: gdb/C question

  1. #16
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by tabstop View Post
    You should look at what is actually being called at line 2623. Is that a strcpy call, or is that a call to something else? You may have a library in between there. A test GDB run gave me for a backtrace
    Code:
    #0  strcpy (dest=0x0, src=0xbffff90f "Hello world!") at strcpy.c:40
    #1  0x08048462 in main () at segfault.c:9
    so that missing line probably represents code we can't see/don't have symbols for.
    2623 is the strcpy()...

    Sorry, I don't what you mean by "missing line".

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by towed View Post
    2623 is the strcpy()...

    Sorry, I don't what you mean by "missing line".
    As you can probably tell by the timestamps, this got posted as you were posting your other reply. So it was another attempt to see what was going on with your 0x000000 line. But since we know line 2623 is strcpy, we don't have to worry about it.

  3. #18
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    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
    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...

    Any more ideas? I am adding a malloc/error returns NULL statement to see what happens...

  4. #19
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Testing for the error message w/

    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;
                        }
    it now prints...

    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, " ");
    So malloc returned NULL... And it now it fails on the next strcpy call that follows after another malloc ( im assuming that one fails.) ...

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?

  6. #21
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by tabstop View Post
    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.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  8. #23
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Quote Originally Posted by tabstop View Post
    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.)
    Yeah, I have it print strlen() when it fails and it returned 3. So, I THINK it is safe to say its memory leak or not enough being present. Could it be that conclusive (in regard to the diagnoses).

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by towed View Post
    Could it be that conclusive (in regard to the diagnoses).
    It is conclusive that malloc is an out-of-memory situation. Whether it's a memory leak or you just requiring more than you have I can't say.

  10. #25
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    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.

  11. #26
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    (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.)
    strlen(ptr_str)+1 can become 0. but I guess it's just because of memory leak.
    @OP you can use valgrind to find out leaks.

  12. #27
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    How can strlen(ptr_str)+1 become 0. Doesn't strlen have to be positive?

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by towed
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM