Thread: Pointer Structure of Arrays?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    Pointer Structure of Arrays?

    I had a working code, such as the one below:
    Code:
    typedef struct { int* data } FIELD;
    FIELD* pField;
    fields;
    
    allocint(int** i,j,k) {
    *i = malloc(sizeof(int*));
    ...
    }
    
    ...
    realloc(pField,...);
    allocint(&pField[fields].data,...);
    ...
    Of course, I've cut most of the code. This method worked fine, but once there got to be about a thousand "realloc(pField,...);" calls, I started to get memory errors. So to fix that, I tried this sort of code:
    Code:
    typedef struct { int* data } FIELD;
    FIELD* pField[MAX_FIELDS];
    fields;
    
    allocint(int** i,j,k) {
    *i = malloc(sizeof(int*));
    ...
    }
    
    ...
    pFields[fields] = malloc(...);
    allocint(&pField[fields]->data,...);
    ...
    Now the program immediately has memory problems when calling allocint (It keeps saying that there is an error with "free()", but that isn't being used...)

    I know that this problem has something to do with the &pField[fields]->data. I suppose that the function is not receiving an editable address of the data variable, but I haven't a clue on how to fix this. I barely understand how double pointers and the like work anyway.

    I hope I didn't cut out so much of the code that it isn't clear what my intentions were. Thanks for your time!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And did you write allocint yourself? I was unable to find such a thing on either a man page or the MSDN site, so I know nothing about what the function is supposed to do, let alone whether it does it.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Yes. I wrote it as a shortcut to malloc or realloc depending on j and k. The actual code would have been inflated if I hadn't done the shortcut. Of course I cut out most of the function because I didn't think it was necessary to answer my question.

    Sorry about the confusion!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh, sorry, I didn't realize that you actually gave the first line of the function in the snippet! Sorry.

    As to free -- well, realloc calls free or something similar, more-or-less by definition. I'm guessing this means that somewhere (not in what was posted, apparently) you are clobbering your pointer, making it point somewhere else, and thus thwarting free/realloc in its task.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Well, that's what I thought, but when I removed the "realloc" from the function, it still came up with the same "free()" error. I didn't bother with any other functions (which have the same task, but with different inputs) because they were never being called by the time the program crashed.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, if you have an error with free, then something's causing it. At this point, you'll probably want to break out the debugger and step through to see where you are when bad things happen, or at least get a stack trace.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I see a few strange things here. Why do this:
    Code:
    typedef struct { int* data } FIELD;
    when you can do this
    Code:
    typedef int* FIELD;
    Because you like typing x->data? Then, by your own admission:
    Quote Originally Posted by Heathtech View Post
    I barely understand how double pointers and the like work anyway.
    Hopefully there is a real lifeguard around, but I don't think you need or should be using one here. Which could lead to the realloc problem tabstop describes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    Okay. I'm no good at figuring out stack traces, but I will try to figure out a work around.

    Just as a last question, do you think that the &pField[fields]->data is being called correctly? It should have the same result as the first code, but this time from within a FIELD* pField[...], versus a FIELD* pField.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    MK27:
    There is actually a lot more data within the structure, not just the int*.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Heathtech View Post
    MK27:
    There is actually a lot more data within the structure, not just the int*.
    Fair enough. But I still don't get how it becomes a pointer to a pointer for allocint. x->data is a pointer to an int.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    I need that function to set the data variable to the address provided by malloc. I don't think that's possible if you don't pass it as a pointer... Maybe I misunderstood your comment.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Fair enough. But I still don't get how it becomes a pointer to a pointer for allocint. x->data is a pointer to an int.
    And therefore &x->data is a ....

    Quote Originally Posted by heathtech
    Okay. I'm no good at figuring out stack traces, but I will try to figure out a work around.
    No time like the present to learn. I can't really help, since you seem unwilling or unable to provide any information about what's going on.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    I don't want to post more than is necessary to ask the question... Since there is still confusion of the purpose of the function, I will post the rest of the code:
    Code:
    int allocint(int** i,int j,int k) {
    	if(x==0) *i = malloc(sizeof(int));
    	else *i = realloc(*i,sizeof(int)*(j+1));
    	if(*i==NULL) return -3;
    	(*i)[j] = k;
    }
    
    //Somewhere else in the main program...
    allocint(&pField[fields]->data,counter++,otherdata);
    "counter" is not necessarily a local variable and "otherdata" is read from a file.

    Maybe this will help clear it up? I just want to know why the &pField[fields].data worked in the first example and now the one above apparently doesn't work. I know that this is the offending function thanks to variously placed printf() statements.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So why not if (j==0) *i = etc.?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Quote Originally Posted by Originally Posted by MK27
    Fair enough. But I still don't get how it becomes a pointer to a pointer for allocint. x->data is a pointer to an int
    And therefore &x->data is a ....
    What do you expect whatever->data to be? an array of ints? Or the literal value of a memory location (0xdeadbeef) that you are going to use how?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. Pointer to next Structure
    By Garfield in forum C Programming
    Replies: 6
    Last Post: 09-16-2001, 03:18 PM