|
Well
I wanna know how i within a function can know the number of arguments that have been passed to it.
void fun(int x,float y)<br />
{<br />
int z;<br />
<br />
printf("Arguments passed = %d",z);<br />
}
I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?
Spread wat u Know!
|
|
|
|
|
Why this? Inside the function, you know [and you should know] that this function takes this or that number of arguments (in this case 2) arguments.
--
=====
Arman
|
|
|
|
|
Arman Z. Sahakyan wrote: Why this?
I wanna know how actually functions knows this.
Spread wat u Know!
|
|
|
|
|
Hi
In fact you should not define the function that way; instead you should use paramArray like in the following example:
<code>
using namespace System;
double average( ... array<Int32>^ arr ) {
int i = arr->GetLength(0);
double answer = 0.0;
for (int j = 0 ; j < i ; j++)
answer += arr[j];
return answer / i;
}
int main() {
Console::WriteLine("{0}", average( 1, 2, 3, 6 ));
}</code>
|
|
|
|
|
As far as I understand, his question is how the compiler learns the number of arguments. In other words, where they are stored and how thay are retrieved...
--
=====
Arman
|
|
|
|
|
For the first time i thought u people r goin 2 touch calling conventions but..........................
Can anybody give solution to the actual problem(code) i posted.
Hope i will get some help...
Spread wat u Know!
|
|
|
|
|
So Cmania, would you please explain your question more clearly?
Are you looking to see how the compiler knows the number of arguments or you want
to get the number of arguments that are passed to the function?
In the second case, I don't get what do you mean, because if user passes any number of
arguments rather than 2 in this case, the compiler would generate an error!!
|
|
|
|
|
I suspect that you can't. You can probably determine how many bytes are allocated on the stack using some (possibly compiler dependent) algorithm, but how many variables this corresponds to cannot be determined (although you may be able to deduce it from the code - e.g. how the variables are accessed). The size of the stack frame for
<br />
void function1(double x)<br />
and
<br />
void function2(float x, float y)<br />
would be the same.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Without using asm i would suggest adapting some stack tracing code to get the undecorated function name as a string and then parse the name. However, i'm pretty sure this will only work for C++ functions.
...cmk
Save the whales - collect the whole set
|
|
|
|
|
You can look at the stack frame all you want, it won't help.
If there's 16 bytes, how do you know if there's 4 ints, 16 chars, etc.??
The compiler knows because it reads the source code. Unless the compiler supplies a method to
determine the number of arguments, you're not going to be able to do it at runtime.
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hi all,
I have generating a class SRecordset,....
/
/ SRecordset.h<br />
<br />
#pragma once<br />
#include "afx.h"<br />
#include <afxdb.h><br />
class SRecordset : public CObject<br />
{<br />
DECLARE_SERIAL(SRecordset)<br />
public:<br />
SRecordset(void);<br />
<br />
public:<br />
~SRecordset(void);<br />
<br />
public:<br />
int iNumFldCount;<br />
int iNumRec;<br />
int iTotalSize;<br />
int iRecCount;<br />
public:<br />
void SSDbConnector();<br />
void SSDataExtractor(CString sQuery);<br />
<br />
public:<br />
CDatabase dbFASP;<br />
CStringArray saDataContainer;<br />
<br />
CString sunami;
<br />
public:<br />
void SSGetFieldValue(int iIndex,CString &FldValue);<br />
<br />
};<br />
<br />
<br />
<br />
#include "StdAfx.h"<br />
#include "FASPRecordset.h"<br />
<br />
IMPLEMENT_SERIAL(SRecordset,CObject,VERSIONABLE_SCHEMA | 2)<br />
<br />
<br />
SRecordset::SRecordset(void)<br />
{<br />
sunami = "Sunami";<br />
<br />
}<br />
<br />
<br />
void SRecordset::SSDbConnector()<br />
{<br />
CString sIp,sDb,sPwd,sConn,sPort,sUn;<br />
sIp="192.168.2.17";<br />
sPort="5432";<br />
sPwd="fasp";<br />
<br />
try<br />
{ <br />
sConn = "DRIVER={PostgreSQL ANSI};SERVER=" + sIp + ";port=" + sPort + ";DATABASE=" + sPwd + ";UID=" + sPwd + ";PWD=" + sPwd + ";";<br />
dbFASP.OpenEx(sConn);<br />
AfxMessageBox("Success");<br />
}<br />
catch(CDBException *e)<br />
{<br />
MessageBox(0,"Database Connection Refused."+e->m_strError,"DataBase Connection Error",MB_OK|MB_ICONERROR);<br />
e->Delete();<br />
<br />
}<br />
<br />
}<br />
<br />
<br />
void SRecordset::SSDataExtractor(CString sQuery)<br />
{<br />
CRecordset rsFASP(&dbFASP);<br />
CString sValue;<br />
<br />
rsFASP.Open(AFX_DB_USE_DEFAULT_TYPE,sQuery);<br />
<br />
iNumFldCount = rsFASP.GetODBCFieldCount();<br />
<br />
iNumRec = 0;<br />
<br />
<br />
while(!rsFASP.IsEOF())<br />
{<br />
rsFASP.MoveNext();<br />
iNumRec++;<br />
}<br />
<br />
iTotalSize = iNumFldCount * iNumRec;<br />
<br />
sunami.Format("%d",iTotalSize);<br />
AfxMessageBox(sunami);<br />
<br />
saDataContainer.SetSize(iTotalSize);<br />
<br />
rsFASP.MoveFirst();<br />
<br />
iRecCount = 0;<br />
<br />
while(!rsFASP.IsEOF())<br />
{<br />
for(int ic=0; ic < iNumFldCount; ic++)<br />
{<br />
sValue = "";<br />
rsFASP.GetFieldValue(ic,sValue);<br />
saDataContainer[iRecCount] = sValue;<br />
<br />
iRecCount++;<br />
<br />
<br />
}<br />
<br />
rsFASP.MoveNext();<br />
sunami = saDataContainer[1];<br />
<br />
}<br />
rsFASP.Close();<br />
dbFASP.Close();<br />
<br />
}<br />
<br />
void SRecordset::SSGetFieldValue(int iIndex, CString &FldValue)<br />
{<br />
FldValue = "";<br />
FldValue = saDataContainer[iIndex];<br />
<br />
}
i will create a object of above the class in my dialog box file and call database connection function, and call dataextract function which will provides set of data set with in CStringArray variable......
void CSerializeDbDataDlg::OnBnClickedConnect()<br />
{<br />
SRecordset *set = new SRecordset;<br />
set->SSDbConnector();<br />
set->SSDataExtractor("select name from master;");<br />
<br />
CFile file;<br />
file.Open("dragon.bin",CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);<br />
CArchive ar(&file,CArchive::store);<br />
<br />
if(ar.IsStoring())<br />
ar << set;
ar.Flush();<br />
ar.Close();<br />
file.Close();<br />
delete set;<br />
}
the file "dragon.bin" create successfully but i think it not contain all relevant data of that object
and i can't read no more data for that object. except the SRecordset Cstring variable "sunami" because i have defined it on constructor of SRecordset
void CSerializeDbDataDlg::OnBnClickedRead()<br />
{<br />
<br />
SRecordset *set1 = new SRecordset;<br />
<br />
CFile filee;<br />
filee.Open("dragon.bin", CFile::modeRead | CFile::typeBinary);<br />
CArchive arr(&filee,CArchive::load);<br />
<br />
if(arr.IsLoading())<br />
arr >> set1; <br />
<br />
AfxMessageBox(set1->sunami);<br />
<br />
for(int i=0;i<set1->iTotalSize;i++)<br />
AfxMessageBox(set2->saDataContainer[i]);<br />
<br />
arr.Flush();<br />
arr.Close();<br />
filee.Close();<br />
delete set1;<br />
}
the above code shows only a "Sunami" messagebox, it wouldn't show the CStringArray variable values because even iTotalSize is also going to be wrong............
Please don't get wrong with me, i don't know any other ways please help me...what can i do
thanks in advance
|
|
|
|
|
..and where is your overriden version of CObject::Serialize? Inside it, you should serialize (ar << XXX) and deserialize (ar >> XXX) all the data you think appropriate. Then inside the button handler, you might write like so;
// into the file
CFile file;
file.Open("dragon.bin", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
CArchive ar(&file,CArchive::store);
set->Serialize(ar);
ar.Close();
file.Close();
--
=====
Arman
|
|
|
|
|
thank you i will try.......
|
|
|
|
|
Its working well except CStringArray that is...........
1 void SRecordset::Serialize(CArchive& ar)
2 {
3 if (ar.IsStoring())
4 { // storing code
5 ar << iNumFldCount;
6 ar << iNumRec;
7 ar << iTotalSize;
8 ar << saDataContainer;
9 }
10 else
11 { // loading code
12 ar >> iNumFldCount;
13 ar >> iNumRec;
14 ar >> iTotalSize;
15 ar >> saDataContainer;
16 }
17 }
Above the code will be thrown error during the compilation that is error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'CArchive' (or there is no acceptable conversion) at LINE NO:8
saDataContainer is a CStringArray variable how can i write it.....
what can i do please help me
|
|
|
|
|
CStringArray has its own Serialize method. You only do call CStringArray::Serialize and it will do the job for you.
void SRecordset::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << iNumFldCount;
ar << iNumRec;
ar << iTotalSize;
}
else
{
ar >> iNumFldCount;
ar >> iNumRec;
ar >> iTotalSize;
}
saDataContainer.Serialize(ar);
}
--
=====
Arman
|
|
|
|
|
Yah it's working very well thank you very much........
|
|
|
|
|
how to dynamic display the value of mouse's coordinate during the mouse moving
|
|
|
|
|
Within your application window or globally? However, check GetCursorPos()
|
|
|
|
|
how do i set a draggable marker in My editor with CRicheditview as base class
|
|
|
|
|
|
There are many ways of doing it. Some easier than others, it all depends on what you want. If all you want is a line drawn from top to bottom then what I would try is using a borderless, one pixel wide child window that sits on top of the view. If you want anything more complicated then you will have to start drawing the view yourself, not a simple task.
It's not rocket surgery!
Within you lies the power for good, use it!!!
|
|
|
|
|
Hi,
I want to use methods in an activex component (.ocx) file in C++;
In VB I simply used Set obj=CreateObject("MSScriptControl.ScriptControl")
I don't know how should I do the same (either LateBonding or EralyBonding) in C++.
I'd be glad if someone could help me out!
|
|
|
|
|
|
hi once again... tnx for the answers to my previous posts...
again i have another qxn to ask...
will the code below post any memory leak or any pointer problems?
i am unsure as how to write the code for a struct to have an indefinite size of array of structs inside it. so what i did is to just use a pointer and have a member which holds the size of the array pointed to. is there any efficient way to go about this? tnx a lot!
typedef struct
{
int number;
int number2;
} TEST, *LPTEST;
typedef struct
{
LPTEST pTest;
int number;
} TEST_CONT;
TEST test[2];
test[0].number = 123;
test[0].number2 = 45;
test[1].number = 678;
test[1].number2 = 910;
TEST_CONT testC;
testC.pTest = test;
testC.number = 98765;
int n = testC.pTest[0].number;
int n2 = testC.pTest[0].number2;
cout << n << " " << n2 << " " << testC.number << endl;
n = testC.pTest[1].number;
n2 = testC.pTest[1].number2;
cout << n << " " << n2 << " " << testC.number << endl;
newbie
|
|
|
|
|
No memory leak in the code just because there is no object created by 'new'.
TEST_CONT testC;
testC.pTest = test;
testC.number = 98765;
here testC.number should be the number of elements of the test, right? So why 98765?
C++ provides std::vector class which is a dynamically growing array. You might use it like so;
typedef struct
{
std::vector<test> tests;
} TEST_CONT;
TEST_CONT testC;
testC.tests.push_back(test);
//...
int n = testC.tests[0].number;
int n2 = testC.tests[0].number2;
cout << n << " " << n2 << " " << testC.tests.size()<< endl;
--
=====
Arman
|
|
|
|