Re: WinCE Struct Help

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Paul G. Tobey [eMVP] (ptobey)
Date: 03/23/05


Date: Wed, 23 Mar 2005 09:03:48 -0700

You can allocate *anything* with malloc() except a C++ object that needs to
have its constructor called, so you can allocate your array with it, you can
allocate a single struct instance with it, whatever you'd like. A strategy
that you might take is to use malloc() to allocate your initial array and,
when you decide that, hey, my array is full, you might use realloc() to
reallocate it to a bigger size. realloc(), as you can see from the help,
will copy the current contents of the array to the new memory block before
returning. You would then count on the fact that a pointer and an array are
accessed pretty much interchangeably in C.

typedef
    struct
    {
        int a;
        int b;
        int c;
        int d;
    } mystruct;

mystruct *structarray = 0;
int structarrayelements = 0;

#define INITIAL_ARRAY_ELEMENTS 10

main()
{
    // ...

    // During initialization, you have to allocate the initial array.
    structarray = malloc( sizeof( mystruct ) * INITIAL_ARRAY_ELEMENTS );
    structarrayelements = INITIAL_ARRAY_ELEMENTS;

    // Access items in the array just as though it was statically allocated,
as in your
    // example.
    structarray[ 3 ].a = 6;

    // If you decide that you need more space, say newArrayCount elements,
you'd
    // do something like this:
    mystruct *newstructarray = realloc( structarray, sizeof( mystruct ) *
newArrayCount );

    // ***Don't forget to handle errors here and end my assigning
structarray = newstructarray;,
    // if there were no errors!***

}

I think that the original response was a good one. This isn't a how do I
program in C tutorial group and malloc has to be one of the most-covered
subjects in any C documentation. We don't expect you to be an expert, but
we *do* expect you to help yourself as much as possible, rather than relying
on us to hold your hand. Read the help, read your programming references,
try to do the right thing; if it doesn't work, post. We aren't getting paid
for this...

Paul T.

"Vin" <Vin@discussions.microsoft.com> wrote in message
news:24E68EE0-17BE-4FBB-8994-28E7B024ADE4@microsoft.com...
> Man this is a tough area to ask for help. I asked a simple question and
> instead of getting a simple helpful hints or example, I got a lecture
> about
> courses in college. Dang it must been almost 20 years since I was in
> program
> 101. I don't live inside of books. At times I forget simple things, and
> that
> is why I am here.
>
> I know that you guys are gurus at these things, but when you read these
> posts put yourself in these people's shoe. We are not gurus like you
> guys.
> Some of us just started programming while others are more advanced. We
> all
> can't be like you or wanted to be like you guys.
>
> thanks for the help :)
>
> "Todd Krochta" wrote:
>
>> I think what Phil means is that what you're talking about doing would be
>> solved by implementing a linked list or some other basic programming
>> structure which uses dynamic list allocation and deallocation. This type
>> of
>> programming ("Intro to Data Structures") is usually covered very early on
>> in
>> a software engineering ciriculum.
>>
>> --
>> Todd Krochta
>> Applied Data Systems
>>
>>
>> "vin" <vin@discussions.microsoft.com> wrote in message
>> news:B063B28F-1DA5-472B-ACB5-49F253547F7B@microsoft.com...
>> > Hi Phil,
>> >
>> > what do you mean basic concept of malloc and realloc?? I know how to
>> > use
>> > these functions. But I want to use struct. Are you saying that we can
>> > use
>> > malloc or realloc function for struct?
>> >
>> > some example would be great.
>> >
>> > "Phil Frisbie, Jr." wrote:
>> >
>> >> Vin wrote:
>> >>
>> >> > Hi,
>> >> >
>> >> > I developed an application using WinCe 4.0 for Pocket PC 2002.
>> >> >
>> >> > I have defined a struct as global:
>> >> >
>> >> > //events struct
>> >> > struct EVENTS
>> >> > {
>> >> > long m_eventID;
>> >> > BYTE m_pubKey[148];
>> >> > ULONGLONG m_openGate;
>> >> > ULONGLONG m_closeGate;
>> >> > };
>> >> > EVENTS events[25];
>> >> >
>> >> > I have allocated 25 arrays for this struct. My goal is to use this
>> >> > struct to
>> >> > hold data for at least 25 events.
>> >> >
>> >> > My question are:
>> >> > 1. is this how I go about doing it?
>> >>
>> >> It is one way.
>> >>
>> >> > 2. Can I reallocat or expand this struct to more than 25 arrays
>> >> > later
>> >> > on in
>> >> > the program?
>> >>
>> >> No.
>> >>
>> >> > If I need to hold 40 events info, can I expand it? if so, would
>> >> > you please give me a code example of doing it?
>> >>
>> >> No. This is VERY basic C programming. Look up malloc() and realloc()
>> >> in
>> >> your
>> >> compiler documentation.
>> >>
>> >> > Many Thanks
>> >>
>> >> --
>> >> Phil Frisbie, Jr.
>> >> Hawk Software
>> >> http://www.hawksoft.com
>> >>
>> >>
>>
>>
>>



Relevant Pages

  • Re: MFC Interview Tests
    ... The intersect rect problem that I give applicants is for them to actually ... my past experience (18 years of VC++ programming). ... way to allocate memory", so I said "malloc or new. ...
    (microsoft.public.vc.mfc)
  • Re: MFC Interview Tests
    ... That's one smart kid for figuring out the problems with BASIC, ... what might be real world programming practices to you might not be to ... way to allocate memory", so I said "malloc or new. ...
    (microsoft.public.vc.mfc)
  • malloc questions in C
    ... if I use malloc() in a function to allocate a space for my array of ... Prev by Date: ...
    (comp.lang.c)
  • Re: C or C++ for FOSS/linux?
    ... On 2006-12-29, Sebastian 'lunar' Wiesner wrote: ... I don't know much of C programming, but that is known to me as the way ... you allocate an array of 100 integers on the heap in C. ...
    (comp.os.linux.development.apps)
  • Re: C or C++ for FOSS/linux?
    ... I don't know much of C programming, but that is known to me as the ... way you allocate an array of 100 integers on the heap in C. ... Freedom is always the freedom of dissenters. ...
    (comp.os.linux.development.apps)