|
The title of this thread suggests you are dealing with some function that is returning an array of int s, yet your Basistrefferermittlung() function is simply returning a single int . Is that intentional?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Some issues:
static int Einschlagsnummer;
Is never default initialized.
Some code paths return nothing so whatever is in [e]ax gets returned, which may be 1.
If Trefferchance is <= 80 and m_Schutzwall > 5, it will return 1 for each iteration up to five. However, you stated that m_Schutzwall is 1 which means it's never decremented to zero since the test is for m_Schutzwall > 1, not m_Schutzwall >= 1. This suggests that the return value is likely what happens to be in [e]ax.
now nitpicking:
int Zufallstreffer;
Zufallstreffer=rand()%99+1;
int Trefferchance;
Trefferchance=rand()%99+1;
Why not initialize the variables in the declaration of them?
Why
m_Schutzwall=m_Schutzwall-1; instead of --m_Schutzwall;
|
|
|
|
|
I would like to make a simple program: it gets 2 strings as input, and outputs everything between them. For example, for input 'a' , 'b' , output is:
a b
for input 'a' , 'zz' , output is:
a b c ... x y z aa ab ac ... ax ay az ba bb bc ... bx by bz ... za zb zc ... zx zy zz
What is the most efficient way to do it in C?
|
|
|
|
|
There is no efficient way to do it you have to do a brute force roll of each character in each row.
It's a standard odometer setup roll a column, when that is complete move the next column 1.
Essentially it's just two functions roll character, roll next column a character.
The roll next column will on occasion call itself because rolling one column needs to roll the next and next etc (watch an odometer on a car roll)
As this is usually homework for a programming unit I will give you one of the functions, you need to do the other
void RollCharacter (char* MyStr, unsigned short Pos) {
while (MyStr[Pos] <= 'z') {
printf("Permutation string = %s\r\n", MyStr);
MyStr[Pos]++;
}
}
char TestStr[10] = { 0 };
TestStr[0] = 'c';
RollCharacter(TestStr, 0);
The column roll is fractionally harder but not much.
In vino veritas
|
|
|
|
|
I should also say there is a generic form of counting used in encryption that uses sets of characters to numbers.
It isn't fast to code because it requires loops with modulo and divides but it gives you a count in a set of restricted characters or characters in any order.
Consider a set of characters abcABC and that was the digit order like 1,2,3,4,5,6
So counting 0 = a, 1 = b, 2 = c, 3 = A, 4 = B, 5= C, 6 = aa, 7 = ab, 8 = ac, 9 = ba etc
The code to display a count sequence is done like this but I would not call it efficient
static char CodeSet[6] = { 'a', 'b', 'c', 'A', 'B', 'C'};
void CovertCountToCode(char* Buf, unsigned short BufSize, unsigned long Count) {
if ((Buf) && (BufSize)) {
BOOL notComplete = TRUE;
unsigned short column = BufSize - 2;
memset(Buf, 0x20, BufSize);
Buf[BufSize - 1] = '\0';
do {
unsigned short digit = Count % _countof(CodeSet);
Buf[column] = CodeSet[digit];
column--;
if (Count >= _countof(CodeSet)) {
Count /= _countof(CodeSet);
Count--;
} else notComplete = FALSE;
} while ((notComplete) && (column > 0));
}
}
void CountInCode(unsigned short MaxCount) {
char Buf[10];
unsigned short i;
for (i = 0; i < MaxCount; i++) {
CovertCountToCode(Buf, _countof(Buf), i);
printf("Code string = %s\r\n", Buf);
}
}
CountInCode(50);
In vino veritas
|
|
|
|
|
Efficient in terms of speed or lines of code?
If the latter, create a string buffer and then manipulate it until it is greater than the end.
|
|
|
|
|
Hi,
As part of the application development, we need to launch the Printing Preferences window programmatically for the given printer.
We tried using DocumentProperties() API with fmode as DM_IN_PROMPT.
With this we are able to launch the Printing Preferences of V3 Printers.
When we tried for the V4 Printers, we are getting the standard UI provided by the Microsoft, but the custom UI provided by the manufacturer is not displayed.
Is there any way to launch the custom UI of V4 Printers programmatically?
Your valuable answers are highly appreciated.
Best Regards,
PurnaReddy
-- modified 25-Oct-16 0:55am.
|
|
|
|
|
I think you need to talk to the manufacturer, we have no idea what a V4 printer is.
|
|
|
|
|
r[1000];
*r = (int *) malloc ((n+ 1) * sizeof(int));
but if I use int *r then also it doesn't passes properly .Should I use structure?
The problem should take input like
division-3
input of price-1,5,8
and give rod cutting whereas there are three iteration which should be one r=0;
and the maximum price for this division should be 8 but the result comes 1677218
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<limits.h>
int sum=0;
int max(int a, int b)
{
return(a>b)?a:b;
}
int cut_rod_aux(int price[], int n, int r[])
{ int i;
if(r[n]>=0)
{
return r[n];
printf(" %d\n",r[n]);
}
if (n==0)
{
sum=0;
}
else
{
sum =100000;
for( i=1; i<n;i++)
{
sum=max(sum, price[i]+cut_rod_aux(price, n-i,r));
printf("%d \n",sum);
}
}
r[n]=sum;
return sum;
}
int cut_rod(int price[], int n)
{ int i,r[1000];
*r = (int *) malloc ((n+ 1) * sizeof(int));
for( i=0; i<n;i++) {
r[i]=100000;
printf("i=%d\t r=%d \n",i,r[i]);
}
return cut_rod_aux(price,n,r);
}
int main()
{ int arr[100],i, N;
printf("Enter the number of length that will be divided\n");
scanf("%d", &N);
for ( i = 0;i< N; i++) {
printf("Enter price for %d length\n", (i+1));
scanf("%d", &arr[i]);}
printf("output %d \n",cut_rod(arr, N));
return 0;
}
modified 17-Oct-16 3:21am.
|
|
|
|
|
I am assuming your cut and paste failed because as that appears to me that would not compile the r line has no type
Anyhow your malloc is correct the problem is with r
1.) You can't and don't need the array specifier.
2.) You definitely don't dereference the malloc to it ... "*r" means dereference
Anyhow any quick check of creating a dynamic array would give you the correct syntax
int* r;
r = (int *) malloc ((n+ 1) * sizeof(int));
That will allocate n+1 integers holding the allocated array in pointer r.
To access them you then use r[x] where x is the index position in the array.
It's your responsibility to release the memory when you are done
free(r);
In vino veritas
|
|
|
|
|
|
You have declared r As an array of 1000 integers and then try to assign it a malloc 'ed array of elements. That makes no sense.
modified 17-Oct-16 11:07am.
|
|
|
|
|
I think I finally outdone myself.
#1 <strike>In have class member function to assign pointer to structure from another class. No matter what I do I cannot make the compiler happy.
Closes to "solution" was "need to use static" than when using static I got incomplete type, again. Originally this function was global and the pointer in question was also globally accessible ( function); The pointer ends up in globally accessible struct, so this exercise may be futile anyway. One solution may be to move the assigning function, but I am not sure of its use down the line, I am not there yet,
I am about to give up and make the function global and be done with it. </strike>
OK that one is fixed moved the pointer assinge,t function DONE
#2 I access a class function via pointer however the function calls the calling call method in sort of circular fashion. Again because originally the app was classless , flat.
Any suggestion how to solve this issue?
I really like to keep the functions / methods in relations to hardware , each in its own class.
Sorry for being such pest.
Vaclav
void HCD_SetEnumerationStartFunction(void (*ResetEnd)(void))
{
hcd_ControlStructure.ResetEnd = ResetEnd;
}
hcd->HCD_SetEnumerationStartFunction(A_USBD::USBD_GetDeviceDescriptorBegin);
-- modified 14-Oct-16 13:26pm.
|
|
|
|
|
You keep doing this over and over .. think about it
A_USBD::USBD_GetDeviceDescriptorBegin IS NOT AND NEVER WILL be a "void (*) void" function.
It's a class member function and so it will be a "USBD::void(*)void"
If you need to check stuff the search string "Member function pointer" will get you what you need.
However I strongly advise you to stop and think for a minute before you start passing member
function pointers around. If you need to do this a lot you might as well pass the whole class
in as a pointer it is only 4 bytes and save the effort.
Here lets cross connect both classes via class instance pointers
class USDB;
class HCD;
class USBD {
public:
void ConnectHCD (HCD* hcd);
void SomeFunction (void);
HCD* ConnectedHCD;
};
class HCD {
public:
void ConnectUSBD (USBD* usbd);
USBD* ConnectedUSBD;
};
void USBD::ConnectHCD (HCD* hcd) {
ConnectedHCD = hcd;
}
void USBD::SomeFunction (void){
}
void HCD::ConnectUSBD (USBD* usbd) {
ConnectedUSBD = usbd;
}
USBD usbd;
HCD hcd;
usbd.ConnectHCD(&hcd);
hcd.ConnectUSBD(&usbd);
hcd.ConnectedUSBD->SomeFunction();
See each class can freely use it's connect buddy via the connected pointer.
You just need to remember to connect them before you ever attempt to use them as
the pointers will always start as null until set.
Usually if you have to do this you have a basic design flaw but you can do it.
In vino veritas
modified 15-Oct-16 0:38am.
|
|
|
|
|
Thanks Leon,
after good night sleep I figured it out and came to same conclusion - it it all about function pointer, again.
I'll get it eventually.
Cheers
Vaclav
|
|
|
|
|
No worries just remember classes always push a self pointer down to the call stack before any call to a function.
That is something a static function pointer can not do and why it needs special syntax.
If you just think of a class as a glorified C struct that can hold code as well as data and you will have an exact
understanding of what is happening. The code block in two instances of the class run exactly the same code the
only thing that changes is the code pulls back different self pointers which where pushed down to make the call.
So the same code on different class instances changes data in two different memory blocks because "self" changed.
In vino veritas
|
|
|
|
|
Leon,
I did check couple of "assigning pointers to class functions" articles.
I'll need some time to digest it. Pretty strange syntax.
I can see how "function to function" works, my problem is the way the original code was I have to eventually pass the pointer to struct and now it is in "standard" function pointer format ()(*)() . So I can either add a new member into the control struct ( it passes it to the ISR) or figure out how to convert the "member pointer" to it, if there is a need to do that.
So I really need to study this first.
Still makes me wonder why I picked such obscure "feature". Oh well.
Cheers
Vaclav
|
|
|
|
|
So if that is the case you have a structural problem in how you organized code. However don't panic first get the
code running and the easy way to do that is pass the whole class in to functions as a pointer .. it's only 4 bytes.
You can pass the class pointer freely into another class lets say USBD needs HCD
void USBD:: SomeFuncThatNeedsHCD (HCD* hcd){
hcd->SomeHCDFunction();
}
or you can pass it to a static code block, lets say some static block needs HCD access
static void SomeStaticCodeWhoAccessHCD (HCD* hcd){
hcd->SomeHCDFunction();
}
As I said it's a symptom of a structural problem but a class is nothing more than a fancy struct it is
legal to pass the class around as a pointer just like you did the original struct ... We just frown on it
Yes you can even pass the class pointer to an interrupt just make sure it is valid before the ISR uses.
That usually means set it to NULL and the ISR checks for a non NULL value before it uses.
In vino veritas
|
|
|
|
|
Just an update.
After all this messing around, I came to realize it all boils down to "plain" function pointer usage.
I need to pay more attention to hardware instead of software.
I essentially have two processes - one runs the video display and the other collects the video data using hardware interrupt.
And there is the culprit - the hardware interrupts fires via single function pointer, and nothing but function pointer. No members function pointers, no class to class access via pointers - its all pretty much "down the hill" after the type of interrupt is determined.
But I have learn few things and that is much appreciated.
Thanks
|
|
|
|
|
Yes but that is easy to deal with fire the interrupt to a static function then kick it to the object. That solution is trivial.
PSOMECLASS* TheClassObject = NULL;
static void InterruptComesInHere (void){
if (TheClassObject) {
TheClassObject->CallSomeMethod();
}
}
All you have to do once you have your object initialized ready to receive is set the pointer to the created object using the & symbol.
I would disable interrupts while you do the pointer set
All it really does is makes the interrupt a couple of cycles slower. The harder part by far is dealing with the interupts in
windows which is nowhere near as easy as DOS.
I assume this is a windows app and I wonder have you got the interrupt working because that isn't easy on windows 7,8 & 10
Windows uses hardware interrupts internally and does not let applications mess with them. You need to create a write a kernel-mode driver for that which is not trivial.
To be honest in Windows at your level it would be easier to simply create a thread and poll the hardware for data and get rid of the interrupt all together.
If you are trying to do this sort of thing commercially you are better off porting the IO onto a USB because they have raw block transfer mechanisms and the USB interface can be bought off the shelf. If you look up EasyCap for example which sells for about $10-$15 will drag normal S video into windows via the USB using exactly that trick.
In vino veritas
modified 19-Oct-16 14:02pm.
|
|
|
|
|
Hi,
I developed a software presentation. I wanted to distribute it using cd's & dvd's.
But, the problem is piracy.
so, i wanted to know the different kinds of ways to provide security to my software.(presentation).
Please . Help me. i need a solution.
Thankyou
|
|
|
|
|
|
You first need to be absolutely sure the thing is worth the effort to protect because any protection scheme software or hardware has a cost and it isn't cheap.
Even just putting a software decrypt key that gets stored in the registry or a hidden file means you have a way to produce the keys in some sort of program. That means time and effort to develop the key release program and then physically handle requests by people for a key. It's all cost.
The physical hardware key solution suggested is not cheap either and not much more difficult to break than a software key. I can de-hasp a program in about 30min because it's dead easy to intercept the communication with the hasp plug with a special driver. Many of the pirates you refer too will be able to do the same.
Often you are better off just leaving a nag screen in for products that are low dollar and try and guilt the users into purchase.
There are two alternative marketing tricks
1.) Make the thing so big with graphics and the like that you need multiple DVD roms to distribute and makes torrenting the thing awkward. It becomes such a pain people just go and buy the disk it isn't worth the effort to pirate.
2.) Keep putting out frequent updates even if small, it keeps the pirates disinterested in it.
So are you sure the product is worth the effort?
In vino veritas
|
|
|
|
|
<b>Never mind, instead of trying to find the bug in my code I am going to rebuild the classes,
they need clean-up anyway.</b>
I have , so far , two relatively similar classes dealing with embedded processor hardware.
Here is a code snippet,
....
class IDE_USBD;
class IDE_HCD;
class IDE_ILI9341
{
public:
class IDE_HCD hcd;
class IDE_USBD usbd();
....
If I do not include parenthesizes in implementation of class IDE_USBD I get this error
IDE_ILI9341.h:371: error: field 'usbd' has incomplete type
class IDE_USBD usbd;
^
exit status 1
field 'usbd' has incomplete type
If I add them to class IDE_HCD hcd(); than I get this error:
gpf_isr = hcd.UOTGHS_Handler;
^
IDE_ILI9341.h:2351: error: '((IDE_ILI9341*)this)->IDE_ILI9341::hcd' does not have class type
hcd.HCD_Init();
^
exit status 1
'((IDE_ILI9341*)this)->IDE_ILI9341::hcd' does not have class type
Both classes have #include files before the IDE_ILI9341 gets implemented , I checked that.
Now adding or removing parentheses makes it work, it ain't broke , but why are the parenthesizes needed for IDE_USBD?
BTW that class has only default constructor implemented at this time.
Help solving this puzzle will be appreciated.
Cheers
Vaclav
-- modified 12-Oct-16 23:13pm.
|
|
|
|
|
First you don't put the word "class" in front when using a class, the compiler already knows what IDE_USBD & IDE_HCD are.
However the main problem is you can't forward declare a class and use it as a member
If you don't know these are forward declarations .. you are telling compiler you have a class IDE_USBD & IDE_HCD
class IDE_USBD;
class IDE_HCD;
The problem is it doesn't tell the compiler how big they are and so it can't size them to use as a member. So you need
at least the class data and prototypes to be above where you try and include them into another class
class IDE_USBD {
public:
int Hello(void);
};
class IDE_HCD {
};
class IDE_ILI9341
{
public:
IDE_HCD hcd;
IDE_USBD usbd;
};
int IDE_USBD::Hello(void) {
}
Now the second option is close to your original but it has advantages and disadvantages you need to weight up.
That is turn the entries to pointers to the classes. The compiler will be happy because it knows the size of a
pointer.
class IDE_USBD;
class IDE_HCD;
class IDE_ILI9341
{
public:
IDE_HCD* hcd;
IDE_USBD* usbd;
};
class IDE_USB {
};
class IDE_HCD {
};
The advantages are you can have the classes in whatever order you want. You can change the
class objects on the fly if you need or not even create instances of them if not required.
So you get a fair amount of flexibility.
The downside is you have to call the class functions by pointer hcd->function(blah blah)
from within your class. That makes the code ever so fractionally slower than the other.
We aren't talking very much and the flexibility sometimes makes it very attractive.
The big thing in this form is that class IDE_USB and IDE_USBD will not auto create (its
only a pointer after all). You need to make sure you manually create an instance of the
classes to the pointer in your constructor of IDE_ILI9341. So your constructor must contain
the two lines to actually create the classes to the pointers
hcd = new IDE_HCD ( );
usbd = new IDE_USB ( );
You should also dispose of them in IDE_ILI9341 destructor. As the construction used new it
becomes your responsibility to destroy them as they have no scope to auto dispose.
In vino veritas
modified 13-Oct-16 0:29am.
|
|
|
|
|