Re: Filling data in function...

From: Tom Junior (tbjnospam_at_automateddesign.com)
Date: 02/25/04


Date: Wed, 25 Feb 2004 13:18:02 GMT

On Wed, 25 Feb 2004 09:54:18 +0100, Karl Heinz Buchegger
<kbuchegg@gascad.at> wrote:

Karl,
Thank you for your help. Your comments are much appreciated.

>
> PS:
> I see that this is an example made up just for the NG. I'm quite
> sure you know that all 255 entries in MyValList point to the
> very same COL_NAME and VALUE.
>

Yes. For the actual program, I use a calloc statement. Here's the actual
line:

        MyTestList =
                calloc(table_num_rows("pet") * table_num_fields( "pet" ), sizeof(struct
SQLValList ) );

May I ask another question?

What is the best way to do pointer arithmetic, in essence stepping through
an array by adding to the pointer (I am learning this as I go along, so I
probably don't know the correct terminology).

For instance, let's say that in the last example, MyLocalData is allocated
a chunk of memory for an array of data. That data is filled up in foo. Now
I need to cycle through those values.

Through troubleshooting, I've found that if the pointer isn't at its start
position when you try to free() it, the program crashes.

What is the favored method of doing what I have below?

void
fetch_all( struct SQLValList * TableData ) {

        int x,y;
        struct SQLValList * MyLocalData;
        struct SQLValList * startMyLocalData;

        MyLocalData = calloc(255, sizeof( SQLValList ) );

        /* to keep things simple, lets assume that MyLocalData is filled with */
        /* column names and values here */
        foo( MyLocalData );

        /* Store the start position of MyLocalData */
        startMyLocalData = MyLocalData;

        /* fill in data */
        for (x=0;x<255;x++) {

                 TableData->COL_NAME = MyLocalData->COL_NAME;
                TableData->VALUE = MyLocalData->VALUE;
                MyLocalData++;
                TableData++;
        }

        /* return MyLocalData to the start position */
        MyLocalData = startMyLocalData;

        /* otherwise I can't free MyLocalData */
        free( MyLocalData );
        MyLocalData = '\0';

} /* fetch_all() */

Thank you again,
Tom

> Thomas wrote:
>>
>> I made a post above specific to the application I am working, but I
>> think
>> the problem is my (lack of) understanding of pointers.
>>
>> My goal is to pass a pointer to a structure to a function which will
>> fill
>> it with data. However, the data seems to be stroed only locally to the
>> function. When the function returns and I try to access the data, parts
>> of
>> it are missing -- I believe that the missing parts are reused parts of
>> memory.
>>
>> Anyway, here is a simplified version of what I am trying to do:
>>
>> struct SQLValList {
>> char * COL_NAME;
>> void * VALUE;
>> };
>>
>> main ( void ) {
>>
>> /* create a pointer to the structure */
>> struct SQLValList * MyValList;
>> int x;
>>
>> /* allocate memory for the structure */
>> /* for right now, I'll just assume 255 elements */
>> MyValList = malloc( sizeof( SQLValList ) * 255 );
>>
>> fetch_all( MyValList );
>>
>> for(x=0; x<255; x++)
>> printf("MyValList %d \t : \t %s %s", x,
>> MyValList->COL_NAME, (char
>> *)MyValList->VALUE );
>>
>> } /* main() */
>>
>> void
>> fetch_all( struct SQLValList * TableData ) {
>>
>> int x;
>> struct SQLValList * MyLocalData;
>>
>> /* to keep things simple, lets assume that MyLocalData is
>> filled with */
>> /* column names and values here */
>> foo( MyLocalData );
>>
>> /* fill in data */
>> for (x=0;x<255;x++){
>>
>> TableData->COL_NAME = MyLocalData->COL_NAME;
>> TableData->VALUE = MyLocalData->VALUE;
>> TableData++;
>> }
>>
>> } /* fetch_all() */
>>
>> So, have I made a permanent change to MyValList back in the main()
>> procedure?
>
> yes
>
>> Did I allocate the memory for that correctly in the first
>> place?
>
> yes. MyValList ist fine.
> But how did you allocate the space for the character strings?
> (in other words: what is happening in function foo() ?)
>
> PS:
> I see that this is an example made up just for the NG. I'm quite
> sure you know that all 255 entries in MyValList point to the
> very same COL_NAME and VALUE.
>

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


Relevant Pages

  • Re: Is There Any Reason to Even Use VC++ Anymore?
    ... If, for another reason, the calling function needs to allocate memory, ... It does this by taking a pointer to a ball object ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Memory management and allocation
    ... > As I'm writing a piece of code that basically acts as a server and ... > memory management is a topic that is quite crucial. ... Or can I just allocate the variable ... Nor is it usually necessary to set the pointer to ...
    (comp.lang.c)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... I kept on getting error messages. ... You don't allocate enough memory here. ... Again conversions between pointer and integer types. ...
    (comp.lang.c)
  • Re: two dimensional arrays:
    ... you can create a 2 dimensional array via following ... >> Since your two allocate functions use the same allocation logic, ... both allocation pointers are of type void pointer. ...
    (comp.lang.c)