I would like to write a C-program that allocates 20,000 memory lines with each line containing 8 bytes and are labeled in the range 2500 to 24999.. The program actually simulates a simple assembly language IDE. All the 20,000 memory lines may or may not be used at once. Suggest me how to allocate memory for those locations in the C program. Should it be a static allocation? Visit http://screencast.com/t/69T7u0avH
Asked
Active
Viewed 87 times
0
-
1) What is a "memory line"? Do you mean you want to allocate 20000 * 8 bytes? – Mark Segal May 15 '15 at 11:34
-
2) What do you mean by "are labeled in the range 2500 and 24999" ? – Mark Segal May 15 '15 at 11:34
-
3) What do you mean by "how to allocate memory for those locations"? – Mark Segal May 15 '15 at 11:35
-
2I don't want to discourage you, but the way that you have worded your question makes me wonder if this might be a little bit beyond you... – David Hoelzer May 15 '15 at 11:39
-
@Mark Segal, please check http://screencast.com/t/69T7u0avH – lapita May 15 '15 at 11:47
-
So you basically wish to allocate 20000 * 8 bytes, and access then as "8 byte lines". It is exactly the same as writing `long long array[20000]` and accessing it normally. – Mark Segal May 15 '15 at 11:52
-
@lapita please elaborate your question. What do you want to achieve exactly. Your question is likely to be closed soon because "It's unclear what you are asking". – Jabberwocky May 15 '15 at 11:54
-
Yes, but I'm asking is there any other way for allocation other than just statically defining an array ? – lapita May 15 '15 at 11:56
-
@lapita: yes of cours. It's called "dynamic allocation". Use the `malloc` function. It's very basic C knowledge. – Jabberwocky May 15 '15 at 11:57
1 Answers
1
Try
unsigned char (*ptrtomem)[8];
unsigned char (*ptr)[8];
/* ... */
ptrtomem = malloc(20000*8);
ptr = ptrtomem-2500;
/* use ptr[2500][] to ptr[24999][] */
/* when done */
free(ptrtomem);
or use _alloca() (or alloca() depending on compiler) if you want to allocate off the stack.

rcgldr
- 27,407
- 3
- 36
- 61