|
hi, is there anyway in C++ that I could get the size of a simple type array?
I'm trying to pass an array to a function without passing the size of the array..
(a.k.a. Wal2k) www.wal2k.com
|
|
|
|
|
not in general.
if you know the arrays are always allocated with malloc, calloc or realloc, you can use _msize to get the allocated size, then divide by the type size. but that's a giant hack and is very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new)
it's best to just pass the array size, or use a dynamic container like std::vector.
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
Chris Losinger wrote:
but that's a giant hack and is an very likely to lead to disaster (ex. if someone passes an array that was created on the stack or via new)
For that reason, I never could get the infamous countof to work!
#define countof(X) (sizeof(X) / sizeof((X)[0]))
Good to know why it doesn't work..
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
That macro only works if the compiler can see X as an array:
#define COUNT_OF(X) (sizeof(X) / sizeof((X)[0]))
void DisplayNumbers(int[6] numbers);
void DisplayNumbers2(int* pNumbers);
void DisplayNumbers3(int* pNumbers, int nSize);
void main()
{
int numbers[] = {1, 2, 3, 5, 8, 13};
int nSize = COUNT_OF(numbers);
DisplayNumbers(numbers);
DisplayNumbers2(numbers);
DisplayNumbers3(numbers, nSize);
}
void DisplayNumbers(int[6] numbers)
{
printf("DisplayNumbers shows:\n ");
int nSize = COUNT_OF(numbers);
for (int i = 0; i < nSize; ++i)
printf("%d ", numbers[i]);
printf("\n");
}
void DisplayNumbers2(int* pNumbers)
{
printf("DisplayNumbers2 shows:\n ");
int nSize = COUNT_OF(pNumbers);
for (int i = 0; i < nSize; ++i)
printf("%d ", numbers[i]);
printf("\n");
}
void DisplayNumbers3(int* pNumbers, int nSize)
{
printf("DisplayNumbers3 shows:\n ");
for (int i = 0; i < nSize; ++i)
printf("%d ", numbers[i]);
printf("\n");
}
The output for this program would be:
DisplayNumbers shows:
1 2 3 5 8 13
DisplayNumbers2 shows:
1
DisplayNumbers3 shows:
1 2 3 5 8 13
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Chris Losinger wrote:
it's best to just pass the array size, or use a dynamic container like std::vector.
True. Another alternative, which is used sometimes for arrays of pointers, are NULL terminated arrays.
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Waleed wrote:
is there anyway in C++ that I could get the size of a simple type array?
no, but you might know the size as you explicitely told the amount of memory needed to build it...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
thanks to all of you, I was thinking about using a special value to end my array as it's posted here...
|
|
|
|
|
Hi,
ive made a combo box which resembles MS' combo box in explorer.exe. only trouble is, when i select an item from the drop down, it doesnt display the icon next to the path. ive added the message OnDrawItem and ive tried overriding DrawItem but that didnt work. Where can i handle the message which will allow me to draw that icon?
Thanks,
Skute
|
|
|
|
|
Hi,
I have to write application to detect Disk configuration.
Is disk type is Basic or Dynamic?
I know Basic disk is supported by all windows versions, but dynamic is supported only by windows 2000, XP Pro, 2003 Server.
I am able to get the OS information but unable to get info for hard disk's.
Can anyone help me?
Thanks,
sanjay
|
|
|
|
|
Amazingly, this code seems to compile without errors in VC6. Are you able to repro this or am I doing something stupid?
#include "stdafx.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
<code>
CWinApp theApp;
void foo
(CString str)
{
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
<code>
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
printf ("Fatal Error: MFC initialization failed");
return (1);
}
unsigned int x = 1;
foo (x); <code>
foo (1); <code>
return (0);
} /ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
I would guess you are seeing an implicit constructor call, probably one of the CString(XCHAR c,int count = 1); variants.
Software Zen: delete this;
|
|
|
|
|
|
VC7.0 and VC7.1 add a macro (ATL_CSTRING_EXPLICIT_CONSTRUCTORS , or something like that) to force compile errors in this case.
Software Zen: delete this;
|
|
|
|
|
Hello,
I'm just curious why you define _tmain() in a MFC application?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Bob Stanneveld wrote:
why you define _tmain() in a MFC application?
It's a console MFC app (specifically, a test framework for a lib that happens to link with MFC).
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
-- modified at 23:57 Monday 5th September, 2005
|
|
|
|
|
Hi,
I use webbrowser control in a vc project,and when navigate to a local file which contains ActiveXObject .Then it blocks .
How can i let the webbrowser direct shows the contenct without user allows it.
A example of it is the Directx Sample Browser .I want just the same .
Some one please help me. Thanks
|
|
|
|
|
I seem to have a compatibility issue with functions accessing the registry. All XP systems work well but 98 and 2K seem to decide they won't play. Its common to all Reg...Ex() functions. Anyone know how to solve this cross Windows issue before I start coding everything in 16bit.
Many thanks
<br />
bool WriteKey(char * key_type, char * key)<br />
{<br />
bool status = true;
<br />
LONG ret;<br />
<br />
HKEY kh1, kh2;<br />
<br />
DWORD disposition;<br />
<br />
char *buf1;<br />
char *buf2;<br />
<br />
buf1 = new char[256];<br />
buf2 = new char[256];<br />
<br />
<br />
ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE, <br />
"SOFTWARE\\KEY1", <br />
0,<br />
NULL,<br />
REG_OPTION_NON_VOLATILE,<br />
KEY_ALL_ACCESS,<br />
NULL,<br />
&kh1,<br />
&disposition);<br />
<br />
ret = RegCreateKeyEx(kh1, <br />
"KEY2",<br />
0,<br />
NULL,<br />
REG_OPTION_NON_VOLATILE,<br />
KEY_ALL_ACCESS,<br />
NULL,<br />
&kh2,<br />
&disposition);<br />
<br />
<br />
if(ret != ERROR_SUCCESS)<br />
{<br />
MsgBox("WriteKey(): RegCreateKeyEx: Failed.");<br />
status = false;<br />
}<br />
<br />
ret = RegSetValueEx(kh2, key_type, 0, REG_SZ, (CONST BYTE *) key, strlen(key) + 1);<br />
<br />
if(ret != ERROR_SUCCESS)<br />
{<br />
MsgBox("WriteKey(): RegSetValueEx: Failed.");<br />
status = false;<br />
}<br />
<br />
ret = RegCloseKey(kh1);<br />
ret = RegCloseKey(kh2);<br />
<br />
if(ret != ERROR_SUCCESS)<br />
{<br />
MsgBox("WriteKey(): RegCloseKey: Failed.");<br />
status = false;<br />
}<br />
<br />
delete buf1;<br />
delete buf2;<br />
<br />
return status;<br />
}<br />
u6ik
|
|
|
|
|
Cracked it. Simple really...
<br />
<br />
ret = RegSetValueEx(kh2, key_type, 0, REG_SZ, (CONST BYTE *) key, strlen(key) + 1);<br />
<br />
if(ret != ERROR_SUCCESS)<br />
{<br />
ret = RegSetValue(kh2, key_type, (CONST BYTE *) key, strlen(key) + 1);<br />
}<br />
Just use the older code if the newer code fails. Bit of a faf, but it works.
u6ik
|
|
|
|
|
Hello.,
I developed an application which stores some data.
Next I create a setup file .
The SetUp file (If I installed in my Machine) works fine in my System, But It can not works fine in other machines.
=>Because I hard coded the DSN name as 'MyDSNName'
and Database Table as 'MyDataBase'
--
I wrote code like this.-=-->
-------------
CString sql,sDate;
CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
CString sDsn;
CString sFile = "c:\\mydatabase.mdb";
CDatabase db;
sDsn.Format("ODBC;DRIVER={%s};DSN='MyDsnName';DBQ=%s",sDriver,sFile);
db.Open(NULL,false,false,sDsn);
CRecordset recset( &db);
------------
>>I want flexible code ,which works fine for all machines.,
Praveen Chowdam Kumar
|
|
|
|
|
parims wrote:
sDsn.Format("ODBC;DRIVER={%s};DSN='MyDsnName';DBQ=%s",sDriver,sFile);
You are using a DSN name that maybe it's not created in the others computers. This article[^]may help you.
Marc Soleda
... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
|
|
|
|
|
Hi,
How can one the setupapi device driver removing?
What do I make wrong in my code?
Can anybody help me at the problem?
Thanks
Peter
<br />
#include "stdafx.h"<br />
#include <stdio.h><br />
#include <windows.h><br />
#include <setupapi.h><br />
#include "cfgmgr32.h"<br />
<br />
int main(int argc, char* argv[])<br />
{<br />
<br />
PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd;<br />
SP_DEVICE_INTERFACE_DATA spdid;<br />
SP_DEVINFO_DATA spdd;<br />
HDEVINFO hDevInfo;<br />
DWORD dwIndex, dwSize, dwType;<br />
TCHAR szProperty[256];<br />
GUID ClassGUID;<br />
TCHAR ClassName[MAX_CLASS_NAME_LEN];<br />
LPTSTR INFFile = argv[1];<br />
CString strInfClassID;<br />
BOOL isInstall = FALSE;<br />
<br />
ZeroMemory(&spdid, sizeof(spdid));<br />
spdid.cbSize = sizeof(spdid);<br />
<br />
if(!SetupDiGetINFClass(INFFile,&ClassGUID,ClassName,sizeof(ClassName),0))<br />
{<br />
printf("GetINFClass");<br />
goto cleanup_DeviceInfo;<br />
}<br />
<br />
LPOLESTR InfBuffer;<br />
<br />
StringFromCLSID( ClassGUID, &InfBuffer );<br />
strInfClassID = InfBuffer;<br />
CoTaskMemFree(InfBuffer);<br />
<br />
<br />
hDevInfo = SetupDiGetClassDevs(&ClassGUID, NULL, NULL,<br />
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);<br />
<br />
<br />
<br />
<br />
<br />
if(hDevInfo == INVALID_HANDLE_VALUE)<br />
{<br />
printf("SetupDiGetClassDevs nicht gefunden.\n ");<br />
goto cleanup_DeviceInfo;<br />
<br />
}<br />
<br />
<br />
<br />
for(dwIndex = 0; (SetupDiEnumDeviceInterfaces(hDevInfo,NULL, &ClassGUID, dwIndex,&spdid)); dwIndex++)<br />
{<br />
<br />
SP_REMOVEDEVICE_PARAMS rmdParams;<br />
<br />
rmdParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);<br />
rmdParams.ClassInstallHeader.InstallFunction = DIF_REMOVE;<br />
rmdParams.Scope = DI_REMOVEDEVICE_GLOBAL;<br />
rmdParams.HwProfile = 0;<br />
<br />
if(!SetupDiSetClassInstallParams(hDevInfo,NULL,&rmdParams.ClassInstallHeader,sizeof(rmdParams)) ||<br />
!SetupDiCallClassInstaller(DIF_REMOVE,hDevInfo,NULL)) <br />
{<br />
printf("Driver not remove.");<br />
}<br />
else<br />
{<br />
printf("Driver remove.");<br />
<br />
}<br />
<br />
<br />
}<br />
<br />
cleanup_DeviceInfo:<br />
<br />
SetupDiDestroyDeviceInfoList(hDevInfo);<br />
<br />
return 0;<br />
}<br />
<br />
<br />
|
|
|
|
|
I have got a handle of a Window and now I want to change color of that window. How do I set new Color for that Window at run-time ?
|
|
|
|
|
|
|
TCHAR* buffer = new TCHAR[1024];
UINT ret = pFile->Read(buffer, 1024);
while ( ret != 0 )
{
buffer[ret] = TEXT('\0');
strText = buffer;
OutWebResource += strText;
ret = pFile->Read(buffer, 1024);
}
pFile->Close();
|
|
|
|
|