I think it's time to contact your professor . . .
For now, you should probably malloc() an array of that size rather than declaring it.
I think it's time to contact your professor . . .
For now, you should probably malloc() an array of that size rather than declaring it.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Okay, I see what he wants you to do now. Most likely, he wants you to just hardcode to page size for each platform when you recompile the program on that platform.
>It is also too large to be dynamically created with a call to malloc().
This is still incorrect.
>The only remaining option is to make it global.
This may work, if he's asking what I think he's asking. It's not guaranteed though, which makes the problem dicey at best. Just declare a global array (let's assume for the example that the page size is 4096) and hope that your implementation "correctly" handles the object code for it:
You would be much better off using malloc, but unless you post the code you're using, we can't help you troubleshoot the warnings you've been getting.Code:#include <stdio.h> char global[20480][4096]; int main ( void ) { return 0; }
My best code is written with the delete key.
Correct me if I'm wrong. . . but can one not call malloc() asking for the size of memory and get it regardless of how much is already in memory (assuming that some type of swapping exists)?Originally Posted by groberts1980
malloc() is not guaranteed to return newly allocated memory, if that's what you're suggesting. Some systems might not have any free disk space for a swap file or virtual memory, or no swap file support altogether. Or a DOS program might not be allowed to allocate over 64/640KB.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>can one not call malloc() asking for the size of memory and get it regardless of how much is already in memory
It depends. Any decent memory manager will eat into virtual memory at some point, and you can more or less allocate an "infinite" amount with malloc. However, requests for single large blocks are less likely to be honored than multiple small blocks because the manager still has to manage the fragments. Even if you have a terabyte of free virtual space, malloc may fail if you ask for a terabyte and the space is split in the middle by a single byte reserve.
My best code is written with the delete key.
I'm not getting compiler errors this time. I had a bunch of commented out code for later use in the program. Once I took that out, the compiler errors went away. So with the above code, I have a fully initialized array like this?Code:#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char ** argv) { int getpagesize(void); int columns; columns = getpagesize(); char ** p = malloc (20480 * sizeof *p); int i; for(i = 0; i < 20480; i++) p[i] = malloc (columns * sizeof p[i]); return(0); }
I may have misunderstood his requirements. I assumed he wanted us to call getpagesize() and set the value at runtime. Possible that he wants us to hard code the array size for the machine before compiling on that platform.Code:p[20480][4096]
Last edited by groberts1980; 11-14-2006 at 03:08 PM.
No, not suggesting that malloc() always returns a valid memory location. . . just saying that, for example, on Linux (with a large swap space, ie, 2X memory), one could request for most of memory and get it. Might have to run throug a loop to get the max, but still could get almost all of it.
Which only supports the argument that you should be able to allocate enough memory.
Your code uses C99-style mid-block variable declarations, which your compiler might not like. Nor do you free any of that memory that you allocate, which is a bad idea when you allocate so much. Other than that, your code looks good.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>So with the above code, I have a fully initialized array like this?
Functionally, yes. In reality, that's probably not what you want to test paging and virtual memory because the memory you've allocated probably isn't contiguous. To get it all clumped together in one big block, you would do something more like this:
Code:char *p = malloc ( 20480 * 4096 );
My best code is written with the delete key.
I think he wants you to run a separate program which calls getpagesize(). Then once you have the page size create a new program to create and access the global array:I may have misunderstood his requirements. I assumed he wanted us to call getpagesize() and set the value at runtime. Possible that he wants us to hard code the array size for the machine before compiling on that platform.
Code:#include <stdlib.h> #include <stdio.h> char [20480][4096]; //Number returned from getpagesize(), in this example 4096 int main() { //Fill the array, etc }
I emailed him, this was his response:
"The page size needs to be hardcoded. I'll explain why in lecture next time. For now, just write a 2-line program to print the value and plug that value into your VirtMem program."
So, write a program to print the pagesize (already done), run that program on a given platform, hard code that value into my array program, re-compile on that platform, run program. Sound about right?
He said we would run into problems using malloc to create the array, but I seem to have no problems on my machine. But I do have 1GB of ram and a 2GB swap file for linux. Is that why it runs fine on my system?
>He said we would run into problems using malloc to create the array
You can keep repeating what he said, but it doesn't make him any more right. The only time I would expect problems with malloc is on an embedded platform, or an ancient and gimped OS. However, it's generally a bad idea to tell your teacher he's wrong, even if he is. So just do what he wants and keep in mind that he might just be clueless.
My best code is written with the delete key.