|
>>>
Ooops - I missed that one. As a matter of interest, while I would never advocate using anything other than delete [] for an array, I am led to believe under VC++ it makes no difference because of the way it is implimented.
<<<
Good call, unless my debugger is missing the leak, it seems to work perfectly... very strange.
>>>
The careful application of terror is also a form of communication.
<<<
Did you by any chance have a talk with my professors? He seems to have adopted the same philosophy
cheers,
Ben Burnett
---------
On the topic of code with no error handling -- It's not poor coding, it's "optimistic"
|
|
|
|
|
Hello, the codegurus around the world.;)
We can keep the variable by the global variable, reference, and pointer
to keep the variable alive out of the function in especially C.
1) Delcare int something[20] as the global variable.
2) Use the reference by the function parameter.
void func1(int &a[20]);
3) Use the pointer by the funtion parameter
void func2(int *a);
In C++, we can use the member variable by private or protect,
we can simply assign this array or pointer to the new value
inside the function.
Have a nice day!
-Masaaki Onishi-
|
|
|
|
|
>First off, in the second function you should be
>using operator delete[], or else your just deleting
>the first allocated integer -- and thus a memory
>leak.
This is innacurate. The operator 'delete[]' calls a destructor for each member of an array while the operator 'delete' simply deletes the array itself. For simple types which require no destructor, 'delete' works just fine.
(Note: This is definitely Visual C++ behavior. Some may argue that this isn't strictly ANSI C++ but I disagree and here's why: delete[] indicates that I am deleting an array of objects, delete indicates that I am deleting an object which may, or may not, be an array.
Having said that, I actually use malloc/free for the latter case. It's usually faster and makes for clearer code since it indicates that what I wanted was a chunk of memory, nothing more, nothing less.)
|
|
|
|
|
Hi,
i'm programming a demo in win32 and i'm putting everything in classes. Now since i don't use MFC, i need a c++ (not willing to use STL) class that implements an array of objects or a list of objects. This list/array would need to have a quick search algoritm and ideally sorting.
Does anybody know where i can find such a class? Basically all my objects will be in memory after i have read them from file and that will be about 5000-10000 records (objects) maximum. I'm not sure what would be better, a array or a list.
Thanks.
|
|
|
|
|
Ideally you should probably use a binary tree - it will sort in place and be quick to search. Map is one implimentation, but for some reason you don't want to use the tools C++ provides ( STL ) and instead want to write your own. If this is for a learning exercise then great, if not, then what's the problem with STL ?
Christian
#include "std_disclaimer.h"
The careful application of terror is also a form of communication.
Eagles may soar, but weasels don't get sucked into jet engines.
|
|
|
|
|
the reason i don't want (i don't dislike it) to use STL is that i don't think the code is that clear. Plus i don't like their nameing conventions they use. When you write your own classes, you have access to the code as well.
|
|
|
|
|
A simple list is inherently slow to search, because you can only go through sequentially. Your best bet is probably a binary tree (though it depends how your data is organised before loading. Simple binary trees are a disaster if your data is allready sorted).
If your data is loaded in order, use a straight array and search with a binary chop.
If your data is loaded at random use a binary tree. Building a binary tree is pretty trivial if you don't care about balancing (required for optimal search times).
<br />
class CMyNode {<br />
char *key;
...<br />
...<br />
CMyNode *children[2];
<br />
static CMyNode *root;<br />
<br />
CMyNode(...)<br />
static CMyNode *find(const char *);<br />
};<br />
<br />
CMyNode::CMyNode( ... ) {<br />
<br />
...<br />
<br />
CMyNode *p,**pp = &root;<br />
while((p = *pp) != NULL) {<br />
int cmp = strcmp(key, p->key);<br />
if(cmp == 0) {<br />
.. duplicate (Error? )<br />
} else<br />
pp = &p->children[cmp < 0 ? 0 : 1];<br />
}<br />
*pp = this;<br />
children[0] = NULL;<br />
children[1] = NULL;<br />
<br />
..<br />
}<br />
<br />
CMyNode CMyNode::find(const char *key) {<br />
CMyNode *p;<br />
int cmp;<br />
<br />
for(p = root; p; p = p->children[cmp < 0]) {<br />
cmp = strcmp(key, p->key);<br />
if(!cmp)<br />
return p;<br />
}<br />
return NULL;<br />
}<br />
|
|
|
|
|
Thanks for the code.
If i understand correctly, if i'm going to use your code, every node needs a keyvalue and i best not write them to disk sorted. Am i correct?
What if you would have a class with an id, name, and say integer and you want to be able to search for the id, the name and the integer value, would that mean that you have to construct the binary tree every time again for the data? Or would you have 3 binary trees for that 1 class?
Wouldn't a linked list with a simple sort ( quicksort ) be faster then or at least easier to use? Although (i could be wrong) i ahve heard that a binary search is faster.
|
|
|
|
|
The code was a generic example which assumed the key was a text string. If you use another kind of key you need to replace the strcmp with a suitable equivalent comparator (if your records are searchable you obviously do have keys).
If you want several alternate keys you can use the same actual set of nodes to create several trees, each tree has it's own children and root pointers in each node. You only need one set of nodes, but you have several arrangements going at the same time. (Assuming that you search separately for the different keys. If you want to search on a combined key, just combine the comparator).
The trouble with a simple linked list is that you can only walk through it sequentially. You can't, for example, go straight to the middle element for a binary chop.
The binary tree is a binary search, with the same order of search time as a binary chop into a sorted table (order of log(n)).
The only thing is that the tree works efficiently only if it's approximately balanced. That's why it screws up if you put the keys in in sorted order. You wind up adding everything on one branch and it degenerates to a simple list (it still works, but you lose the speed advantage). If the data are random then, on average, the tree will be reasonably balanced. There are algorithms for rebalancing a tree but, believe me, you don't want to know.
|
|
|
|
|
OK, thanks for the info. It cleared up some of the questions i had. You wouldn't have some links to site which have more explanation on this?
|
|
|
|
|
Salut!
Dans un environement Client\Serveur Comment utiliser les threads afin que le Serveur puisse gerer tout les clients du réseau? Est que il faut passer imperativement par les threads d'interface? A partir de quelle fonction on doit lancer le thread (accept?).
Priere de détailler vos réponces afin qu'on puisse comprendre.
Salutations distinguées.
|
|
|
|
|
hmm, in english would be better. this is an international forum
|
|
|
|
|
hee hee..
Nous ne parlons pas Français bien. (or was it meilleur?..)
Voulez-vous parler en Anglais s'il vous plait?
Merci beaucoup!
Baaf.
_________
So, what do you think of my French?
|
|
|
|
|
You can use the following VB Script to create a project:
Set objMSDEV = WScript.CreateObject("msdev.application")
objMSDEV.addproject "test","c:\myapp",TRUE
objMSDEV.ActiveProject.AddFile "test.cpp"
objMSDEV.Quit
WScript.Echo "Done"
Is there a way to do this in Visual C++?
Thanks for any info you can provide.
Steve.
|
|
|
|
|
OK, I've managed to work out that the following code should create a test project:
#include "stdafx.h"
#include<stdio.h>
#include<atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include <initguid.h>
#include<comdef.h>
#include <ObjModel\appauto.h>
#include <ObjModel\appdefs.h>
#include <ObjModel\appguid.h>
#include <ObjModel\bldauto.h>
#include <ObjModel\bldguid.h>
#include <ObjModel\blddefs.h>
#include <ObjModel\textauto.h>
#include <ObjModel\textguid.h>
#include <ObjModel\textdefs.h>
#include <ObjModel\dbgauto.h>
#include <ObjModel\dbgguid.h>
#include <ObjModel\dbgdefs.h>
IApplication *pApp;
int main(int argc, char* argv[])
{
HRESULT hr = CoInitialize(NULL);
if(FAILED(hr))
{
printf("Failed to initialize the COM libraries\n");
return FALSE;
}
hr = CoCreateInstance(CLSID_Application, NULL, CLSCTX_LOCAL_SERVER, IID_IApplication, (void**)&pApp);
if(FAILED(hr))
{
printf("Failed to create an instance of MSDEV\n");
CoUninitialize();
}
CComBSTR bProjectName("project");
CComBSTR bPath("c:\\myproject");
CComBSTR bType("Application");
VARIANT_BOOL bCreateDirs=VARIANT_FALSE;
pApp->AddProject(bProjectName,bPath,bType,bCreateDirs);
pApp->Quit();
pApp=NULL;
CoUninitialize();
return 0;
}
The problem is the call to create the application is generating the following error:
Debug Error!
Program: test.exe
Module:
File: i386\chkesp.c
Line: 42
The value of ESP was not properly saved accros a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
Anbody got any ideas what the problem is?
|
|
|
|
|
Here is the complete error message to go with my previous post.
Debug Error!
Program: test.exe
Module:
File: i386\chkesp.c
Line: 42
The value of ESP was not properly saved accros a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
Anbody got any ideas what the problem is?
|
|
|
|
|
Hello, i am rather in experienced in C++-Programming, but need a hint for a VC++-Problem.
I want to scroll a bitmap in a dialog-based program.
It works well in an SDI-program. I can load and display the bitmap in the dialog-based program, for which I created a CScrollView-window. But if I want to scroll the bitmap, it is not displayed properly. A portion of the bitmap is repeated in the area, that was previously outside.
What can I do ???
baywa
|
|
|
|
|
Hello,
I need to open / save a txt file into a RichEditView at runtime without using the File -> Open / Save menu selections. ie. no user intervention.
I'd like to use the Stream functions of the RichEditView, but I can't figure out how to call the function. Looking at the code in ViewRich.cpp I can see how its supposed to work, but it always fails.
Can anyone give me any ideas? I'm not necessarily looking for the code, but rather some direction so that I can figure it out myself.
I've already got the filename, stored in a CFile. And, a pointer to a CArchive object. I have called:
Stream(arLoad, FALSE);
However, the function needs to know what the value of IsStoring() is. How do I set that?
I've looked at the WordPad sample...it uses custom streamin / streamout functions.
Thanks for any ideas you can give me!
Frank
|
|
|
|
|
Don't use stream(). Use CRichEditDoc::OnFileSave("C:/yada.txt").
|
|
|
|
|
Thanks Baafie...I actually got it to work using streamin. I found an example on MSDN:
static DWORD CALLBACK MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
CFile* pFile = (CFile*) dwCookie;
*pcb = pFile->Read(pbBuff, cb);
return 0;
}
void SomeFunctionThatLoadsAStringFromSomeFileOrSomethingLikeThat()
{
EDITSTREAM es;
es.dwCookie = (DWORD) &myFile;
es.pfnCallback = MyStreamInCallback;
GetRichEditCtrl().StreamIn(SF_TEXT, es);
}
Thanks!
Frank
|
|
|
|
|
You're welcome, but it doesn't look like my advice helped you out..
|
|
|
|
|
The reason I was looking to use Stream (In) is due to the fact that I am writing temp files outside the scope of my doc class.
My app writes out temp data to a file for processing first...after processing it loads it back into the view for user intervention. From there a file save or print or print preview can be performed.
I tried using a loop and loading the file 1 line at a time but it looked rather FREAKY.
MSDN had the example that worked (I love that)...unfortunately for me my subscription ran out in october and I cant afford another one.
Frank
"Gumbercules? - I love that Guy!"
- Dr. Zoidberg
|
|
|
|
|
i write an application and add five class(Recordset) in program but i am change or delete this class.
how to?
thanks for your help
ali
|
|
|
|
|
You go to the file view, highlight and press delete. Then, if you want, go into My Computer, navigate to the file location, hold down shift, press delete and click OK.
Christian
#include "std_disclaimer.h"
The careful application of terror is also a form of communication.
Eagles may soar, but weasels don't get sucked into jet engines.
|
|
|
|
|
You also want to remove the files from the FileView tab.
|
|
|
|