|
If you are writing C++ code (you are using classes, which is C++ specific), why do you still want to use C functions like malloc ? Prefer the use of new and delete instead.
|
|
|
|
|
dehseth wrote: sub::sub()
{
a = (char**) calloc(10, sizeof(char));
for (int i = 0; i < 10; i++)
{
a[i] = (char*) calloc(10 , sizeof(char));
}
printf("\n%x - %x - %x\n", a[0], &a[0], *a[0]);
};
sub::~sub()
{
for (int i = 0; i < 50; i++)
{
char** A = a;
char* Ai = a[i];
char* Aii = (char *)*((int*)a[i]);
free(a[i]); // crash point
}
};
you are allocating 10 items (for (int i = 0; i < 10; ) and freeing 50 items (for (int i = 0; i < 50; ) and what you are trying to achieve with char* Aii = (char *)*((int*)a[i]); and you are not freeing the first dimension array "a" in sub and in main(). I prefer you to learn the concepts clearly
|
|
|
|
|
Hi,
Like Cedric has mentioned you should try to use thew new and delete.
dehseth wrote:
a = (char**) calloc(10, sizeof(char)); // main array
for (int i = 0; i < 10; i++)
{
a[i] = (char*) calloc(10 , sizeof(char)); // char arrays
}
a has a size of 10 bytes (calloc(10, sizeof(char)) = 10 * 1 byte) but to
store 10 pointers you need 40 byte since a pointer has a 4 byte size (sizeof(int)).
Rewrite the allocation of a:
a = (char**) calloc(10, sizeof(int));
But again you should try to use the new and delete.
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
BadKarma wrote: a = (char**) calloc(10, sizeof(int));
what about sizeof (char *)
|
|
|
|
|
Good question.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
char* Aii = (char *)*((int*)a[i]);
What is This?
char* Aii = (char *)((int*)a[i]);
|
|
|
|
|
|
Why are you using pure C here?
As you program C++, try to use C++ structures.
Like a std::vector of std::strings .
Or the MFC-class CStringArray.
Also, input and output is much easier using the predefined IOStreams std::cin and std::cout !
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all. Douglas Adams, "Dirk Gently's Holistic Detective Agency"
|
|
|
|
|
You make the same error on each allocation:
For instance your code is
dehseth wrote: a = (char**) calloc(10, sizeof(char));
but a is declared as follows
char ** a;
hence you must allocate memory this way
a = (char**) calloc(10, sizeof(char <big>*</big>));
Please note the * operator inside sizeof .
BTW as Cedric Moonen already stated, in C++ language, new is the preferred way to allocate memory.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
First of all thank you everyone!
I am writing the code in DLL, and this was not the actual code. I need malloc and calloc and free in my code.
Problem solved by using * in sizeof functions. As an example:
previous code:
allocater** a = (allocater**) calloc(10, sizeof(allocater));
new code:
allocater** a = (allocater**) calloc(10, sizeof(allocater<big>*</big>));
I forgot that 50 in loop since I have tried too many things I guess.
I should have noticed this
Thank you everybody again.
Let's keep movin'
|
|
|
|
|
dehseth wrote: free(a[i]); // crash point
What is the value of i ?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Problem has been solved. i is the loop integer which gets values from zero to ten in given example code.
|
|
|
|
|
dehseth wrote: i is the loop integer which gets values from zero to ten in given example code.
Actually, it was being incremented to 50, hence the crash and my question.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
CString csSql= "update [MY_TABLE]"
" set"
" STRING_0 = \'hello\', "
" TIME_0 = \'02/30/2008 12:5:7\'"
" where"
" TABLE_ID = 10"
" and ITEM_ID = 100"
" and TIME_0 = \'01/24/2008 7:10:33\'"
;
//===============================================
When the SQL is executed, an error says:
Data type mismatch in criteral expression (3464)
//===============================================
when I delete last line of the SQL statement
" and TIME_0 = \'01/24/2008 7:10:33\'"
the SQL statement becomes OK.
type of field TIME_0 is Date/Time.
what is wrong?
any sample code for using Date/Time in 'where' of 'update ... set ...' statement?
thanks
|
|
|
|
|
Have you tried displaying the csSql string with a message box and verified that the SQL statement is perfect before executing it on the DB?
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
includeh10 wrote: " STRING_0 = \'hello\', "
" TIME_0 = \'02/30/2008 12:5:7\'"
" where"
Max. possible day value of February 2008 is just 29 together with a leap year. Feb. 30 is an invalid date (02/30/2008).
|
|
|
|
|
hi friends!
i have included one activeX control in my MFC dialog project, through Project-->Add to Project-->Component & controls. and its appeared in the toolbox.
is it possible to remove that control from the toolbox? if yes how, no means why?
because my system is getting dead slow to compile the project ones i have include the ActiveX control. (e.g. MSFlexGrid).
please let knw the answer as soon as possible..
thanks in advance..
truly
AK
|
|
|
|
|
Did you right click the control on the ToolBox?
|
|
|
|
|
Khathar wrote: i have included one activeX control in my MFC dialog project, through Project-->Add to Project-->Component & controls. and its appeared in the toolbox.
It shows you are using VS 6.0 or lower (than 2005)
Khathar wrote: is it possible to remove that control from the toolbox? if yes how, no means why?
1) no, it is the issue with VS [PRB: Can't Remove Controls from the Controls Toolbar in VC++^], go for higher IDE like 2005 or higher it supports easy UI for that.
2) yes, but manually [How to remove a Toolbox component, an OLE Control, or a Component Gallery component from a Visual C++ project
^]
Khathar wrote: (e.g. MSFlexGrid).
remove all the files created (font1.(cpp & h) msflexgrid.(cpp & h) picture1.(cpp & h) rowcursor1.(cpp & h) remove any dependency with other classes like message handlers)
delete or backup your *.dsw file and edit your *.dsp file to remove the sections
# Section <project name> : {7BF80981-BF32-101A-8BBB-00AA00300CAB}
# 2:5:Class:CPicture
# 2:10:HeaderFile:picture1.h
# 2:8:ImplFile:picture1.cpp
# End Section
# Section <project name> : {9F6AA700-D188-11CD-AD48-00AA003C9CB6}
# 2:5:Class:CRowCursor
# 2:10:HeaderFile:rowcursor1.h
# 2:8:ImplFile:rowcursor1.cpp
# End Section
# Section <project name>: {6262D3A0-531B-11CF-91F6-C2863C385E30}
# 2:21:DefaultSinkHeaderFile:msflexgrid1.h
# 2:16:DefaultSinkClass:CMSFlexGrid
# End Section
# Section <project name>: {5F4DF280-531B-11CF-91F6-C2863C385E30}
# 2:5:Class:CMSFlexGrid
# 2:10:HeaderFile:msflexgrid1.h
# 2:8:ImplFile:msflexgrid1.cpp
# End Section
# Section <project name>: {BEF6E003-A874-101A-8BBA-00AA00300CAB}
# 2:5:Class:COleFont
# 2:10:HeaderFile:font1.h
# 2:8:ImplFile:font1.cpp
# End Section
now re load the *dsp and allow the IDE to create fresh *dsw
Khathar wrote: because my system is getting dead slow to compile the project ones i have include the ActiveX control.
Are you sure compile time is because of this, please have a look at [reducing Compilation time^]
|
|
|
|
|
where to add the code for status bar if i need to use it in dialog other than the main dialog,they don't have a OnInitDialog()function in them
RAJA
|
|
|
|
|
|
i did see the coding given in that link,but my question was where should i do it in the newly created dialogs,they don't have a OnInitDialog(...)....so where should i put this code
m_bar.Create(this);
m_bar.SetIndicators(indicators,2);
CRect rect;
GetClientRect(&rect);
m_bar.SetPaneInfo(0,ID_INDICATOR_NISH,
SBPS_NORMAL,rect.Width()-100);
m_bar.SetPaneInfo(1,ID_INDICATOR_TIME,SBPS_STRETCH ,0);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,
ID_INDICATOR_TIME);
RAJA
|
|
|
|
|
Hi,
U can only do it in the OnInitDialog(). The OnInitDialog is similar to OnCreate. So u need to place your code inside the OnInitDialog of the dialog in which u wanted place the Status bar.
thanks
Nitheesh
|
|
|
|
|
I need to add the status bar in this dialog,but i don't have a onInitDialog(),so where should i add the code????????
IMPLEMENT_DYNAMIC(autoscan, CDialog)
autoscan::autoscan(CWnd* pParent )
: CDialog(autoscan::IDD, pParent)
{
}
autoscan::~autoscan()
{
}
void autoscan::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(autoscan, CDialog)
ON_BN_CLICKED(IDOK, &autoscan::OnBnClickedOk)
ON_LBN_SELCHANGE(IDC_LIST2, &autoscan::OnLbnSelchangeList2)
END_MESSAGE_MAP()
void autoscan::OnBnClickedOk()
{
OnOK();
}
void autoscan::OnLbnSelchangeList2()
{
}
RAJA
|
|
|
|
|
If you want to make status bar when you clicked a button you can put your code on that function,now where's problem?
|
|
|
|