|
Are you wanting something like this or this?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I want to do something like this:
char *str = new char[n];
Here n can be vary at runtime.
So anybody tell me how can i do this without new operator?
Manish Patel
|
|
|
|
|
Use
char *str = malloc(n*sizeof(char));
if str is local, then
foo(int n)
{
char str[n];
}
is also valid in vc6.0
Regards,
Pratap
|
|
|
|
|
std::string text(n);
const char* str = &text[0];
Remember the const , though.
Almost the same is possible with the MFC CString.
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"
|
|
|
|
|
to get the char* string of a std::string, you can use the std::string::c_str() function for that
|
|
|
|
|
You are of course right.
I had changed my post from using std::vector<char> to std::string .
I obviously did it without thinking too much
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"
|
|
|
|
|
Actually, it will work even for the string the way you wrote it. It is not standardized yet, but it will be soon, and all the implementation already enable this.
|
|
|
|
|
There had be a 'technical addendum' or something, requiring the std::vector to store its contained items in one continuous memory block.
Also, the complexity-requirements almost demand an implementation like this.
But for std::string, you are really at the mercy of the implementors., I guess (But it works with VC2003 and VC2005).
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"
|
|
|
|
|
jhwurmbach wrote: But for std::string, you are really at the mercy of the implementors., I guess
As I said, in theory yes - but all the implementations allow this, and it will be standardized in C++ 0x.
|
|
|
|
|
The short answer is you can't.
The long answer is:-
You can use a pre-written class like std::string or CString instead of a plain character array but somewhere underneath the neat interface new will be in use if you change the memory size you require at runtime.
One alternative is to fix n but to make it large enough that there won't be a problem. e.g.
char str[1024];
This is always risky and can be wasteful but may be the right solution in some cases.
Another approach is to use BSTR s otherwise known as Bee Stings because they can be very painful. SysAllocString and realted APIs use some special Win32 magic, probably involving reserved memory or non-paged pool to do memory operations faster than ordinary new and delete . If you dig around CP and elsewhere you might even find a string class that uses BSTRs internally but has a nice easy to use CString like interface. It will still likely be less efficient than using plain character arrays though if raw speed is all you're after.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
i think he was just confused about all the complaining answers he got there[^]
|
|
|
|
|
hey, you didn't gave me time to finalize the answer !
here it is[^]
enjoy
|
|
|
|
|
Ahaaa! Use STL instead!
Seriously use the STL, unless you have a good reason not too. The little code line you showed is equivalent to “char* str = (char*)malloc(n)” in C code. Use std::string instead of that and ignore the internal details (life gets complicated there).
I love C, but you have to learn to separate the concepts that apply to C from those that apply to C++.
If you use the STL liberally, then you will rarely need to allocate memory directly.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Hello,
Hope somebody can help me.
I'm using VC++ 2003 and i want to make a connection to MySQL database in my MFC program.
cx_jin comment this:
#include "mysql.h"
#include "winsock.h"
#pragma comment(lib,"libmySQL.lib")
MYSQL mysql;
mysql_init (&mysql);
if(!mysql_real_connect(&mysql,"localhost","root",NULL,"mydb",3306,NULL,0))
{
AfxMessageBox("database connection error");
return FALSE;
}
A link or a thread will do.
Your comment will be a big help.
Thanks.
|
|
|
|
|
TooShy2Talk wrote: I'm using VC++ 2003 and i want to make a connection to MySQL database in my MFC program.
Ok, so what is it about your code snippet that is not working?
TooShy2Talk wrote: Your comment will be a big help.
As would your description of the problem.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks for the time.
Well, I just want to seek help on how can I make the source code above work.
or you have other way to connect to MySQL.
I run the code but I don't have "mysql.h" and i think the lib.
Where can i get these files? Are they downloadable?
I have found other code but I don't know the provider for MySQL.
Please reply.
Thanks
|
|
|
|
|
TooShy2Talk wrote: I run the code but I don't have "mysql.h" and i think the lib.
Exactly how are you running the code in the absence of a necessary .h and .lib file? Sans those, you are not even compiling the code, let alone running it.
To narrow down the problem a bit, try:
MYSQL mysql;
if (mysql_init(&mysql) != NULL)
{
if (! mysql_real_connect(&mysql, "localhost", "root", NULL, "mydb", 3306, NULL, 0))
{
CString str;
str.Format("mysql_real_connect() failed. Error: %s", mysql_error(&mysql));
AfxMessageBox(str);
return FALSE;
}
}
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The following bit of code is from the Atmel AtUsbHid example. Can someone please explain the line DYNCALL(writeData)((UCHAR *)"12") which i believe is part of a DLL, maybe....
I know the code turns on and off a LED but i want to know how the code works....
void CUsbHidDemoCodeDlg::OnLed2()
{
if(Led2 == false) {
Led2 = true;
m_Led2.SetWindowText(_T("LED 2 ON"));
DYNCALL(writeData)((UCHAR *)"12");
}
else {
Led2 = false;
m_Led2.SetWindowText(_T("LED 2 OFF"));
DYNCALL(writeData)((UCHAR *)"02");
}
}
Thanks
Don
|
|
|
|
|
DYNCALL will be a macro defined elsewhere. You need to find the definition to determine what these lines in your sample expand to. It will probably be some kind of function call like
<br />
DYNCALL(writeData)((UCHAR*)"12")); => SomeType result = writeData((UCHAR*)"12"));<br />
Without the macro definition there's no way for me to tell exactly. I often use a similar macro arrangement to call DLL functions and it can expand into quite a lot of code including, loading the Dll, looking up the function, caching the function pointer and making the call in try{}catch{} block so that a bad or missing DLL won't kill your application.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Is it possible to make VC6 use both cores when compiling? I've got HT on but CL.EXE only utilises one core.
|
|
|
|
|
I think the general answer is no. You'd have to run 2 copies of CL.EXE working on different parts of the project and possibly even then you might need to fiddle with processor affinity to stop them fighting over 1 core. Make the move to VS 2005 before it too becomes obselete, the back end really is much better.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
That was what I feared. The problem with moving to VS2005 is that I have a 250k-loc project that I cannot spend the time to convert, so I'm kinda stuck with it.
Oh well, better put in for a new machine...
|
|
|
|
|
A new machine is always good Even with a very large project though conversion may still be worth the time and effort if the original source is good. We ported our little system of 50 COM Dlls using MFC and ATL and MSVCRT in less than 4 person/weeks including moving the CRT string calls over to the secure versions to get rid of the warnings. We've only had 2 very minor bugs as a result in the following 18 months. I was surprised how easy it turned out to be but I had been very fortunate to inherit a great source base.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Steve_Harris wrote: I've got HT...
Is this now considered a dual-core CPU.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
*shrug* dunno. The CL.EXE compilation thread only runs on half of it so it looks very much like one.
|
|
|
|