|
Very true. And the best way to learn is to take a properly structured course, or buy a good book on the language.
|
|
|
|
|
OK,I am looking at the C++ Primer Plus of Stephen Prata these days. What else do you recommend?
|
|
|
|
|
Sorry, it is a long time since I read any C++ books, you will need to research for yourself.
|
|
|
|
|
Well, I wish myself success in learning programming, thanks,best wishes to you!
|
|
|
|
|
While, like Richard, I haven't read any (recent) books on C++, The C++ programming Language[^] is still pretty much a must-have. Bjarne Stroustrup keeps updating it to make sure it covers all the new features of the constantly evolving C++ standard (currently C++17, working on C++20). And it provides lots of useful examples that help understand all the great features of this language.
Other than that, I suggest searching the web for articles from specific Book authors such as Herb Sutter (check out his Guru of the Week series[^] !) , Alexei Alexandrescu, or Scott Hanselman.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
|
I am referring to the added information in your other responses, specifically your goal to implement a memory pool.
First, it seems to me you've set your goal prematurely: You're worrying about the performance overhead of overusing new/delete, but I'm not at all sure that this will really be relevant in your environment.
- For one, your application may be fast enough as it is. Any time spent on optimization is wasted.
- Second, even if it is slow, memory allocation may only eat ~10% of the total processing time. therefore even a 100-fold speedup can not increase your app speed by more than 10%.
- Third, even if the amount of time your app spends on memory management is really excessive, the problem is most likely the programming that requires so much allocations and deallocations, not the speed of each individual allocation. You can very likely improve the speed much more by fixing that programming rather than fixing your memory handling.
- Fourth, the systems memory management functions are already very efficient. Doing a billion allocations and deallocations takes only a few seconds. It's not so easy to beat that without a very thorough understanding of memory management as well as a good understanding of the memory dynamics of your application. And if you do understand what your app needs, you should be able to come up with a program design that doesn't lean so heavily on memory management.
All that said, if you still want a memory manager, why not use what you can already find on the web? For instance here is a neat memory manager that is already implemented as an allocator for using it with STL containers: GitHub - foonathan/memory: STL compatible C++ memory allocator library using a new RawAllocator concept that is similar to an Allocator but easier to use and write.[^] . Being an allocator means you can just use STL containers rather than custom containers and don't need to care about explicitely allocating or deallocating individual elements.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Hi,
I am writing a DLL in which certain data have to be shared between all processes, and all threads accessing this DLL. This is put in a Shared section in the DLL. Do the sync objects such as CRITICAL_SECTION, MUTEX, etc. still work as expected? or are there caveats to be weary of. It is something that is very difficult so test or simulate!
Regards,
Bram van Kampen
|
|
|
|
|
Quote: CRITICAL_SECTION, MUTEX, etc. still work as expected? Normally, they do test before releasing the libraries and SDKs, but it still depends on which runtime and its version are you using, as well as what is the deployment OS.
Did you check the documentation? About Synchronization | Microsoft Docs.
Also, please read this reference of synchronization to get to know a bit more about these topics, mutex, critical sections, etc. Synchronization Functions | Microsoft Docs, there are remarks for each function and the type, you can review them. For example, here is the documentation and remarks for the function call that enables the program to enter the critical section, EnterCriticalSection function | Microsoft Docs
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
|
I use this class to print:
Printing Class Library[^]
I can print bitmaps if there are in a separate file. How can I print a bitmap which I added to my
resource. That is, the bitmap is IDB_SAMPLE and I want to use this code
pPage->PrintBitMap(top, left, top + 0.89, left + 0.29, "test.bmp");
where "test.bmp" is LPCSTR variable. How can I convert IDB_SAMPLE which is a bitmap I added to
my resource to a LPCSTR variable so that I can print using this function PrintBitmap().
Please any response any one can give me will be greatly appreciated.
|
|
|
|
|
wrote: I use this class to print:
Printing Class Library[^]
Then why don't you want to ask the author of this article?
|
|
|
|
|
Because he passed away. Im hoping someone can help.
|
|
|
|
|
wrote: Because he passed away.
But did you try to ask him?
Anyway, you should investigate/debug his code to understand how the method CPage::PrintBitMap works.
Then you perhaps will be able to modify or add some code to use the bitmap resource ID rather than the bitmap pathname.
|
|
|
|
|
Victor Nijegorodov wrote: Because he passed away.
But did you try to ask him?
Well, I guess tonight would be the right night to try. Ouija boards at the ready!
NB: "He passed away" means "er ist tot".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: NB: "He passed away" means "er ist tot".
I am sorry...
Please forgive my very poor English...
|
|
|
|
|
You may also use some other code to print out the bitmap.
For instance, get this one: Print a bitmap full page and just change the parameters of LoadImage to load form resources rather than the file.
|
|
|
|
|
The PrintBitMap code is likely just calling the standard windows API call LoadImage.
LoadImageA function | Microsoft Docs[^]
If you read the documentation the fix will be obvious, you need to not set the LR_LOADFROMFILE flag when passing in a resource ID in the string.
Down the bottom in the remarks you will find reference to a neat macro IS_INTRESOURCE(lpszName).
IS_INTRESOURCE macro | Microsoft Docs[^]
So Microsoft gives you a macro to work out if a string is actually an INTRESOURCE.
So basically a one line fix in the code find the LR_LOADFROMFILE and set it only if IS_INTRESOURCE returns false.
It is the always setting of that flag that is fouling the LoadImage working properly because you want the extended behaviour that is spelled out in this statement
Quote: The image to be loaded. If the hinst parameter is non-NULL and the fuLoad parameter omits LR_LOADFROMFILE, lpszName specifies the image resource in the hinst module. If the image resource is to be loaded by name from the module, the lpszName parameter is a pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by ordinal from the module, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the LoadImage function.
As an extended answer you may want to consider adding a few lines of code to test the string extension for known types JPG etc not just resource ID and use IPicture to add that support. As an example this will take a filename string and convert a jpg to a bitmap handle. You would use this function in place of LoadImage in the situation you had a jpeg filename and you could then print jpegs.
The disadvantages of using classes and frameworks is you never learn the Windows API and how it is designed to work. Anyhow the small code block follows, I gave you the option of returning the wth, ht of the jpeg loaded but you can use NULL if you don't want them returned.
#include <olectl.h>
HBITMAP HBMPFromJPGFile (char* filename, int* returnWth, int* returnHt){
const int HIMETRIC_PER_INCH = 2540;
int Wth, Ht;
HRESULT hr;
WCHAR OlePathName[512];
HBITMAP bmp;
HDC MemDC;
SIZE sizeInHiMetric;
IPicture *Ipic = NULL;
MultiByteToWideChar(CP_ACP, 0, filename,
(int)strlen(filename)+1, &OlePathName[0], 512);
hr = OleLoadPicturePath(OlePathName, NULL, 0, 0,
IID_IPicture, (LPVOID*) &Ipic);
if ((hr == S_OK) & (Ipic != 0)){
HDC Dc = GetDC(NULL);
int nPixelsPerInchX = GetDeviceCaps(Dc, LOGPIXELSX);
int nPixelsPerInchY = GetDeviceCaps(Dc, LOGPIXELSY);
ReleaseDC(NULL, Dc);
Ipic->get_Width(&sizeInHiMetric.cx);
Ipic->get_Height(&sizeInHiMetric.cy);
Wth = (nPixelsPerInchX * sizeInHiMetric.cx +
HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;
Ht = (nPixelsPerInchY * sizeInHiMetric.cy +
HIMETRIC_PER_INCH / 2) / HIMETRIC_PER_INCH;
MemDC = CreateCompatibleDC(0);
int P2Width = Wth >> 2;
if ((P2Width << 2) < Wth) P2Width++;
P2Width = P2Width << 2;
BITMAPINFO bi = {0};
DWORD *pBits = 0;
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biWidth = P2Width;
bi.bmiHeader.biHeight = Ht;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biPlanes = 1;
bmp = CreateDIBSection(MemDC, &bi, DIB_RGB_COLORS,
(void**)&pBits, 0, 0);
Ipic->Release();
if (returnWth) (*returnWth) = Wth;
if (returnHt) (*returnHt) = Ht;
return bmp;
}
return 0;
}
In vino veritas
modified 1-Nov-18 0:17am.
|
|
|
|
|
Hi
When have the following code
class a
{
int a;
LPCTSTR b;
c d;
}
where "c" is defined
class c
{
…
}
so if class a is in a.h and class c is in c.h
what is the difference between #include "c.h' in a.h or declaring class c; or as a better question what is the way to resolve the unresolved reference in a.h for the type "c" which is a class
is it to "include "c.h" or declare class c; that c is of type class and I guess in that case "c" will get resolved by the linker
Hope this question makes sense
thanks
|
|
|
|
|
Did you try to add the forward declaration
class c; in the a.h? Does it compile? If Yes - then use it.
Otherwise put
#include "c.h" in a.h
|
|
|
|
|
the forward declaration worked #include didn't was just wondering about the difference I guess the linker will resolve class a ? right thanks
|
|
|
|
|
No, the linker connects object code, it has no knowledge of classes. Classes are purely a mechanism used in source languages and converted into object code by the compiler.
|
|
|
|
|
In the code you posted, the forward declaration can not work. If it worked in your code, it may be including c.h without your knowing, maybe indirectly through another header.
If #include doesn't work, read the error message. The only reasons for #include not working that I can think of is that either the code in your c.h has a syntax error, or you forgot to add an Include guard[^], causing a duplicate definition.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
In this case, a.h needs to include c.h since the former needs to know the full definition of c.
|
|
|
|
|
Well, Well, Well!
This question is the result of too much abstraction in education of computer scientists!
A bit of learning of how a compiler works, Things like Bits and Bytes, the underlying mechanisms, and what it cannot do would help you here. This is an insolvable problem! Ultimately, a compiler lays out code, reserves blocks of memory of a certain size, etc.
The compiler needs to know how much memory is needed for each user defined type (in C(++)) that is a union, structure or a class!
When you Declare a class, you tell the compiler to take note of the name of the class! The Linker may than be able to find that class properly defined in another file, and hence in another .obj file! The compiler cannot at that stage know anything about the size of the object, so, all you can use is either a pointer of an object of that type, or, a reference to it! A Reference to an object is very similar to a pointer, it is a compiler guaranteed pointer, that cannot be null, and always points at an object of the type!
In Your code class 'c' needs to be defined in the file before class a in which it is used! Otherwise your compiler cannot calculate the size of class a!
Best of luck learning more,
There may be rules around this conundrum in synthetic languages such as C#,Java etc. I would know nothing of these! There are no such shortcuts in C or CPP!
I encourage anyone to take up C or CPP! It are the base languages on which all others are built! A knowledge of Machine Code, and how it translates in ASM, would also help you
Bram van Kampen
|
|
|
|
|