473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

What instead of new/delete?

Can I use any method to allocate /deallocate memory other than new/
delete operators? Actually the problem is while using delete operator
in my ".sqc" files the application crashes. It seems the memory
location is locked by DB and so it throws an 'unhandled exception'.
Following is the code snippet from my application and now i dont wanna
touch the basic design.

Regards,
UvT

Code Snippet:
//----------------------------------------------------------------------------
// SQLFetchfun() - Fetch the next record pointing by the cursor.
//-----------------------------------------------------------------------------

long SQLfunFetch(fun *afun)
{
#if 0
EXEC SQL BEGIN DECLARE SECTION;
char hv_fnName[31];
char hv_fnID[16];
char hv_area[11];
short *hv_fnCrType;
char hv_creatorName[21];
char hv_creatorID[11];
EXEC SQL END DECLARE SECTION;
#else
/* char *hv_fnName; //Comments : Here comment the
earlier code & intialize all the
char *hv_fnID; // pointer variables with "new"
declaration.
char *hv_area;
short *hv_fnCrType;
char *hv_creatorName;
char *hv_creatorID;*/

char *hv_fnName = new char[sizeof(afun-
>fnName)*sizeof(TCHAR)]; //Comments : use sizeof var name * sizeof
TCHAR
char *hv_fnID = new char[sizeof(afun->fnID)*sizeof(TCHAR)];
char *hv_area = new char[sizeof(afun->area)*sizeof(TCHAR)];
short *hv_fnCrType;
char *hv_creatorName = new char[sizeof(afun-
>creatorName)*sizeof(TCHAR)];
char *hv_creatorID = new char[sizeof(afun-
>creatorID)*sizeof(TCHAR)];
#endif

EXEC SQL SET SQLCA "gpSqlca";

/* hv_fnName = cfun->fnName; //Comments : Here comment the
earlier code of intialization & use Convertto_mbcs
hv_fnID = cfun->fnID; //
hv_area = cfun->area;
hv_fnCrType = &cfun->fnCrType;
hv_creatorName = cfun->creatorName;
hv_creatorID = cfun->creatorID;*/

Convertto_mbcs(afun->fnName,hv_fnName);
Convertto_mbcs(afun->fnID,hv_fnID);
Convertto_mbcs(afun->area,hv_area);
Convertto_mbcs(afun->creatorName,hv_creatorName);
Convertto_mbcs(afun->creatorID,hv_creatorID);
hv_fnCrType = &afun->fnCrType;
EXEC SQL FETCH RELATIVE 1 funCursor
INTO :hv_fnName,
:hv_fnID,
:hv_area,
:hv_fnCrType,
:hv_creatorName,
:hv_creatorID; //Comments : After the SQL statement is
finished convert the vaules from
// host variable to widechar & fill the unicode
structure
Convertto_widechar(afun->fnName,hv_fnName);
Convertto_widechar(afun->fnID,hv_fnID);
Convertto_widechar(afun->area,hv_area);
Convertto_widechar(afun->creatorName,hv_creatorName);
Convertto_widechar(afun->creatorID,hv_creatorID);

delete hv_fnName; //Comments : Delete the
variables earlier assigned with "new"
delete hv_fnID;
delete hv_area;
delete hv_creatorID;
delete hv_creatorName;

return ( gpSqlca->sqlcode );

}

Mar 12 '07 #1
3 2461
uj***********@gmail.com wrote:
Can I use any method to allocate /deallocate memory other than new/
delete operators?
You don't need to. You're using 'delete' to free memory allocated
by 'new[]'. Use

delete[] blah;

