|
See this[^] tutorial.
/ravi
|
|
|
|
|
|
Hi
i have a function that gets a parameter of type (char **ptr)
i want to reallocate the space for this pointer (reduce or increase)
i have the followig code that raise an error when realloc change the original pointer
(*ptr)=(char *)realloc(*ptr,(originalLength+shift) * sizeof(char));
what is the problem in this line
note : its work fine when realloc returns the same pointer ?
thanks
|
|
|
|
|
I have a idea.
Are you sure ptr is not null (0) before you call realloc.
Because if ptr is NULL (0) then when you call *ptr is wrong.
you can add a code to report that ptr is null like this:
if(!ptr) MessageBox(0,"ptr is null","error",0);<br />
|
|
|
|
|
no its not null because when i debug the code
*ptr contains a string .
and kindly note the the realloc code that i added is used in many calls before for the same array
and its work fine when the array size is in smaller size but if i added some characters to it
then the allocation failed and i get the error (if i added one character !!)
i think this happen when realloc moves the array to another place and returns a new pointer ?
|
|
|
|
|
yeh, so how you add character to this pointer.
If possible, you can copy your code and paste here.
It is hard to explain if you only talk without code.
Ah, I think maybe you allocated memory before for *ptr by another function like *ptr = new char[]. Don't you do that. If it's, it's wrong.
-- modified at 3:41 Sunday 4th March, 2007
|
|
|
|
|
the array is created by adding characters using the following function
void Append(char **buffer,int size,char c)<br />
{<br />
<br />
(*buffer)=(char *)realloc(*buffer,(size+1) * sizeof(char));<br />
(*buffer)[size-1]=c;<br />
(*buffer)[size]='\0';<br />
}
after the array is created i am running the function that raise the error several times and its work fine and i increase and decrease its content using the following function :
void replaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx)<br />
{<br />
<br />
char *ptr=(*replaceIn)+fromIndx;<br />
int replaceWithLen=(replaceWith==NULL)?0:strlen(replaceWith);<br />
int replacedStrLen=toIndx-fromIndx;<br />
int originalStrLen=strlen(*replaceIn);<br />
int shift=replaceWithLen-replacedStrLen;<br />
int bytesToMove=strlen(ptr)+1;<br />
int originalLength;<br />
<br />
printf("%s\n",*replaceIn);<br />
if(replaceWithLen==0)<br />
{<br />
memmove(ptr-1,ptr+replacedStrLen,bytesToMove);<br />
<br />
}<br />
else<br />
{<br />
if(replaceWithLen>replacedStrLen)<br />
{<br />
originalLength=strlen(*replaceIn);<br />
<big>(*replaceIn)=(char *)realloc(*replaceIn,(originalLength+shift+1) * sizeof(char));</big><br />
if(*replaceIn==NULL)<br />
printf("Error while allocating memory");<br />
*((*replaceIn)+originalStrLen)='\0';<br />
ptr=(*replaceIn)+fromIndx;<br />
memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove);<br />
}<br />
else<br />
memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove);<br />
<br />
}<br />
<br />
memcpy(ptr,replaceWith,replaceWithLen);<br />
<br />
}
the error happen at some point in the line in bold !
-- modified at 4:21 Sunday 4th March, 2007
|
|
|
|
|
you have to give me an example that raises error.
I try your code with my example:
int main(){
char *lpstr1 = 0,*lpstr2 = 0;
lpstr1 = (char *)malloc(10);
lpstr2 = (char *)malloc(20);
strcpy(lpstr2,"damnit");
strcpy(lpstr1,"kha");
replaceStr(&lpstr1,lpstr2,2,10);
cout<<"str1 "<<lpstr1<<endl;
cout<<"str2: "<<lpstr2<<endl;
return 0;
};
there is no problem.
I think your code is hard to read.
I do not understand, what do you want when replaceWithLen = 0, i think you dont have to do any think. The logic is complex.
I think you need a new version.
I write this code, I hope it matchs your will. Please try it.
<code>
void myreplaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx){
if((replaceWith==0)||(replaceIn==0)){
printf("%s","nothing to do here\n");
return;
}
if(*replaceIn == 0){
printf("%s","nothing will modified here\n");
return;
}
if(fromIndx > strlen(*replaceIn)){
printf("%s","index outside of string, commonly we dont want this happen\n");
}
DWORD minSize = (strlen(replaceWith) > toIndx - fromIndx) ? (toIndx-fromIndx+1): strlen(replaceWith);
if(strlen(replaceWith+fromIndx) < minSize){
*replaceIn = (char *)realloc(*replaceIn,strlen(*replaceIn + fromIndx) + minSize);
}
memcpy(*replaceIn + fromIndx,replaceWith,strlen(replaceWith));
};
</code>
-- modified at 6:21 Sunday 4th March, 2007
|
|
|
|
|
Can you show how this memory is previously allocated, and used (code)? Possibly, its getting corrupted due to using it wrong way.
|
|
|
|
|
the array is created by adding characters using the following function
void Append(char **buffer,int size,char c)<br />
{<br />
<br />
(*buffer)=(char *)realloc(*buffer,(size+1) * sizeof(char));<br />
(*buffer)[size-1]=c;<br />
(*buffer)[size]='\0';<br />
}
after the array is created i am running the function that raise the error several times and its work fine and i increase and decrease its content using the following function :
void replaceStr(char **replaceIn,char *replaceWith,int fromIndx,int toIndx)<br />
{<br />
<br />
char *ptr=(*replaceIn)+fromIndx;<br />
int replaceWithLen=(replaceWith==NULL)?0:strlen(replaceWith);<br />
int replacedStrLen=toIndx-fromIndx;<br />
int originalStrLen=strlen(*replaceIn);<br />
int shift=replaceWithLen-replacedStrLen;<br />
int bytesToMove=strlen(ptr)+1;<br />
int originalLength;<br />
<br />
printf("%s\n",*replaceIn);<br />
if(replaceWithLen==0)<br />
{<br />
memmove(ptr-1,ptr+replacedStrLen,bytesToMove);<br />
<br />
}<br />
else<br />
{<br />
if(replaceWithLen>replacedStrLen)<br />
{<br />
originalLength=strlen(*replaceIn);<br />
<big>(*replaceIn)=(char *)realloc(*replaceIn,(originalLength+shift+1) * sizeof(char));</big><br />
if(*replaceIn==NULL)<br />
printf("Error while allocating memory");<br />
*((*replaceIn)+originalStrLen)='\0';<br />
ptr=(*replaceIn)+fromIndx;<br />
memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove);<br />
}<br />
else<br />
memmove(ptr+replaceWithLen,ptr+replacedStrLen,bytesToMove);<br />
<br />
}<br />
<br />
memcpy(ptr,replaceWith,replaceWithLen);<br />
<br />
}
the error happen at some point in the line in bold !
|
|
|
|
|
Im doing a calculator project for my c++ class but my operations dont compute properly and i cant quite figure out whats wrong any input would be greatly appreciated.
<br />
<br />
<br />
<br />
<br />
#include <iostream><br />
#include <string><br />
#include <cmath><br />
using namespace std;<br />
string upconv(string instring);<br />
void help();<br />
<br />
int main () {<br />
double memory;
double display;
double number;
string strInput;
char firstChar;
char secondChar;
int inputLength;
<br />
cout << "Enter ? for help" << endl;<br />
while(true) {<br />
cin >> strInput;<br />
strInput = upconv(strInput);<br />
firstChar = strInput[0];<br />
inputLength = strInput.length();<br />
if(inputLength==1) {<br />
switch(firstChar) {<br />
case '+':<br />
cin >> number;<br />
display = display + number;<br />
break;<br />
case '-':<br />
break;<br />
case '*':<br />
break;<br />
case '/':<br />
break;<br />
case '=':<br />
break;<br />
case '?':<br />
help();<br />
break;<br />
case 'I':<br />
break;<br />
case 'N':<br />
display = display * -1;<br />
break;<br />
case 'E':<br />
break;<br />
default:<br />
cout <<"Error invalid command" <<endl;<br />
}
}
else {
if(firstChar == 'M') {
secondChar = strInput[1];<br />
switch(secondChar) {<br />
case 'S':<br />
memory = display;<br />
break;<br />
case 'R':<br />
display = memory;<br />
cout << "Memory equals " << endl;<br />
break;<br />
case '+':<br />
display = memory + display;<br />
break;<br />
case 'C':<br />
break;<br />
default:<br />
cout << "Invalid memory command"<<endl;<br />
}
}
else {<br />
}<br />
}
cout << display << endl;<br />
}
<br />
return 0;<br />
}
<br />
<br />
<br />
string upconv(string instring) {<br />
string newstring=instring;<br />
int len = instring.length();<br />
for(int i=0; i<len; i++) {<br />
newstring[i]= toupper(instring[i]);<br />
}
return newstring;<br />
}
<br />
void help() {<br />
cout << "HELP" << endl;<br />
}<br />
Charles Covington
|
|
|
|
|
You need to tell, how this program is expectedt to work. And how it is behaving.
Charles Covington wrote: //declarations
double memory; //contains value in memory
double display; //contains contents of display
double number; //contains input number
string strInput; //contains an operator or command
char firstChar; //first char of input
char secondChar; //second char of input
int inputLength; //length of input
It is good practice to initialze variables on its declaration.
|
|
|
|
|
You didn't write what exacly is the problem, but i think I know. Maybe I'm wrong, but you've declared variables: double display,memory; and you haven't initialized them with any value (in your case the smart choice would be 0), so these variables contain random values, because they are declared in main's body, and not as a global variables (global variables==0). According to my scribble, in case like this:
cin >> number;<br />
display = display + number;<br />
display will always contain random values.
|
|
|
|
|
Any faster method to change the contrast of ab image......... OTHER than that unlock and unsafe method its very slow...... Is there anything else?????????
|
|
|
|
|
For starters, a faster method than WHAT method?
And what is "ab image"?
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
I don't understand what exactly you want, but I can give you the general math. Assuming each pixel is represented by the 3 RGB components with values between 0.0 and 1.0:
_________________________________________________________
a) Apply contrast gain GCon with:
new_pix.r=(old_pix.r-0.5)*GCon+0.5
new_pix.g=(old_pix.g-0.5)*GCon+0.5
new_pix.b=(old_pix.b-0.5)*GCon+0.5
This operation corresponds to changing the distance of each component relative to the center (0.5). GCon=1.0 means no contrast change (the distance remains the same), GCon>1.0 means increasing contrast, and GCon<1.0 means decreasing contrast. GCon=0.0 means pure gray (midscale). You should, of course, use saturated arithmetic.
_________________________________________________________
b) Apply brightness gain GBri with:
new_pix.r=old_pix.r*GBri
new_pix.g=old_pix.g*GBri
new_pix.b=old_pix.b*GBri
This operation corresponds to changing the distance of each component relative to black (0.0). This is the most subjective operation, since there are many ways to change the perceived light intensity (which we usually call brightness) to model various physical or electronic effects. GBri=1.0 means no brightness change (all components remain the same), GBri>1.0 means increasing brightness (all components become closer to 1.0), and GBri<1.0 means decreasing brightness (all components closer to black). You should, of course, use saturated arithmetic.
_________________________________________________________
c) Apply color gain GCol with:
new_pix.r=(old_pix.r-old_avg_rgb)*GCol+old_avg_rgb
new_pix.g=(old_pix.g-old_avg_rgb)*GCol+old_avg_rgb
new_pix.b=(old_pix.b-old_avg_rgb)*GCol+old_avg_rgb
The "old_avg_rgb" is the average of the 3 RGB components. This operation corresponds to changing the distance of each component relative to the average of all the 3 components. GCol=1.0 means no color change, GCol>1.0 means increasing color, and GCol<1.0 means decreasing color. GCol=0.0 means black&white. You should, of course, use saturated arithmetic.
The fastest way to implement any of these operations is to do it in hardcore assembly language. However, I sugest a specific layered aproach:
1) First implement in traditional C/C++. If everything fails then the algorithm will still work. This will also serve as a reference to ensure you are doing the operation you want the right way, and also as a benchmark.
2) Implement in traditional I386 assembly language. On a decent processor (like a Pentium 4 at 3.4 GHz) we will probably see small increase in speed relative to 1), somewhere in the range of the 5% or 10% (probably not worth the trouble). On an older processor (like a Pentium Pro or 486) you may see a larger increase, like 50% or 100% (probably worth the trouble). This will depend on your artistic skills when writing assembly. This speed gain, which we are comparing to 1), will also depend heavilly on the C/C++ compiler used. I did my tests against VC6 with full optimizations in 1). Other compilers may achieve not so good optimizations in 1), so 2) may be worth it (since the performance of 2) will be guaranteed by design and thus be compiler-independent). I also assume I386 assembly since it will yield the best results with the smallest amount of effort. I am also pretty sure that the speed of a good I386 assembly implementation will never fall behind the best possible compiler optimization of C/C++. So, I386 will be a guaranteed "greater or equal" to C/C++.
3) Implement in MMX assembly language. This technology is available in many processors (since the Pentium MMX at 166MHz I think), but you should test at run time if the processor running the code supports MMX. There are assembly instructions you can use to determine this. If MMX is not available, then fallback to 2) or 1). This will yield benefits in speed which easily go up to 600% gain compared to 1), even if you program it badly. At this point other aspects of the computer (like memory speed) become important. I'm pretty sure that an average MMX implementation will always be much faster than the best implementations of 2) or 1).
4) Implement in SSE2 assembly language. This technology is available in not so many processors (but I think all Pentium 4 have it). Again, you should test at run time if the processor running the code supports SSE2. There are assembly instructions you can use to determine this. If SSE2 is not available, then fallback to 3), 2), or 1). This will yield benefits in speed which I have measured to be in the range of about 1200% when compared to 1). At this point every little thing in the computer's hardware affects speed. I'm pretty sure that an average SSE2 implementation will always be much faster than an average MMX implementation, and always much much much faster than the best implementations of 2) or 1).
The I386, MMX, and SSE2 assembly languages are simple enough to make it worth using them. Especially SSE2. Programming with SSE2 is a little more anoying than with MMX because you will want to take advantage of the larger register sizes which may put constraints on image size. The best is to do the bulk with MMX or SSE2 and leave the remainder (which is not a multiple of register size) to C/C++ or I386 assembly.
It is very important to note that if you are aiming at the higher speed while maintaning some portability, then your best strategy will be to implement all 2), 3), and 4) in the release version of your program, so that, at run time, the software can always execute as fast as possible for the given hardware. If your software finds SSE2 then uses it, otherwise it tries MMX, and if that also fails it uses I386 assembly. Also you may want to add specific implementations for other processors, like the AMD (does it have any specific instructions?).
I hope this helps,
Rilhas
|
|
|
|
|
how do i connect to SQl SERVER WHIT vc++.NET?
|
|
|
|
|
|
Trivia question time... in any given class, typical OO programming suggests access functions rather than public member variables. I have seen a variety of "styles", and worse, a mix of styles. I'd like to know what your thoughts are on the matter, mindful that it's pretty trivial...
To send data to a class:
put_Something(value)
PutSomething(value)
set_Something(value)
SetSomething(value)
On the retrieval end:
get_Something()
GetSomething()
What do you like and why?
Charlie Gilley
Will program for food...
Whoever said children were cheaper by the dozen... lied.
My son's PDA is an M249 SAW.
My other son commutes in an M1A2 Abrams
|
|
|
|
|
I use :
Thing GetSomething();
SetSomething( Thing t );
why ? because it's simple.
|
|
|
|
|
I've developed a simple dialog based project and I've added a menu
as new resource. Then I've associated the menu to the dialog through
the property editor of the dialog.
Now, I've mapped ON_UPDATE_COMMAND_UI and ON_COMMAND event in the
dialog class (for any menu item) in order to manage it.
The situation in the following:
- the menu is shown;
- ON_COMMAND events are properly mapped;
- ON_UPDATE_COMMAND_UI events are not managed as I expect.
The method associated with the ON_UPDATE_COMMAND_UI event is called when
I click on the menu item (before calling the ON_COMMAND method) instead
of being called before showing the menu item.
I've tryed to do the same thing in a simple SDI project and in this case
it works fine (but in that case the meenu is load by code in OnCreate method).
Are there some settings to do ?
Have I done something wrong when I've associated the menu resource to the dialog
through the property editor ?
How can I solve this problem ?
Thanks a lot !
|
|
|
|
|
|
Hi,
What is the best book to learn Visual Studio 2005 (specially VC++). I'm really good on C++, but I would like to port my software to a complex VC++ gui interface. So I need some very advanced issues about the visual programming.
Thanks a lot,
Dirso
|
|
|
|
|
Visual C++? Are you intending to buy a book on Managed C++? (Since you've mentioned 2005).. For unmanaged code, I would suggest you to buy Programming Windows by Charles Petzold. That would give you a good start. For MFC, You could start with Programming Windows with MFC, by Jeff Prosise. Good luck.
Let us not listen to those who think we ought to be angry with our enemies, and who believe this to be great and manly. Nothing is so praiseworthy, nothing so clearly shows a great and noble soul, as clemency and readiness to forgive.
- Marcus Tullius Cicero
|
|
|
|
|
How can we design a program that when executes is not shown in the Task Manager.
I mean the program can't be stopped with the help of Task Manager.
Apurv
|
|
|
|
|