|
The code below uses a variable's type as size in malloc(). And I printf the size of //line 1, b and c to find out their sizes malloc will allocation the space for them in heap.
Question part 1:
One person told me I should use // line a's size to malloc in // #####, while the other person said I should use //line c instead. They do have the same size in printf(). Which one is correct? Why?
Question part 2:
// line b and // line c both have the same type of variable pointers, but why do they have different sizes?
struct Vector {
double *data;
size_t size;
};
int main()
{ int a, b, d;
int sz = 12;
a = sizeof(struct Vector); // line a
printf("%d\n", a); //a = 16
b = sizeof(struct Vector*); // line b
printf("%d\n", b); //b = 8
c = sizeof(*retVal); // line c
printf("%d\n\n", c); //c = 16
struct Vector *retVal = malloc (sizeof (struct Vector)); //#####
retVal->data = malloc (sz * sizeof (double));
// Set size and return.
retVal->size = sz;
printf("retVal->size: %d\n", sz);
return 0;
}
Thank you for your help.
|
|
|
|
|
Both are correct. sizeof(struct Vector) expands to the size of the type while sizeof(*retVal) expands to the size of the type pointed to by the variable.
The second method should be preferred because it will be always the correct size even when changing the variable type. Imagine that you have another structure with different size. When you now change the type of retVal to that structure, you must also change the type for the first sizeof() but not for the second.
|
|
|
|
|
Thanks for your response. Just to make sure I understand your answer, I listed .....I'm thinking the second malloc() used in my previous example can be replaced by the line below?
sizeof(12 * sizeof(retVal->data));
Thank you!
|
|
|
|
|
No, that is wrong. The second example from your initial post (sizeof(struct Vector*) ) returns the size of a pointer which is 4 or 8 with 32-bit or 64-bit builds.
The sizeof[^] operator expands to the size of the argument during compilation. If the argument is not a type but a variable, the type of the variable is returned. If the variable is a pointer, the pointer must be dereferenced using the * operator to get the size of the type instead the size of the pointer. Similar applies when the type is a pointer itself.
But when passing a type followed by * like in your second example, it is a pointer to the type. In that case the type does not care because all pointers have the same size.
Maybe you are irritated how the * operator is treated. With declarations it indicates a pointer. When accessing variables, it accesses the content of pointer variables.
|
|
|
|
|
Thank you for clarifying my confusion, Jochen!! I appreciate it.
|
|
|
|
|
Jochen provided a more complete answer.
modified 21-Nov-15 6:54am.
|
|
|
|
|
I have a single doc multiple views app in a tabbed view type mainframe. I have printing working nicely for each view on its own, but I need to print all views in one 'report'. I started by adding a "Print All" command in CMainFrame , and then traversing all document views (as CView pointers), but it gets very messy for a few reasons:
- each view must be cast to its RUNTIME_CLASS and checked to find which it is, so that its OnPrint function can be called
- each view has different scaling, so a single CPrintInfo won't suffice
- its not known in advance how many pages to set, but this can be overcome with m_bContinue checks.
Any suggestions on how to print a single document multiple views please?
Thanks!
|
|
|
|
|
.
What do you get when you cross a joke with a rhetorical question?
The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism.
Do questions with multiple question marks annoy you???
modified 20-Nov-15 8:46am.
|
|
|
|
|
|
Brisingr Aerowing wrote: WHY ON EARTH IS IT MISSING IN THE FIRST PLACE?!? That is the proper question.
Could you please report the exact error message (and any accessory, useful info)?
|
|
|
|
|
Brisingr Aerowing wrote: the ~ operator (bitwise not) not being implemented That would break a lot of software, I suspect something else is the actual issue. A sample of the code and the error details would help.
[edit]
Are you sure this is MSVC 14? no such version referenced on MSDN[^] as far as I can see.
[/edit]
|
|
|
|
|
Richard MacCutchan wrote: Are you sure this is MSVC 14? That is the compiler version: Microsoft Visual C/C++ Compiler version 14. That version is part of Visual Studio 2015.
|
|
|
|
|
MSVC is the IDE not the compiler.
|
|
|
|
|
You are right, it is not only the compiler but the complete C/C++ development environment.
But version 14 is those from VS 2015. See for example the Wikipedia[^].
|
|
|
|
|
I have VS 2013 express and the C compiler is:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC>cl -v
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
|
|
|
The 18 is the RTM number from my above link.
What a mess with all these different numbers for a single product
|
|
|
|
|
OP deleted his question; I can guess why.
|
|
|
|
|
void assign(size_type n, el const T& el = T())
This function is a member function of the STL list class and I do not understand the bolded part. I understand the const T& el is a reference to a constant parameter which means I cannot change the value/content of the passed argument + I understand that el = T() means a default parameter so if there is no argument passed in the calling function the el will use the no-argument constructor of the T() type OK. But what make me confused the el at the beginning before the const?!!! so if someone can fill the blank gaps for me I will be grateful . Thanks in advance.
|
|
|
|
|
|
I read it in a book called "Data Structures and Algorithms in C++, 4th Edition by Adam Drozdek" it was in chapter 3 "Linked Lists" at the beginning of page 110 . It made me had the feeling of this when read it, and after searching and find nothing I become like this ;P;P (i.e., psycho). Thanks by the way for reply
|
|
|
|
|
Looks like a typo, to me.
|
|
|
|
|
I have wrote a method that load and rescale a CBitmap:
CBitmap* CMyDlg::LoadPicture(CString strFile)
{
int m_nPictureSize = 170;
CBitmap* pBitmap = NULL;
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, strFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(NULL != hBitmap)
{
CBitmap Bitmap, MemBitmap;
Bitmap.Attach((HBITMAP)hBitmap);
CBitmap* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldBmp);
pBitmap = new CBitmap;
pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
BITMAP bm3;
pBitmap->GetBitmap(&bm3);
TRACE("Width and height INSIDE if condition: %d|%d\n", bm3.bmWidth, bm3.bmHeight);
}
BITMAP bm4;
pBitmap->GetBitmap(&bm4);
TRACE("Width and height OUTSIDE if condition: %d|%d\n", bm4.bmWidth, bm4.bmHeight);
return pBitmap;
}
I want to use this method in order to add this created CBitmap pointer to add them into CTypedCBitmapList:
CBitmap* pImage = LoadPicture(strPath);
if(NULL == pImage->GetSafeHandle())
return 0;
m_PtrArrayBitmap.Add(pImage);
Ok, but the strange thing is that pBitmap from inside of LoadPicture method has correct size inside of 'if' condition, and wrong size outside of 'if' condition:
Width and height INSIDE if condition: 170|170
Width and height OUTSIDE if condition: -858993460|-858993460
Why ? It is not the same pBitmap object ? Could you help me ? Kindly thank for any further help !
|
|
|
|
|
It appears that the bitmap you are pointing to is a temporary object whose scope is only within the if block. In the following block of code where do pOldBitmap and pOldBmp come from?
CBitmap* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldBmp);
pBitmap = new CBitmap;
pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
|
|
|
|
|
I think I figure out why it doesn't work ... pOldBitmap and pOldBmp are decalred inside of 'if' statement:
if(NULL != hBitmap)
{
CBitmap* pOldBitmap = MemDC.SelectObject(&MemBitmap);
CDC SourceDC;
SourceDC.CreateCompatibleDC(&cdc);
CBitmap* pOldBmp = SourceDC.SelectObject(&Bitmap);
CBitmap* pFinalBitmap = MemDC.SelectObject(pOldBitmap);
SourceDC.SelectObject(pOldBmp);
pBitmap = new CBitmap;
pBitmap->Attach((HBITMAP)pFinalBitmap->GetSafeHandle());
BITMAP bm3;
pBitmap->GetBitmap(&bm3);
TRACE("Width and height INSIDE if condition: %d|%d\n", bm3.bmWidth, bm3.bmHeight);
}
BITMAP bm4;
pBitmap->GetBitmap(&bm4);
TRACE("Width and height OUTSIDE if condition: %d|%d\n", bm4.bmWidth, bm4.bmHeight);
return pBitmap;
|
|
|
|
|
Well, that is quite different from the code in your original question.
|
|
|
|