instead. And read up on the differences.
Actually the problem is while using delete operator
in my ".sqc" files the application crashes. It seems the memory
location is locked by DB and so it throws an 'unhandled exception'.
Following is the code snippet from my application and now i dont wanna
touch the basic design.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 12 '07 #2
uj***********@gmail.com wrote:
Can I use any method to allocate /deallocate memory other than new/
delete operators? Actually the problem is while using delete operator
in my ".sqc" files the application crashes.
Not using delete will not make your program run correctly, I'd say you
messed up in some other place, only that it remains invisible until you
try to invoke delete.
It seems the memory location is locked by DB and so it throws
an 'unhandled exception'.
It throws a C++ exception? That would mean that you make some logic error
and the allocation method is not the problem. However, throwing from a
destructor is a bad idea, so there might be other things wrong, too.

Please clean up your code before posting. In particular remove unused parts
and make sure the code can be compiled.
char *hv_fnName = new char[sizeof(afun->fnName)*sizeof(TCHAR)];
What is this supposed to do? Are you using the chars as raw storage or as a
distinct number of chars? In the former case, computing the number of
required chars is correct, in the latter it should only contain the number
of chars you want. Also pay attention that TCHAR is not a standard type
and that its size and underlying type depend on the context. Ask in a
win32-related group about that.
//Comments : use sizeof var name * sizeof TCHAR
What is this comment telling you? Also, if 'afun->fnName' is a pointer, you
will get the size of a pointer and not the size required for the string.
BTW, when sizeof is applied to an object, you don't have to use brackets.

My suggestion:
1. If you are using strings (i.e. text) use std::string, std::wstring or if
you really are working TCHAR-based std::basic_string<TCHAR>. Forget about
new/delete for managing strings.
2. If you need a temporary buffer, use std::vector<>.

EXEC SQL SET SQLCA "gpSqlca";
Nobody without background can interpret this. It is not part of standard
C++. If this is not the problem, you should have removed it during the
cleanup before posting.
Convertto_mbcs(afun->fnName,hv_fnName);
I strongly guess that you messed up between pointer sizes and array sizes.
Use strings, not raw arrays or new/delete.
delete hv_fnName;
If you use the array form of new, you also have to use the array form of
delete. See the C++ FAQ at parashift's.
return ( gpSqlca->sqlcode );
'return' is not a function, no need for brackets.

Uli

--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
Asking smart questions: http://www.catb.org/~esr/faqs/smart-questions.html
Mar 12 '07 #3
In article <et**********@news.datemas.de>, Victor Bazarov
<v.********@comAcast.netwrites
>uj***********@gmail.com wrote:
>Can I use any method to allocate /deallocate memory other than new/
delete operators?

You don't need to. You're using 'delete' to free memory allocated
by 'new[]'. Use

delete[] blah;

instead. And read up on the differences.

And better still use some suitable library type instead of doing manual
allocation/deallocation of dynamic memory. Either std::vector<or
std::string.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Mar 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sid | last post by:
Hi, I was going through this reference code and playing around with it. The code (shown below) shows a class that represents a stack of string pointers In the code below in particular in the...
3
by: cfxchange | last post by:
I am looking into work-arounds for what seems to be a flaw, or "undocumented feature" of SQL Server replication and Instead of Delete triggers not playing together. It seems that if you want to...
4
by: Mark Reed | last post by:
Hi all, I have the following code which imports the contents of all files within a set folder which works excellently. Once it has imported from each file, it deletes the file. Is there a way that...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
7
by: TJS | last post by:
javascript "confirm" fires after deletion instead of before deletion. how do I get this to stop the processing ? code ================== Sub ShowAlert(ByVal s As string)...
18
by: __PPS__ | last post by:
Hello, I'm a university student and I'm preparing for my final today. I'm reading course notes, I found completely strange piece of code. It makes me laugh, I think the teacher needs to prepare...
2
by: Pep | last post by:
I have been investigating a piece of code that deals with buffers and have the real possibility that after a convoluted algorithm to determine the length of the desired buffer is executed, the...
9
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
4
by: sevenever | last post by:
It seems leads to memory leaking on windows + VC. The delete will not free the memory in such condition?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.