|
I have 3 Questions.
1. Why a size of an empty class is 1 byte?
2. if this code executes, again the size of class is 1 byte (padding not occuring even padding size is 4)
class MyClass
{
char i;
};
MyClass m;
printf("%d",sizeof(m));
3. for this code it will print 8 bytes as size (default padding 4 bytes)
class MyClass
{
int i;
char i;
};
MyClass m;
printf("%d",sizeof(m));
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
Sarath. wrote: 1. Why a size of an empty class is 1 byte?
Because the standard requires it to be so. Basically, it is so you avoid problems where you try to allocate 0 bytes of memory (ever try to do int* pi = new int[0] ?). If an empty class is always at least 1 byte, then you can't have that problem.
Sarath. wrote: 2. if this code executes, again the size of class is 1 byte (padding not occuring even padding size is 4)
The padding for characters is 1 byte. If that is all that is in your class, then it won't bother padding it since it is almost guaranteed to be placed on a byte boundary.
Sarath. wrote: 3. for this code it will print 8 bytes as size (default padding 4 bytes)
This actually occurs because the compiler packs extra characters for you (if you use pragma pack, you avoid this). Since you have more than just your character now, it places enough memory for your class to end on a double word boundary. This makes it more efficient if you declare an array of these objects since the integer values will be guaranteed to be on the proper boundaries in memory.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks alot for your reply. it was quite informative.
one more question which has been raised when I read your answer
int* p1 = new int[0];
*p1 = 10;
printf("%d",*p);
delete p1;
In the above code, printing of the above code will work fine. but delete fails.
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
Sarath. wrote: int* p1 = new int[0];
That line will actually cause a memory corruption. It has been mentioned that the compilers should treat this as an error when the new standard is released, but if you try it on certain compilers right now, it won't give you an error. If you do it with Microsoft's compiler, you usually get a weird looking message box that pops up and tells you that the memory has been corrupted.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Anyway it's returning a valid pointer. my question was, will this create a memory leak? (if we don't call delete.anyway if we call delete it will fail)
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
Sarath. wrote: Anyway it's returning a valid pointer
No, its not valid pointer.
operator new allocates additional memory on heap , when we use it. It contains header info and bytes to guard off memory, called 'No Mans land'.
So actual size will be,
actual_size=sizeof(headerinfo)+nSizeRequestedbyUs+NOMansLandSize
In above case though nSizeRequestedbyUs is 0, it returns valid address, but not supposed to access by us.
Sarath. wrote: will this create a memory leak? (if we don't call delete.anyway if we call delete it will fail)
Yes.
By accessing it we are corrupting it. So giving error while deletion.
If you try to delete it without accessing it , ie. without assigning any value to it, it will not give you error.
-- modified at 2:57 Thursday 3rd August, 2006
|
|
|
|
|
Sarath. wrote: Anyway it's returning a valid pointer.
On the VC6 compiler, the line with new int[0] would cause an immediate crash in both release and debug builds. I'm not sure if the newer compilers still do that, but the line is invalid code. If the pointer that comes back looks valid, I assure you, it isn't. You are guaranteed to corrupt your memory by writing to the location it gives you.
Sarath. wrote: my question was, will this create a memory leak?
You won't get a memory leak, you get memory corruption. Basically, the line itself causes a problem, and deleting it is the least of your worries (calling delete won't fix the memory problem you created by trying to declare a 0-sized array).
In general, think logically when programming. It makes no sense to have an array with no capacity; therefore, it is not valid to declare.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Sarath. wrote: 1. Why a size of an empty class is 1 byte?
See here.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I have a dialog based app and would like to have another dialog be anchored to either the bottom or the left side of my main dialog. Can someone please point me to some code for this, or help me? I figured I should be using SetWindowPos(), but how do I know where to place it?
Thanks in advance.
|
|
|
|
|
- Handle the
WM_MOVE notification in your main dialog.
- Compute your main window's bottom-L corner.
- Call
MoveWindow() or SetWindowPos() on the secondary dialog. /ravi
|
|
|
|
|
IN addition to Ravi's reply, you may have to use modeless dialog
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
yes, I'm sorry. The second dialog I am trying to use is a modeless dialog that is show when a menu item is selected.
can someone help me calculate the size of my main dialog?
|
|
|
|
|
GetClientRect and GetWindowRect are at your disposal
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|
|
NYTSX wrote: calculate the size of my main dialog?
GetWindowRect()
/ravi
|
|
|
|
|
I have a many C++ windows projects, dlls and librarys. I have done some research to find out what happens if there are similar string IDs in projects, dlls and libraries after they are are linked together.
What happens is the string in the project always replaces the string in the dll or library if there is a string ID match.
Example: I declare string ID 100 as "Chris" in the project, "John" in the dll, and "Bob" in the library. I use CString::LoadString(100) in the dll and CString::LoadString(100) in the library. When these two statements are executed the string "Chris" is loaded from the project string table and not the library and dll.
I was able to solve the similar string issue between projects and dlls useing ::FindResource along with the dlls instance. Has anyone figured out how to have similar strings between projects and libraries?
Chris
|
|
|
|
|
|
So how do you get the instance of a library?
Chris
|
|
|
|
|
See this[^] article.
/ravi
|
|
|
|
|
I read the article and there is no mention of a library project. This article deals with string conflicts in DLL projects. My issue is having similar strings with a library project where there is a DLL is not created.
Chris
|
|
|
|
|
You are creating static link libraries with resources in them?
The problem you are having deals with search order. It is very similar to dealing with a PATH variable. The LoadString method looks in its module for a string id. If it finds it, it uses it. Otherwise, it moves on to the static libraries that were compiled into your project (which really shouldn't have resources of their own, but I digress). Finally, it starts marching through the DLLs you have loaded.
This is why you should make sure that your string IDs are unique.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
So there is no way to have the same string id in a project and in a library and have the library string "Win".
Chris
|
|
|
|
|
The reason I am asking this question in the first place is because I have three different languages in all the projects, libraries and dlls. That is why I have string resources in libraries.
Chris
|
|
|
|
|
The better way to handle that case is to make each language its own resource dll and to allow the user to select which language they want to use (or detect what Windows is using when the application starts up). Then you would just load that DLL. There is an example of how to do that in an article on this site (I believe it is in the DLL section).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
It almost sounds like you need AFX_MANAGE_STATE(AfxGetStaticModuleState()) at the top of the routine that calls LoadString() . This is just a guess, however.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
This looks very interesting. I have never used AFX_MANAGE_STATE(AfxGetStaticModuleState()) before. I am not useing LoadLibrary() anywhere in the projects. I just put the library location in the linker of Microsoft Visual Studios 6.0. Also I wonder if AFX_MANAGE_STATE would even work because the description of this macro is "Call this macro to protect an exported function in a DLL." I am not sure if this would work with a library or not.
Chris
|
|
|
|