Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Best Method for Clearing an Array of Struct

Ed HallEd Hall Member Posts: 395
I'm programming this in win32 with C++. I have a struct with 47 elements of varying types from strings to BOOLs. I then set up an array of 1000 entries for this struct. I read/write these from/to a file. Some of the operations include deleting some of the records. I'd like to fully clear the array prior to my read operations. Is this how I should clear it:

[code]

struct competitors{
char CompNum[7];
...
BOOL Sen, Jun, Wom, ...;
int ...;
};

competitors comp[1000];

...


memset(comp, 0, sizeof(competitors)*1000);
[/code]

Or, can I just use:

[code]
memset(comp, 0, sizeof(comp));
[/code]

Or is there something better since this is in Windows?

Thanks.

Take Care,
Ed

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : I'm programming this in win32 with C++. I have a struct with 47
    : elements of varying types from strings to BOOLs. I then set up an
    : array of 1000 entries for this struct. I read/write these from/to a
    : file. Some of the operations include deleting some of the records.
    : I'd like to fully clear the array prior to my read operations. Is
    : this how I should clear it:
    :
    : [code]:
    :
    : struct competitors{
    : char CompNum[7];
    : ...
    : BOOL Sen, Jun, Wom, ...;
    : int ...;
    : };
    :
    : competitors comp[1000];
    :
    : ...
    :
    :
    : memset(comp, 0, sizeof(competitors)*1000);
    : [/code]:
    :
    : Or, can I just use:
    :
    : [code]:
    : memset(comp, 0, sizeof(comp));
    : [/code]:
    :
    : Or is there something better since this is in Windows?
    :
    : Thanks.
    :
    : Take Care,
    : Ed
    :

    The second code only clears the first item, so you'll need to use the first code (with *1000). I can't think of any 'better' way to do it.
    However, clearing the entire array shouldn't take too long, since it's 'only' 1000 times the size.
    I think this is the best code, unless you want to start thinking about more difficult construction to perhaps gain a little bit of speed?
    On an interesting side-note, when programming for windows you notice that routines such as memset are just wrappers for the corresponding windows API's.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • Ed HallEd Hall Member Posts: 395
    Thanks,

    I do understand about the wrapper part, although I don't uderstand enough to use the APIs directly yet.

    I've been trying to get a "handle":-) on using IStream and CreateStreamOnHGlobal. If I can figure it out enough, I may use it in my current project for both my struct array and a text buffer I'm building that will grow from a few thousand up to ?? It seems like it would be perfect.

    Take Care,
    Ed
  • LundinLundin Member Posts: 3,711
    memset(comp, 0, sizeof(comp));

    No, that clears all 1000 items since the array is statically allocated. Multiplying sizeof(comp) with 1000 will lead to a nasty crash.

    One thing you might have to be aware of however, is that the sizeof() one struct item isn't necessarily equal to the sum of sizeof() for all members in that struct. This is because of struct padding.
  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : memset(comp, 0, sizeof(comp));
    :
    : No, that clears all 1000 items since the array is statically
    : allocated. Multiplying sizeof(comp) with 1000 will lead to a nasty
    : crash.

    Ahh yeah, my bad. Read competitors for comp :P
    Anyway, it's probably a good idea to use the sizeof(struct xyz)*arraysize method, so it works with dynamic arrays as well.

    In response to Ed Hall:
    :I do understand about the wrapper part, although I don't uderstand enough
    : to use the APIs directly yet.

    If I remember correctly (I've spent a lot of time browsing the VC++ 6.0 and 2005 Win32 SDK code), then memset has a couple of preprocessor switches, and either it does a loop something like "while( cnt-- ) *pch++ = val",
  • Ed HallEd Hall Member Posts: 395
    Thanks for all the help. A couple more items:

    Doesn't the padding happen at creation of the struct? Therefore, wouldn't the sizeof() return the true size? Or, would there possibly be padding between array elements?

    There's an article I've been studying about the IStream use at:

    http://www.codeproject.com/KB/cpp/memorystream.aspx

    I'm almost at a point where I'm ready to experiment...

    I looked at RtlFillMemory - Isn't it odd that MS swaps the parameter order between this and memset()?

    I also saw RtlZeroMemory, which looks like more of what I need, although MS also suggests RtlSecureZeroMemory since it says the compiler might optimize RtlZeroMemory away. I'm hoping they mean when the program exits, data my be left and not when I call for a clearing...

    Thanks again for all the help.

    Take Care,
    Ed
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Thanks for all the help. A couple more items:
    :
    : Doesn't the padding happen at creation of the struct? Therefore,
    : wouldn't the sizeof() return the true size? Or, would there
    : possibly be padding between array elements?
    :
    : There's an article I've been studying about the IStream use at:
    :
    : http://www.codeproject.com/KB/cpp/memorystream.aspx
    :
    : I'm almost at a point where I'm ready to experiment...
    :
    : I looked at RtlFillMemory - Isn't it odd that MS swaps the parameter
    : order between this and memset()?
    :
    : I also saw RtlZeroMemory, which looks like more of what I need,
    : although MS also suggests RtlSecureZeroMemory since it says the
    : compiler might optimize RtlZeroMemory away. I'm hoping they mean
    : when the program exits, data my be left and not when I call for a
    : clearing...
    :
    : Thanks again for all the help.
    :
    : Take Care,
    : Ed
    :
    [color=Blue]sizeof () always return a true size. However, as someone said - the SUM of sizeof()-s for each field MAY NOT BE the sizeof() of the whole. For clearing memory just use sizeof() - if there are any padding - they will be just set to zero - no big issue. In fact, if you do that BEFORE filling the structure - you can also use memcmp() to compare them, because each structure will have zeroes in padding bytes.[/color]
Sign In or Register to comment.