Link to home
Start Free TrialLog in
Avatar of superwick
superwick

asked on

C script written in Unix not performing same way in LoadRunner

when compiled/run in unix it correctly places 111111 into donation2 when donation has the value $111,111.00. But when run in LoadRunner the value placed into donation2 is 1$111,111.001aejoykeigwtsve1111

i noticed on the first loop it placed 1$111,111.00 into donation2

loop 1 : 1$111,111.00
loop 2 : 1$111,111.001aejoykeigwtsve
loop 3 : 1$111,111.001aejoykeigwtsve1
loop 4: 1$111,111.001aejoykeigwtsve11
loop 5 : 1$111,111.001aejoykeigwtsve111
loop 6 : 1$111,111.001aejoykeigwtsve1111

in my unix acct when compiled/run :

it does

loop 1: 1
loop 2: 11
... and so forth til 111111

i really have no clue why LR is doing this.. anyone know?
char donation[];
char donation2[];
char x;
int count, count2, i, count3;
 
int main() {
        //donation = "$100,000";
        strcpy(donation, "$100,000.00");
        count = strlen(donation2);
        count2 = strlen(donation)-3;
        printf("the count of donation2 is %d\n", count);
        printf("the count of donation is %d\n", count2);
 
        for (i=1; i<count2; i++) {
                if (isdigit(donation[i])) {
                        donation2[strlen(donation2)] = donation[i];
                }
        }
 
        printf("donation2 is %s\n", donation2);
        int count3 = atoi(donation2);
        printf("converted d2 : %d\n", count3);
 
}

Open in new window

Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

How big are you arrays??
Avatar of superwick
superwick

ASKER

they are dynamically created arrays?

char donation[];
char donation2[];

so if passing "$111,111.00" makes the strlen of donation = 8
and donation2 strlen increases as I add chars to it

at least thats how it works on my unix acct
Avatar of Infinity08
Two things :

1) you have to actually give a size to the arrays that is sufficient to hold whatever data you put in it :

         char donation[16];
         char donation2[16];

2) you have to initialize the donation2 array before you use strlen on it :

         strcpy(donation2, "");     /* <--- empty string */
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yah i did :

strcpy(donation, "$100,000.00");

in my code.. which runs perfectly on my unix acct..  i'm just trying to figure out why this same exact code isn't working in Load Runner
oh actually it should say

strcpy(donation, "$111,111.00");
oh wait u guys are talking about donation2 ok lemme see
>> yah i did :

That's not what I said. Take a look at the two modifications I suggested.


The reason it runs on your Unix account is by pure chance. The code is not correct and exhibits undefined behavior. Undefined behavior means that it might be correct, but it most likely will not.
i tried both suggestions and they didn't fix the problem in LoadRunner.

I'm not sure if its pure chance on unix b/c it works for any random number? i fed the script $123,456.00 , $1,000.00 , $9,999,999,999.00  and it stripped all of the non numerical values correctly on there. I'm a total c beginner too though.. so I'm down with any other suggestions you have?
>> i tried both suggestions and they didn't fix the problem in LoadRunner.

Can you show your current code ?


>> I'm not sure if its pure chance on unix b/c it works for any random number?

It is pure chance, and depends on the contents of memory the moment you run the code. You cannot depend on it being filled with zero's.
Hm.. ok I see what you're saying about the memory now. I tried to initialize donation2 though and that didn't work.
LoadRunner Code:
sprintf(donation, "%s", lr_eval_string("{donationAmt}"));
   
    count = strlen(donation)-3;
	
    count3 = 0;
	
	for (i=1;i<count;i++) {
		if (isdigit(donation[i])) {
			
			temp2 = donation[i];
			donation2[strlen(donation2)] = temp2;
		
		}
	}
 
	donAmt2 = atoi(donation2);
 
	if (donAmt2==rValue) {
		lr_output_message("donAmt2 : %d, is equal to rvalue : %d\n", donAmt2, rValue);
	}
 
 
 
Unix Code:
 
char donation[];
char donation2[];
char x;
int count, count2, i, count3;
 
int main() {
        //donation = "$100,000";
        strcpy(donation, "$100,000,000.00");
        count = strlen(donation2);
        count2 = strlen(donation)-3;
        printf("the count of donation2 is %d\n", count);
        printf("the count of donation is %d\n", count2);
 
        for (i=1; i<count2; i++) {
                if (isdigit(donation[i])) {
                        donation2[strlen(donation2)] = donation[i];
                }
        }
 
        printf("donation2 is %s\n", donation2);
        int count3 = atoi(donation2);
        printf("converted d2 : %d\n", count3);
 
}

Open in new window

Can you try applying the two suggestions EXACTLY as I said ? There are very good reasons for both :

1) the first makes sure that memory is set aside for the strings
2) the second makes sure that the ENTIRE string is filled with zero's, since otherwise your strlen's won't work correctly.
char donation[16];    /* <---- specify the size of the string */
char donation2[16];   /* <---- specify the size of the string */
char x;
int count, count2, i, count3;
 
int main() {
        //donation = "$100,000";
        strcpy(donation, "$100,000.00");
        memset(donation2, 0, 16);     /* <---- initialize the entire buffer with zero's */
        count = strlen(donation2);
        count2 = strlen(donation)-3;
        printf("the count of donation2 is %d\n", count);
        printf("the count of donation is %d\n", count2);
 
        for (i=1; i<count2; i++) {    /* <--- why are you starting at 1 ? */
                if (isdigit(donation[i])) {
                        donation2[strlen(donation2)] = donation[i];
                }
        }
 
        printf("donation2 is %s\n", donation2);
        count3 = atoi(donation2);   /* <---- don't re-declare count3 - you can't do that in the middle of a block anyway if you want to write valid C code */
        printf("converted d2 : %d\n", count3);
        
        return 0;        /* <--- needed to make it valid C code */
}

Open in new window

Your arrays should be bigger than 16 - try 32.
>> Your arrays should be bigger than 16 - try 32.

16 is the value I picked based on the example data "$100,000.00". If larger input strings are possible, then obviously this value needs to be increased. It was just an example though ...
thx.. that worked.. i set all values of the second array to 0 using memset. You rock!
>> thx.. that worked.. i set all values of the second array to 0 using memset. You rock!

Great :) Thank you for the nice comment !