I need to store an array in a file. The array length is 1000. I want the fastest disk writing method to work. I know reading a big block from the hard disk is faster because it involves only 1 seek. I seem to have 2 optons :
Option 1) for(i=1 to 1000)
fll%26lt;%26lt;Array[i];
Option 2) Store the array contents into a single string str and then fll%26lt;%26lt;str;
where fll is the file pointer. Which would be faster. I am concerened about the performance because I need to write numericaal data in a file of size around 4 Gb.
http://cs.dal.ca/~koul/
Calling all C/C++ programmers.?
I'm a C programmer, but from the documentation I can find this looks to be true for C++ as well.
The file functions in C and C++ are buffered. This means it doesn't matter whether you write one character at a time to the file or 1000, the file won't really be written to until you fill the file buffer, flush the buffer or close the file.
Probably the best way to optimize this is to write the entire string and let C++ handle it. If the size of the array will always be the same, you could set the buffer to that size; however, it's improbable that your program will be that neat. Another option would be to be sure that the buffer is the optimal size for your hardware; i.e. if your hard drive prefers blocks of 1K, set your buffer to 1K.
Reply:Almost certainly option 2 will be faster, since you only have to do one write to disk, and so the operating system can do all sorts of optimizations. If you write them one at a time, there's not much optimization that can be done.
But, I'd try it on a small example on the real system to make sure :-)
Reply:If it is 4 gigabytes of data you had better not try to store it in an array or a string before writing it out!
Maybe that will be no problem in 2015 but in 2005, most computers do not have 4 GB of memory and even the ones that do would really be unnecessarily burdened by that approach.
Instead, why don't you just write the data out using the plain old C++ insertion operator as you describe in Option 2 - but without storing the contents in a single string beforehand?
By default, your file I/O is going to to be buffered. Let the C++ runtime library and the operating system take care of buffering.
With both approaches you mention in options 1 and 2 - they are going to be doing that anyway. No sense duplicating their work and unnecessarily staging the entire contents of a 4 GB file in memory. That 4 GB of virtual memory is going to wind up being stored on disk anyway, since your process will not have that much real memory available for it anyway. And _that_ is going to be *really* slow.
Just make sure your I/O is buffered, not unbuffered, and you should be fine. Benchmark it if you like, just to be sure. Don't be surprised though that when you try to create a 4 GB string - you have a lot of performance problems or something aborts.
bottle palm
Saturday, May 22, 2010
Hw help c++?
1)An object’s non-static member functions have access to a “self pointer” to the object called the _____ pointer.
2)The keyword _____ specifies that an object or variable is not modifiable after it is initialized.
3)If a member initializer is not provided for a member object of a class, the object’s _____ is called.
4)A member function should be declared static if it does not access _____ class members.
5)Member objects are constructed _____ their enclosing class object.
Hw help c++?
RTFM
Read your books and do your homework.
If you have others do this for you, you will not learn anything.
2)The keyword _____ specifies that an object or variable is not modifiable after it is initialized.
3)If a member initializer is not provided for a member object of a class, the object’s _____ is called.
4)A member function should be declared static if it does not access _____ class members.
5)Member objects are constructed _____ their enclosing class object.
Hw help c++?
RTFM
Read your books and do your homework.
If you have others do this for you, you will not learn anything.
In C, what is the difference between * and **.?
Like a FILE * and a FILE **. I used to think it was a typo when the was two asterisks, but now I have realized thta it actually is something different. What is it? It seems hard to imagine it being a pointer to a pointer lol
In C, what is the difference between * and **.?
FILE * is a pointer to a certain memory address which contains a FILE object. This can simply point to another FILE, or it can be used to point to a space in memory that has been allocated with malloc to hold an array of FILE's. If this is the case, you can access it like an array, like file[2], for example.
FILE ** is a pointer to a pointer, and it operates much like a FILE *, but if it is allocated correctly, could be an array of arrays, accessed like file[3][2];
I recommend you read up on memory address/allocation, pointers, and arrays. http://cprogramming.com is a good place to start.
Reply:FILE * is a pointer to a FILE type, it is a pointer to to the address in memory.
FILE ** is a pointer to a pointer to a FILE type, so it is a pointer to the address value.
FILE * can also be thought of as an array of FILEs
FILE** is an 2D array of FILEs,
Hope this helps
In C, what is the difference between * and **.?
FILE * is a pointer to a certain memory address which contains a FILE object. This can simply point to another FILE, or it can be used to point to a space in memory that has been allocated with malloc to hold an array of FILE's. If this is the case, you can access it like an array, like file[2], for example.
FILE ** is a pointer to a pointer, and it operates much like a FILE *, but if it is allocated correctly, could be an array of arrays, accessed like file[3][2];
I recommend you read up on memory address/allocation, pointers, and arrays. http://cprogramming.com is a good place to start.
Reply:FILE * is a pointer to a FILE type, it is a pointer to to the address in memory.
FILE ** is a pointer to a pointer to a FILE type, so it is a pointer to the address value.
FILE * can also be thought of as an array of FILEs
FILE** is an 2D array of FILEs,
Hope this helps
Question about linked lists in C?
When you free the last element of a dynamically allocated linked list ( something like "free(cur);" ), does the next pointer of the previous link node then become a NULL pointer? Or does it still point to the location of the freed memory? That is, do I have to explicitly set that next pointer (now the tail of the list) to NULL?
Question about linked lists in C?
You have to explicitly set the pointer to null.
Indeed you have to manage all the pointer for manipulating lists in C. I usually dont call free directly but do it in a Destroy function. If you have a list of 'CAT' objects for example your function would be like this -
void CatDestroy (CAT *cat)
{
if (cat-%26gt;next) cat-%26gt;next-%26gt;previous = cat-%26gt;previous;
if (cat-%26gt;previous) cat-%26gt;previous-%26gt;next = cat-%26gt;next;
free (cat);
}
This gives you a degree of extra flexibility. For example you can change how a CAT is allocated so that its dosent use free() and malloc(). You could decrement the number of cats variable. You could link the CAT into a list of unused CATs rather than freeing it. You could update a pointer to the tail of the list.
Question about linked lists in C?
You have to explicitly set the pointer to null.
Indeed you have to manage all the pointer for manipulating lists in C. I usually dont call free directly but do it in a Destroy function. If you have a list of 'CAT' objects for example your function would be like this -
void CatDestroy (CAT *cat)
{
if (cat-%26gt;next) cat-%26gt;next-%26gt;previous = cat-%26gt;previous;
if (cat-%26gt;previous) cat-%26gt;previous-%26gt;next = cat-%26gt;next;
free (cat);
}
This gives you a degree of extra flexibility. For example you can change how a CAT is allocated so that its dosent use free() and malloc(). You could decrement the number of cats variable. You could link the CAT into a list of unused CATs rather than freeing it. You could update a pointer to the tail of the list.
Basic C++ help plz?
void getNumber(int %26amp;n)
{
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; n;
}
In this function, the parameter n is a reference variable. Rewrite the function so that n is pointer.
How do I change the n to a pointer?
Thanks.
Basic C++ help plz?
You change the function declaration from:
void getNumber(int %26amp;n)
to:
void getNumber(int *pn)
Now pn is a pointer to an integer. Now instead of using n directly, such as:
n = 3; or x = n/2; you have to dereference the pointer. The equivalent statements would be:
*pn = 3; or x = *pn/2;
What's happening is pn holds the address of an integer. To get the contents (i.e. the actual integer value) you use the * operator.
You can see the difference in the following output:
cout %26lt;%26lt; "pointer address: " %26lt;%26lt; pn %26lt;%26lt; endl;
cout %26lt;%26lt; "integer value: " %26lt;%26lt; *pn %26lt;%26lt; endl;
Pointers are useful for dealing with blocks of data (e.g. an array of 100 integers):
void func(int *block, int block_size)
{
for (int i = 0; i != block_size; ++i)
{
cout %26lt;%26lt; *(block + i) %26lt;%26lt; ", "; // use ith integer from block
}
}
They are also useful for optional data.
void func2(int v1, int *option)
{
cout %26lt;%26lt; "v1: " %26lt;%26lt; v1 %26lt;%26lt; endl; // use v1
if (option != NULL)
cout %26lt;%26lt; "option: " %26lt;%26lt; *option %26lt;%26lt; endl;
}
Reply:By use of the dereferencing operator, '*'.
Pointer creation =%26gt; char * My_Char_Pointer;
To initialize a pointer =%26gt;
My_Char_Pointer = %26amp; other_char;
To change the data stored at the pointer's address =%26gt;
*My_Char_Pointer = Yet_another_Char;
I hope this helps you out. There is a link to a good C++ reference below, if you need more information. Pivy.
magnolia
{
cout %26lt;%26lt; "Enter a number: ";
cin %26gt;%26gt; n;
}
In this function, the parameter n is a reference variable. Rewrite the function so that n is pointer.
How do I change the n to a pointer?
Thanks.
Basic C++ help plz?
You change the function declaration from:
void getNumber(int %26amp;n)
to:
void getNumber(int *pn)
Now pn is a pointer to an integer. Now instead of using n directly, such as:
n = 3; or x = n/2; you have to dereference the pointer. The equivalent statements would be:
*pn = 3; or x = *pn/2;
What's happening is pn holds the address of an integer. To get the contents (i.e. the actual integer value) you use the * operator.
You can see the difference in the following output:
cout %26lt;%26lt; "pointer address: " %26lt;%26lt; pn %26lt;%26lt; endl;
cout %26lt;%26lt; "integer value: " %26lt;%26lt; *pn %26lt;%26lt; endl;
Pointers are useful for dealing with blocks of data (e.g. an array of 100 integers):
void func(int *block, int block_size)
{
for (int i = 0; i != block_size; ++i)
{
cout %26lt;%26lt; *(block + i) %26lt;%26lt; ", "; // use ith integer from block
}
}
They are also useful for optional data.
void func2(int v1, int *option)
{
cout %26lt;%26lt; "v1: " %26lt;%26lt; v1 %26lt;%26lt; endl; // use v1
if (option != NULL)
cout %26lt;%26lt; "option: " %26lt;%26lt; *option %26lt;%26lt; endl;
}
Reply:By use of the dereferencing operator, '*'.
Pointer creation =%26gt; char * My_Char_Pointer;
To initialize a pointer =%26gt;
My_Char_Pointer = %26amp; other_char;
To change the data stored at the pointer's address =%26gt;
*My_Char_Pointer = Yet_another_Char;
I hope this helps you out. There is a link to a good C++ reference below, if you need more information. Pivy.
magnolia
Visual C++ Programmers help...!!!!...?
I have created a mini application (just for fun).
My main objective is that when application is in fullscreen, timer starts counting in seconds until it reaches 4 e.g. for every four seconds, mouse pointer will disappear and will appear again when mouse is moved.
I am using MFC. My problem is that when I use the predefined function OnFullScreen(BOOL bFullScreen) and bFullScreen was already set to true, and when I run the application, mouse pointer still remains on the screen when reaches 4 seconds.
My code for OnFullScreen is this:
void MyView :: OnFullScreen(BOOL bFullScreen)
{
// I also tried if statements for this... and still it doesn't work
switch(bFullScreen)
{
case true: MessageBox("Sample Test", "Check", MB_OK);
break;
default:
MessageBox("is not true", "Check", MB_OK);
}
Please help. I assure you that when I am satisfied with your answer, I will give you the credits by choosing you as the best answerer...
Thanks and God Bless
Visual C++ Programmers help...!!!!...?
How do you hide the pointer ?
ShowCursor(FALSE) must hide the mouse pointer:
http://msdn2.microsoft.com/en-us/library...
The message box that you show may be messing it up. You may also want to set Window's class cursor:
SetClassLong(m_hWnd, GCL_HCURSOR, NULL);
This changes Window class cursor and helps prevent the system from changing it when the mouse is moved.
My main objective is that when application is in fullscreen, timer starts counting in seconds until it reaches 4 e.g. for every four seconds, mouse pointer will disappear and will appear again when mouse is moved.
I am using MFC. My problem is that when I use the predefined function OnFullScreen(BOOL bFullScreen) and bFullScreen was already set to true, and when I run the application, mouse pointer still remains on the screen when reaches 4 seconds.
My code for OnFullScreen is this:
void MyView :: OnFullScreen(BOOL bFullScreen)
{
// I also tried if statements for this... and still it doesn't work
switch(bFullScreen)
{
case true: MessageBox("Sample Test", "Check", MB_OK);
break;
default:
MessageBox("is not true", "Check", MB_OK);
}
Please help. I assure you that when I am satisfied with your answer, I will give you the credits by choosing you as the best answerer...
Thanks and God Bless
Visual C++ Programmers help...!!!!...?
How do you hide the pointer ?
ShowCursor(FALSE) must hide the mouse pointer:
http://msdn2.microsoft.com/en-us/library...
The message box that you show may be messing it up. You may also want to set Window's class cursor:
SetClassLong(m_hWnd, GCL_HCURSOR, NULL);
This changes Window class cursor and helps prevent the system from changing it when the mouse is moved.
C++ how do I point to an element in an existing array?
How can I create a pointer that points to a specific item in an existing array (say item 5) and will let me then use that pointer like an array?
Here is my code:
http://www.marcovitanza.com/pointerArray...
C++ how do I point to an element in an existing array?
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = array + 4;
Now ptr points to the 5th element in the array (remembering element 1 is offset 0)
Here is my code:
http://www.marcovitanza.com/pointerArray...
C++ how do I point to an element in an existing array?
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int *ptr;
ptr = array + 4;
Now ptr points to the 5th element in the array (remembering element 1 is offset 0)
Subscribe to:
Posts (Atom)