Thread: pointers, structures, and malloc

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It makes sense. If it were me I would write this out in C++ just to keep my code looking cleaner. But I am a C guy, so no harm doing it all in C

    Here is how I typically handle this stuff, lugnut, and see if this is of more help:

    Example:
    Code:
    char *pool = malloc(4096), *p; // Lets just pretend like the 4k is allocated no problem.
    struct myheader *h1;
    struct myotherheader *h2;
    
    // after peforming some sort of reading function that fills up pool's 4k of data.
    
    p = pool;
    
    // Lets say the first piece if information in your file is the offset of the first header..
    
    p += *(size_t *)p;
    h1 = (void *)p;
    p = (h1 + 1) + h1->comment_length;
    
    h2 = (void *)p;
    I find it easier to have one pointer that processes the entire data chunk in terms of bytes (or sometimes dwords) then have it scout out where the headers are supposed to exist.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Thanks again

    I think this code handles my situation. To be honest, it's a little (well, maybe not so little) over my head. But I've been known to study this kind of thing until I understood it!

    It just seems there ought to be a simpler way to do pointer arithmetic. Most of this is just trying to beat the compiler.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by lugnut View Post
    Most of this is just trying to beat the compiler.
    You may be new, but its very good that you are capable of making that distinction. The main thing my code does is tries to dupe the compiler. But that is only one way of thinking about it. The other way of thinking about it is that you are just letting the compiler see specifically what you are trying to accomplish. Though to be honest, this is one area of programming where the compiler is a bigger foe than friend. Assembler is much nicer about these sorts of things... Actually assembler is almost the most appriate language for these sorts of things.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Next try!

    I thought I understood this, but apparently I was wrong (just this once). Specifically, I really do not understand the code...
    Code:
     p += *(size_t *)p;
    h1 = (void *)p;
    p = (h1 + 1) + h1->comment_length;
    I made a big mistake in putting all that code out in my first post since it has a lot of irrelevant stuff in it. i decided to write another program. This program malloc's an area and then attempts to put a structure (fixed length) in it, followed by another structure which has a name (variable length in it). I put two of the latter. I also try to change the name in the second area since I have to update this stuff in place. Then I try to print this area. When I can get it to print, I get mostly garbage. I am using Watcom and its debugger. I traced the code and it was putting trash in the area. I modified it several times since and it won't compile now. All of this proves that I still can't navigate my way around a dynamic area.

    In the assembler world, I would get the address of this dynamic area, and use a "dsect" to describe it. I could then "move" anything I wanted to this area by the dsect name. I was thinking that's what the "*ptr" did for a structure. When I try to use this, I get compile errors. I have a trial copy of VS 2008 PRO, but I can't figure out how to set up a "project" and compile. MS has made this difficult, and their helps are useless. This is the usual case for me.

    I would write the mainframe asm that would do this but that wouldn't help... this is no system z. It's very frustrating to understand bytes, addressability, and all that stuff, and not be able to get this to work.

    If I can get this code to work, then I'll know how to make the real code work. It also has several diagnostic printf's. It took me a while to get the debugger going. Incidentally, I got all kinds of errors trying to initialize hdr1 and hdr2 with
    Code:
     hdr1 = {"a","b","c"};
    . I got this straight out of a book.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lugnut View Post

    If I can get this code to work, then I'll know how to make the real code work. It also has several diagnostic printf's. It took me a while to get the debugger going. Incidentally, I got all kinds of errors trying to initialize hdr1 and hdr2 with
    Code:
     hdr1 = {"a","b","c"};
    . I got this straight out of a book.
    I haven't carefully read the address part yet, so I won't speak to that yet -- maybe later. But what your book did say was
    Code:
    hdr1 = {'a', 'b', 'c'};
    which is not quite what you tried.

    Edit: Well, I suppose I should say that what works is using 'characters' and not "strings"; without looking at your book maybe it's a misprint.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, structures, arrays and sudoku!!
    By AmbliKai in forum C Programming
    Replies: 13
    Last Post: 10-15-2008, 03:05 AM
  2. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  3. pointers and structures
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 07-23-2003, 04:45 AM
  4. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 03:26 PM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM