Highlighted
Trusted Contributor.
Trusted Contributor.
1495 views

C interpreter run time error: memory violation Exception ACCESS_VIOLATION received.

Jump to solution

Hi

I converted json data into XML format and try to upload data into application. The below json data is divided into 3 parts and concatinated. The logic is working fine upto 30 loops and getting error C interpreter memory violation when given more than 30.

Can anyone please help me on this.

Logic:

int i, X=30;
    char Part2[1000];
    char str0[100],str1[100], str2[100], str3[100], str4[100];
    
    char Part1 [10000]= "<HP_EXTENSION name=\"JsonXml\">"
                    "<object>"
                    "<array name=\"Transaction\">"
                        "<object>"
                            "<pair name=\"Name1\" type=\"str\">338907298155233</pair>"
                            "<pair name=\"Name2\" type=\"str\">5291052371096332798</pair>"
                            "<pair name=\"Name3\" type=\"str\">6535639941614087480</pair>"
                            "<pair name=\"Name4\" type=\"str\"></pair>"
                            "<pair name=\"Name5\" type=\"str\">08\\/02\\/2018 13:41:00</pair>"
                            "<array name=\"Name6\">"
                            "<object>" ;
                            
                            
                            
    char Part3 [20000] = "</array>"
                            "<array name=\"SID1\"></array>"
                        "</object>"
                    "</array>"
                    "<array name=\"SID2\"></array>"
                "</object>"
            "</HP_EXTENSION>" ;
            
            
    strcpy(str0,"");
        
        
    for(i=0; i<X; i++)
        
    {
        strcpy(Part2,"");
        
        // Copiying "<object>" to product data from 2nd loop
        if (i>0) strcpy(str0,"<object>");
        strcat(Part2,str0);
        
     
        //Taking PID1 and concatinated to part2
        strcat(Part2,"<pair name=\"PID1\" type=\"str\">");
        
        //Adding PID1 to str1
        strcpy(str1,lr_eval_string("{PID1}"));
        //Concatinating PID1   with Part2
        strcat(Part2,str1);
        
        
        strcat(Part2,"</pair><pair name=\"PID2\" type=\"str\">");
        strcpy(str2,lr_eval_string("{PID2}"));
        strcat(Part2,str2);
        strcat(Part2,"</pair><pair name=\"PID3\" type=\"str\">");
        strcpy(str3,lr_eval_string("{PID3}"));
        strcat(Part2,str3);        
        strcat(Part2,"</pair><pair name=\"PID4\" type=\"str\">");
        strcpy(str4,lr_eval_string("{PID4}"));
        strcat(Part2,str4);
        strcat(Part2,"</pair></object>");
        
        // Concatinates Part 1 and Part 2 data into Part1
        strcat(Part1,Part2);    
    
    }
    
      // Concatinates outcome of Part1 and Part3
    strcat(Part1,Part3);
    
    
    lr_output_message("%s",Part1);
    lr_save_string(Part1,"Transactiondata");
    
    web_convert_from_formatted("FormattedData={Transactiondata}","TargetParam=Transactiondata",LAST);

 

Recorded script:

web_convert_from_formatted("FormattedData="
            "<HP_EXTENSION name=\"JsonXml\">"
                "<object>"
                    "<array name=\"Transaction\">"
                        "<object>"
                            "<pair name=\"Name1\" type=\"str\">118907298155202</pair>"
                            "<pair name=\"Name2\" type=\"str\">6257793519368761359</pair>"
                            "<pair name=\"Name3\" type=\"str\">2217415671259141099</pair>"
                            "<pair name=\"Name4\" type=\"str\"></pair>"
                            "<pair name=\"Name5\" type=\"str\">08\\/02\\/2018 15:45:00</pair>"
                            "<array name=\"Name6\">"
                                "<object>"
                                    "<pair name=\"PID1\" type=\"str\">f99592df0862c72f</pair>"
                                    "<pair name=\"PID2\" type=\"str\">8761070136735221551</pair>"
                                    "<pair name=\"PID3\" type=\"num\">1</pair>"
                                    "<pair name=\"PID4\" type=\"str\">2510636190904969086</pair>"
                                "</object>"
                            "</array>"
                            "<array name=\"SID1\"></array>"
                        "</object>"
                    "</array>"
                    "<array name=\"SID2\"></array>"
                "</object>"
            "</HP_EXTENSION>",
        "TargetParam=DFE_BODY",
        LAST);

 

 

 

0 Likes
1 Solution

Accepted Solutions
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: C interpreter run time error: memory violation Exception ACCESS_VIOLATION received.

Jump to solution

Your code shows an warning about too much memory on the stack, here is an simple rewrite that uses dynamic memory allocation.

#include "malloc.h"
f()
{
    int i, X=30;
    char object[1000];
    char *buf;
    char head []= "<HP_EXTENSION name=\"JsonXml\">"
                    "<object>"
                    "<array name=\"Transaction\">"
                        "<object>"
                            "<pair name=\"Name1\" type=\"str\">338907298155233</pair>"
                            "<pair name=\"Name2\" type=\"str\">5291052371096332798</pair>"
                            "<pair name=\"Name3\" type=\"str\">6535639941614087480</pair>"
                            "<pair name=\"Name4\" type=\"str\"></pair>"
                            "<pair name=\"Name5\" type=\"str\">08\\/02\\/2018 13:41:00</pair>"
                            "<array name=\"Name6\">";
    char tail [] = "</array>"
                       "<array name=\"SID1\"></array>"
                       "</object>"
                       "</array>"
                       "<array name=\"SID2\"></array>"
                       "</object>"
                       "</HP_EXTENSION>" ;
            
            
	sprintf(object,"<object>%s%s%s%s</object>"
		,lr_eval_string("<pair name=\"PID1\" type=\"str\">{PID1}</pair>")
		,lr_eval_string("<pair name=\"PID2\" type=\"str\">{PID2}</pair>")
		,lr_eval_string("<pair name=\"PID3\" type=\"str\">{PID3}</pair>")
		,lr_eval_string("<pair name=\"PID4\" type=\"str\">{PID4}</pair>")
	);

    buf = (char*) malloc( strlen(head) + X * strlen(object) + strlen(tail) + 1 );

    strcpy(buf, head);
        
    for(i=0; i<X; i++)
    {
        strcat(buf,object);
    }

    strcat(buf, tail);
    
      // Concatinates outcome of Part1 and Part3
    
    lr_output_message("%s",buf);
    lr_save_string(buf,"Transactiondata");
    free(buf);
}
Signature:
Reward community members who take time to respond and help.

View solution in original post

3 Replies
Highlighted
Regular Contributor.
Regular Contributor.

Re: C interpreter run time error: memory violation Exception ACCESS_VIOLATION received.

Jump to solution

You are trying to allocate all that memory on the stack.  The stack is finite space, you need to use the heap.  malloc or calloc

 

Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: C interpreter run time error: memory violation Exception ACCESS_VIOLATION received.

Jump to solution

Your code shows an warning about too much memory on the stack, here is an simple rewrite that uses dynamic memory allocation.

#include "malloc.h"
f()
{
    int i, X=30;
    char object[1000];
    char *buf;
    char head []= "<HP_EXTENSION name=\"JsonXml\">"
                    "<object>"
                    "<array name=\"Transaction\">"
                        "<object>"
                            "<pair name=\"Name1\" type=\"str\">338907298155233</pair>"
                            "<pair name=\"Name2\" type=\"str\">5291052371096332798</pair>"
                            "<pair name=\"Name3\" type=\"str\">6535639941614087480</pair>"
                            "<pair name=\"Name4\" type=\"str\"></pair>"
                            "<pair name=\"Name5\" type=\"str\">08\\/02\\/2018 13:41:00</pair>"
                            "<array name=\"Name6\">";
    char tail [] = "</array>"
                       "<array name=\"SID1\"></array>"
                       "</object>"
                       "</array>"
                       "<array name=\"SID2\"></array>"
                       "</object>"
                       "</HP_EXTENSION>" ;
            
            
	sprintf(object,"<object>%s%s%s%s</object>"
		,lr_eval_string("<pair name=\"PID1\" type=\"str\">{PID1}</pair>")
		,lr_eval_string("<pair name=\"PID2\" type=\"str\">{PID2}</pair>")
		,lr_eval_string("<pair name=\"PID3\" type=\"str\">{PID3}</pair>")
		,lr_eval_string("<pair name=\"PID4\" type=\"str\">{PID4}</pair>")
	);

    buf = (char*) malloc( strlen(head) + X * strlen(object) + strlen(tail) + 1 );

    strcpy(buf, head);
        
    for(i=0; i<X; i++)
    {
        strcat(buf,object);
    }

    strcat(buf, tail);
    
      // Concatinates outcome of Part1 and Part3
    
    lr_output_message("%s",buf);
    lr_save_string(buf,"Transactiondata");
    free(buf);
}
Signature:
Reward community members who take time to respond and help.

View solution in original post

Highlighted
Trusted Contributor.
Trusted Contributor.

Re: C interpreter run time error: memory violation Exception ACCESS_VIOLATION received.

Jump to solution

Thank you very much JHF Remmelzwaal...!!!

 

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.