MAMEWorld >> Programming
View all threads Index   Threaded Mode Threaded  

Pages: 1

italieAdministrator
MAME owes italie many thank yous, hah
Reged: 09/20/03
Posts: 15246
Loc: BoomTown
Send PM


g*DELETED* *DELETED*
#170817 - 11/10/08 02:49 AM


Post deleted by italie



italieAdministrator
MAME owes italie many thank yous, hah
Reged: 09/20/03
Posts: 15246
Loc: BoomTown
Send PM


C coders, what method do you use to clear a character array? new [Re: italie]
#170818 - 11/10/08 02:54 AM


Do you blow away the address, nt caring about the data?

Do you blow away the data in memory?

Do you allocate and de allocate?

my preference:


Code:

 for(int i = 0; i > sizeof(CharArray); i++){ CharArray[i] = '/0';} 




Just getting a feel for who's doing what, had a lengthy discussion about this earlier and I'm curious....



krick
Get Fuzzy
Reged: 02/09/04
Posts: 4235
Send PM


Re: C coders, what method do you use to clear a character array? new [Re: italie]
#170824 - 11/10/08 05:44 AM


> Do you blow away the address, nt caring about the data?
>
> Do you blow away the data in memory?
>
> Do you allocate and de allocate?
>
> my preference:
>
> for(int i = 0; i > sizeof(CharArray); i++){ CharArray = '/0';}
>
>
> Posting in the 'bin as I'm expecting this thread to stray a bit...


try memset()

http://www.codecogs.com/reference/c/string.h/memset.php



GroovyMAME support forum on BYOAC



Anonymous
Unregistered
Send PM


Re: C coders, what method do you use to clear a character array? new [Re: krick]
#171801 - 11/19/08 11:59 AM


> > Do you blow away the address, nt caring about the data?
> >
> > Do you blow away the data in memory?
> >
> > Do you allocate and de allocate?
> >
> > my preference:
> >
> > for(int i = 0; i > sizeof(CharArray); i++){ CharArray = '/0';}
> >
> >
> > Posting in the 'bin as I'm expecting this thread to stray a bit...
>
>
> try memset()
>
> http://www.codecogs.com/reference/c/string.h/memset.php

the equivalent would be:

memset(CharArray, 0, sizeof(CharArray));

You can't deallocate/allocate arrays & sizeof only does what you want for arrays & not pointers. If CharArray was a pointer then sizeof(CharArray) would return the size of the pointer ( sizeof(*CharArray) would return the size of what the pointer points at ). To use pointers you might want to use calloc ( malloc doesn't guarantee that the memory will be cleared at all ) & then if you later wanted to clear it you need to keep the size that you allocated and pass that into memset instead.



Mario Figueiredo
MAME Fan
Reged: 10/17/08
Posts: 66
Send PM


Re: C coders, what method do you use to clear a character array? new [Re: ]
#172174 - 11/23/08 04:19 AM


> You can't deallocate/allocate arrays & sizeof only does what you want for arrays &
> not pointers. If CharArray was a pointer then sizeof(CharArray) would return the size
> of the pointer ( sizeof(*CharArray) would return the size of what the pointer points
> at ). To use pointers you might want to use calloc ( malloc doesn't guarantee that
> the memory will be cleared at all ) & then if you later wanted to clear it you need
> to keep the size that you allocated and pass that into memset instead.

Meanwhile, one more thing come to mind...

If you are using a char array as a storage area that needs to be repopulated with new data try instead to follow the maxim that says that the fastest way to do something is not doing it at all. On this case consider building your array as such:


Code:

struct my_data
{
int count;
char data[100];
};



Repopulating the array is then a matter of updating my_data.count and doing your business on my_data.data, overwriting needed elements and leaving be any unused ones.

This answers any issues regarding performance. But comes at the cost of memory efficiency.

If this is a problem, or the struct doesn't somehow fit into your plans(?), then memset is the best option indeed and you should fallback to smf's post.

Edited by Mario Figueiredo (11/23/08 04:20 AM)



Anonymous
Unregistered
Send PM


Re: C coders, what method do you use to clear a character array? new [Re: Mario Figueiredo]
#175669 - 12/30/08 12:10 PM


keeping the count only really works if it's a string that you're storing, for C you generally use null terminated strings so putting a 0 at the start would achieve the same thing. if you're storing something like useage counts or the char array is a bitmap you're drawing into then you will need to clear it properly.

also clearing memory is useful for security, you don't want to keep passwords around in ram longer than you need.


Pages: 1

MAMEWorld >> Programming
View all threads Index   Threaded Mode Threaded  

Extra information Permissions
Moderator:  Pi 
0 registered and 30 anonymous users are browsing this forum.
You cannot start new topics
You cannot reply to topics
HTML is enabled
UBBCode is enabled
Thread views: 3649