|
Why does the following print out 2 twice (instead of 1, 2)?
#include <stdio.h>
template < int i >
void foo()
{
printf("%i\n", i);
}
int main(int argc, char* argv[])
{
foo<1>();
foo<2>();
return 0;
}
- thanks
|
|
|
|
|
This is a well known bug in VC++ 6.0: the compiler is blind to template parameters that do not somehow appear as actual arguments of a template function. Hence, foo<1> and foo<2> are treated as the same instantiation.
The following workaround can be applied:
template<int i>
struct int_type
{
};
template<int i>
void foo(int_type<i>* dummy=0)
{
printf("%i\n", i);
}
int main(int argc, char* argv[])
{
foo<1>();
foo<2>();
return 0;
} the dummy argument pulls the i template parameter into the list of arguments of foo , so circumventing the compiler bug. Moreover, as dummy has a default value, you don't need to explicitly mention it when calling foo . Sneaky, isn't it?
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Joaquín M López Muñoz wrote:
This is a well known bug in VC++ 6.0
grrr... ok, thanks!
|
|
|
|
|
I should know how to do this but I keep on coming up with compile errors. I want to create a dynamic-length array of pointers to a custom class I made. I tried using the following malloc commands:
CClass * list[] = (CClass *) malloc(sizeof(CClass) * AmountOfPointers);
The compile error tells me I can't equate a pointer to an array of pointers. Should I be using reinterpret_cast to do this? I tried changing the cast from (CClass *) malloc... to (CClass *[]) malloc... but I got another error, cannot recast a pointer to void to an array of CClass pointers. I tried looking for help on the site but I couldn't find into on dynamically-sized arrays of pointers. Elsewhere in my program I have an array of pointers to something else but that array length is static.
|
|
|
|
|
You shouldn't use malloc to allocate an array of objects, because malloc does not call the constructors for each object. Here's how I would do this:
CClass *list[] = new CClass *[AmountOfPointers];
for (int i = 0; i < AmountOfPointers; i++) {
list[i] = new CClass;
} Note that, when you are done with the list and want to deallocate it, the code will look something like this:
for (int i = 0; i < AmountOfPointers; i++) {
delete list[i];
}
delete []list;
Software Zen: delete this;
|
|
|
|
|
I got an error with the first line of your code, it couldn't cast CClass ** to CClass *[], so I just changed the line to:
CClass **list = new CClass *[AmountOfPointers];
and left the rest alone and I think it works fine, thanks.
|
|
|
|
|
I used your method (although a malloc line did the same thing) to create the array of pointers. After this code is run, I run the same class member function in each instance of the class. I get no compile errors, but I have a piece of code within a member of the class I am creating the array of pointers to. My program crashes when it runs my code after your statements. However, if I eliminate the loop and create each new class manually, the code doesn't crash. In both instances, the trace runs to the first class and finds the member function but an array in the member function crashes when using the first piece of code. The function executes fine with the second set of statements.
This doesn't work:
CClass **list = new CClass *[AmountOfPointers];<br />
for (idnum = 0; idnum < AmountOfPointers; idnum++) { list[idnum] = new CClass(idnum); }
However, this code does work:
CClass **list = new CClass *[AmountOfPointers];<br />
list[0] = new CClass(0);<br />
list[1] = new CClass(1);<br />
list[2] = new CClass(2);<br />
... etc ...
I don't get why it's crashing because of a difference between a loop and a set of manual statements.
|
|
|
|
|
The code below is a similar method, but a little more efficient, since it only calls new twice, and it only calls delete twice.
It calls new once to create the array of pointers, and then it calls new a second time to create a single block array of the target type.
The the for loop assigns different sections of the block of array to each pointer in the pointer array.
<br />
template < typename T ><br />
T **Allocate2DArray( int nRows, int nCols)<br />
{<br />
T **ppi;<br />
T *pool;<br />
T *curPtr;<br />
<br />
ppi = new T*[nRows];<br />
<br />
pool = new T [nRows * nCols];<br />
<br />
curPtr = pool;<br />
for( int i = 0; i < nRows; i++)<br />
{<br />
*(ppi + i) = curPtr;<br />
curPtr += nCols;<br />
}<br />
return ppi;<br />
}<br />
<br />
template < typename T ><br />
void Free2DArray(T** Array)<br />
{<br />
delete [] *Array;<br />
delete [] Array;<br />
}<br />
<br />
int main()<br />
{<br />
double **d = Allocate2DArray<double>(10000, 10000);<br />
d[0][0] = 10.0;<br />
d[1][1] = 20.0;<br />
d[9999][9999] = 2345.09;<br />
Free2DArray(d);<br />
}<br />
Top ten member of C++ Expert Exchange.
http://www.experts-exchange.com/Cplusplus
|
|
|
|
|
I found a page on here and interpreted the help into my program and came up with this. Tell me if this is okay...
CClass ** list = (CClass **) malloc(sizeof(CClass) * AmountOfPointers);
|
|
|
|
|
That still doesn't work. A single malloc allocates a single block of memory. The fact that you are casting the pointers to the right type doesn't change that fact.
What you are asking for is this:
list[]:
+-----+ +-----------+
| [0] |-->| CClass[0] |
+-----+ +-----------+
| [1] |-->| CClass[1] |
+-----+ +-----------+
| [2] |-->| CClass[2] |
+-----+ +-----------+
| [.] |-->| CClass[.] |
+-----+ +-----------+ The code you are using is creating this:
+-----------+
-->| CClass[0] |
+-----------+
| CClass[1] |
+-----------+
| CClass[2] |
+-----------+
| CClass[.] |
+-----------+ Now, if all you really need is an array of CClass objects, the following will work:
CClass *list = (CClass *)malloc(sizeof(CClass) * AmountOfObjects); although, as I mentioned in my earlier message, this isn't a good idea since it doesn't call the CClass constructors.
Software Zen: delete this;
|
|
|
|
|
Two problems:
An array of CClass* is typed as CClass** (note two asterisks).
You can't use the unsized array notation, you can only use that if you have an array initializer.
The correct code is:
CClass** lst = (CClass**) malloc ( sizeof(CClass*) * AmountOfPointers );
--Mike--
LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ
Strange things are afoot at the U+004B U+20DD
|
|
|
|
|
Hi all,
I got a AES impelmentation code in VC++ and have some doubts, which I cannot sort out.
Will you plssssssssss let me know if u are familiar with both the AES algorithm and VC++.
So that I can forward my doubts along with the programme.
Thx...
Vendy
|
|
|
|
|
|
hi,,
oh ok.
There is an implementation of the AES algorithm in the below link. If u download it..
http://www.thecodeproject.com/cpp/aes.asp
in Table.h
(1.) Why have they used s boxes in ‘int’s? why not
const int CRijndael::SBox[256] rather than ---const char CRijndael::SBox[256] and replace the negative values with positive?
(2.) What does
const int CRijndael::sm_T1[256] …. const int CRijndael::sm_T8[256] tables correspond to?
(3.) and const int CRijndael::sm_U1[256]…const int CRijndael::sm_U4[256] tables correspond to
(4.) also const int CRijndael::sm_shifts[3][4][2] tables correspond to?
tnx
|
|
|
|
|
Friends,
I developed an application in VC++ using MFC. I like to launch it at startup. The two methods i know for this purpose are:
1) Placing shortcut in startup folder.
2) Registry enrry: HKCU\Software\Microsoft\Windows\CurrentVersion\Run
Are there other methods you know for this purpose ??
Imtiaz
|
|
|
|
|
Place a shortcut to the application in the user's Startup group. Under Windows NT/2000/XP, there are two Startup groups, one for the individual user and one for 'All Users'.
Software Zen: delete this;
|
|
|
|
|
Imtiaz Murtaza wrote:
Are there other methods you know for this purpose ??
See the MSDN article Q179365.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi,
I've got a .net dll registered for COM, which fires events. Is there any possible way to handle events fired from that dll?
Thanks in advance for any help or clue.
i.chen
|
|
|
|
|
Hi.
Does anybody know how to make a executable VC++ file
with a built in dll hook?
cause i get tired of using a loader, so i wanna try this.
greetz
DP
|
|
|
|
|
|
thnx for your reply.
i want to hook my DLL into another application.
the dll itselfs works, so i use an injector.
the problem is i want to do this in one exe which "contains" my dll, so i don't need the loader.
greetz
DP
|
|
|
|
|
I would like to make a DLL with template functions and classes. How this can be done?
Thank you
DO!
|
|
|
|
|
templates can be defined only in h file, so they can not be in dll.
this is c++ syntax, no other choices.
includeh10
|
|
|
|
|
Templates are used by the compiler to "copy and paste" the code for datatypes that are used with a particular function. By the time it becomes a dll, there is no more need for the template.
|
|
|
|
|
Hi!
I want to start/stop windows service from my C++ program. How do I do that? I searched google but found nothing
Thanks!
Regards,
Iliya Yordanov
www.csharp-home.com - C# Resources
|
|
|
|