From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,691379720c755924 X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: String Allocation when Interfacing C with Ada Date: 1996/12/06 Message-ID: <32A82389.35AC@gsfc.nasa.gov>#1/1 X-Deja-AN: 202682041 references: <32A6D464.41C67EA6@avions.aerospatiale.fr> content-type: text/plain; charset=us-ascii organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA mime-version: 1.0 reply-to: Stephen.Leake@gsfc.nasa.gov newsgroups: comp.lang.ada x-mailer: Mozilla 3.0 (Win95; U) Date: 1996-12-06T00:00:00+00:00 List-Id: Paul Chardon wrote: > > Hello, > I'm writing a C program using a string, this string is passed as > parameter to an Ada subprogram in order to modify its value. Is there > any problem of allocation or deallocation in this case. > > For instance: > > -- The main C program > void Handle_String(char * s); > main () > { > char *a_string > > strcpy(a_string,"high"); Since a_string is not initialized, this writes "high" to an arbitrary location in ram, possibly zero. > printf("String Value : %s \n", A_String); > adainit(); This runs Ada elaboration, which could easily overwrite the location pointed to by the un-initialized a_string. > Handle_String(A_String); > printf("String Value : %s \n",A_String); > /* Print only value token in account when using strcpy in the Ada > subprogram, not when using "Ada allocation" */ > adafinal(); > } > -- The imported Ada subprogram > with Interfaces.C.Strings; > with Text_Io; > procedure Handle_String(S : in out Interfaces.C.Strings.Chars_ptr) is > > procedure C_Strcpy > (Target : out Interfaces.C.Strings.Chars_Ptr; > Source : in Interfaces.C.Strings.Chars_Ptr); > pragma import(C, C_Strcpy,"strcpy"); > > begin > -- THAT CALL > S := Interfaces.C.Strings.New_String > ("Value not token in account"); > -- when I put only this call, no value is printed in the C > -- program This should place a valid address in a_string, so I don't see why it wouldn't work. > -- OR THIS OTHER CALL > C_Strcpy(S, > Interfaces.C.Strings.New_String > ("Value token in account")); > -- When I do that copy instead of the first allocation call, the > -- good value is printed in C main function This should write "Value ..." at the original uninitialized location pointed to by a_string, which might get clobbered. > end Handle_String; > pragma Export(C, Handle_String, "Handle_String"); > > If anyone can explain why it doesnt works in the first call case. > Thanks in advance, Paul. I seem to conclude that what doesn't work should, and what should doesn't. Hope you can sort it out! -- - Stephe