Thread: dynamic buffers

  1. #16
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    ok i figured it out.

    just as i thought, the problem lied within the code of the function itself (do i get a cookie?). strlen is evil! at least i'll never make that same mistake again... especially after a good 6-7 hours of debugging.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (out) { str_cat(out, FRED);
    This doesn't update 'out'. You need a pointer to a pointer if you're going to try and do it that way. You should get rid of the if check all together. At the very least, every single call to str_cat should be assigning the return value. Otherwise, your pointer ends up pointing to memory you've gotten rid of with your realloc call. Then the next call to realloc ends up trying to reallocate a pointer which has already been freed.

    See, there is actually a reason I suggested you post your code.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    9
    could you post a link to an explanation of using a pointer to a pointer like you're talking about

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use a pointer to update the contents of a passed variable. For example:
    Code:
    void foo( int bar )
    {
        bar = 5; /* This will not change 'bar' outside the function. */
    }
    Now to be able to update the variable we pass, we need a pointer instead.
    Code:
    void foo( int *bar )
    {
        *bar = 5; /* This will change 'bar' outside the function. */
    }
    We can do the same thing with actual pointers. If we have a pointer, and inside a function we pass it to, we want to change what it points to, we use a pointer to that. Like so:
    Code:
    void foo( int **bar )
    {
        *bar = malloc( 20 * sizeof( int ) );
    }
    Now outside the function, the pointer we have passed, will end up pointing to newly allocated 20 integers. Called like so:
    Code:
    int * bar;
    
    foo( &bar );
    /* bar now points at our newly allocated memory */
    ...
    free( bar );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Since you appear to be running on a Linux box, I'd suggest you try
    Code:
    gcc -g prog.c -lefence
    Electric fence is a memory debug tool which tries to tell you the point when you first trash memory, not when you first find out about it.
    Then run the executable inside the debugger.

    Example
    char *p = malloc( 10 );
    strcpy( p, "hello world" );
    Now you know this is bad, but there's no reason why these next calls would ever fail, except by chance.

    free ( p );
    char *q = malloc( 100 );
    char *r = realloc ( q, 200 );

    With electric fence in there, you'd see the problem at the strcpy, not some random (and seemingly correct) unrelated code elsewhere in your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. dynamic array/vector help
    By Cpro in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2008, 03:30 PM
  3. Identify dynamic IP address of network device
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 02-21-2006, 01:49 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Dynamic memory confusion
    By ripper079 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 06:15 PM