2 registered members (TipmyPip, AndrewAMD),
1,151
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
[SOLVED] Allocated memory + memset
#243818
01/01/09 01:05
01/01/09 01:05
|
Joined: Dec 2008
Posts: 271
Saturnus
OP
Member
|
OP
Member
Joined: Dec 2008
Posts: 271
|
Hello everybody, I would like to use memset to initialize allocated memory to zero. However it does not work as expected. #include <acknex.h>
#include <default.c>
void main()
{
// allocate memory for 100 integers
int *array = (int *)malloc(100 * sizeof(int));
// initialize memory
memset(array, 0, 100 * sizeof(int));
// display int #3 on the screen (should be zero, but isn't)
error(str_for_num(NULL, *(array + 2)));
} In this example 'array' should be initialized to zero, but it isn't: It still contains random values. What am I doing wrong here? Thank you!
Last edited by Kombucha; 01/01/09 17:47. Reason: marked as solved
|
|
|
Re: Allocated memory + memset
[Re: Saturnus]
#243826
01/01/09 01:34
01/01/09 01:34
|
Joined: Aug 2005
Posts: 512 Bayern
Schmerzmittel
User
|
User
Joined: Aug 2005
Posts: 512
Bayern
|
Probiers mal mit einer for-Schleife. Gehe dabei jeden Array durch und setze ihn auf null. Das hier müsste funktionieren. (Also in C++ auf jedenfall)
int array[100];
for(int iCurrentIndex = 0 ; iCurrentIndex < 100 ; iCurrentIndex++)
array[iCurrentIndex] = 0; ] Ich frage mich nur, warum du überhaupt Speicher Reservieren willst. Du arbeites anscheinend mit Lite-C. Da wird der Speicher ja Dynamisch verwaltet, oder täusche ich mich da.
A7 Com V7.80
|
|
|
Re: Allocated memory + memset
[Re: Schmerzmittel]
#243833
01/01/09 02:14
01/01/09 02:14
|
Joined: Dec 2008
Posts: 271
Saturnus
OP
Member
|
OP
Member
Joined: Dec 2008
Posts: 271
|
Ja, for-Schleifen gehen auch. memset würde ich hier aber bevorzugen, weil ich annehme, dass memset schneller ist. : ) memset sollte in Lite-C funktionieren; jedenfalls habe ich Threads gefunden, in denen es benutzt wurde. Ich frage mich nur, warum du überhaupt Speicher Reservieren willst. Du arbeites anscheinend mit Lite-C. Da wird der Speicher ja Dynamisch verwaltet, oder täusche ich mich da. Diese Speicherreservierung benötige ich für dynamische Arrays. Die Allokierung und Freigabe des Speichers passiert in Lite-C nicht automatisch.
|
|
|
Re: Allocated memory + memset
[Re: Saturnus]
#243838
01/01/09 02:52
01/01/09 02:52
|
Joined: Jun 2008
Posts: 24 germany --> nrw --> aachen
Davidus
Newbie
|
Newbie
Joined: Jun 2008
Posts: 24
germany --> nrw --> aachen
|
I believe you expected C to set newly allocated memory/variables to be zero, but they aren't, they contain the same value that has been stored in your RAM by other programs before - they can contain almost anything before they get a value. Don't know, but in some languages arrays contain only NULL values until things are stored, so perhaps it's the same here. The hint given to you by Painkiller ( ) should work, i expect.
|
|
|
Re: Allocated memory + memset
[Re: Davidus]
#243843
01/01/09 03:30
01/01/09 03:30
|
Joined: Dec 2008
Posts: 271
Saturnus
OP
Member
|
OP
Member
Joined: Dec 2008
Posts: 271
|
I believe you expected C to set newly allocated memory/variables to be zero No, I do not expect the allocated memory to be initialized to zero automatically. Thats why I want memset to do this. ; ) However, memset does not work in my example (first post). Of course, I could use for loops as suggested by Schmerzmittel, but obviously memset is faster than for loops. So I wourld prefer to get memset working properly. : )
|
|
|
Re: Allocated memory + memset
[Re: Saturnus]
#243856
01/01/09 09:01
01/01/09 09:01
|
Joined: Apr 2008
Posts: 586 Austria
Petra
Support
|
Support
Joined: Apr 2008
Posts: 586
Austria
|
You're completely right and I had the same problem with my project! Only with hard looking in the online manual I found the solution. "!! sizeof() is a macro and not a function. It can be used for setting a variable or passing a parameter to a function, but can not be used in expressions. Thus, long_size = sizeof(long); is ok, but long_size_times_ten = sizeof(long)*10; is not. " http://manual.conitec.net/structs.htm // allocate memory for 100 integers int *array = (int *)malloc(100 * 4); // initialize memory memset(array, 0, 100 * 4);
|
|
|
Re: [SOLVED] Allocated memory + memset
[Re: Saturnus]
#244127
01/02/09 20:48
01/02/09 20:48
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Ive been using a workaround for some time that works fine for me. Replace the line int *array = (int *)malloc(100 * sizeof(int)); with int *array = (int *)malloc((int)sizeof(int) * 100);
or if you wanted floats use
float *array = (float *)malloc((int)sizeof(float) * 100); Putting the (int) in front of the sizeof() typecasts it into an integer and Ive found it then works fine in arithmetic. If anyoneone wants to dis-agree, let me know as I may need to change a lot of my old coding...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: [SOLVED] Allocated memory + memset
[Re: EvilSOB]
#244275
01/03/09 18:50
01/03/09 18:50
|
Joined: Dec 2008
Posts: 271
Saturnus
OP
Member
|
OP
Member
Joined: Dec 2008
Posts: 271
|
@EvilSOB Thank you for this info. I have tested this workaraound with various data types and obviously its working fine indeed. I wonder if one could use this without any risks? BTW, does somebody know if memset can be used on only a part of an array (or other memory area) in lite-C? I have tried the following: // presumption: array length: 50, type: int
// initialize second half of array to zero
memset(array + 25, 0, 24 * 4);
// memset(&array[25], 0, 24 * 4); // alternative way But this does not work. I guess its not possible to receive an address with address operators or in another way in lite-C?
|
|
|
Re: [SOLVED] Allocated memory + memset
[Re: Saturnus]
#244287
01/03/09 20:23
01/03/09 20:23
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
I use it all the time and have had no problems in over a year, which is only development projects so far, no ACTUAL games as yet. But I use it a lot with large arrays as Im trying to work out some Entity swarm logic processes for creating dynamic/reactive asteroid fields.
Dunno about memset problem, but try this one memset(&(array[25]), 0, 24 * 4);
[EDIT] This one works for me. Tested with the array defined as var* array = (var*)malloc((int)sizeof(var)*50); memset(array[25], 0, 24 * 4);
Last edited by EvilSOB; 01/03/09 20:43. Reason: Corrected.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|