* String Allocation when Interfacing C with Ada
@ 1996-12-05 0:00 Paul Chardon
1996-12-06 0:00 ` Stephen Leake
0 siblings, 1 reply; 2+ messages in thread
From: Paul Chardon @ 1996-12-05 0:00 UTC (permalink / raw)
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");
printf("String Value : %s \n", A_String);
adainit();
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
-- 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
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.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: String Allocation when Interfacing C with Ada
1996-12-05 0:00 String Allocation when Interfacing C with Ada Paul Chardon
@ 1996-12-06 0:00 ` Stephen Leake
0 siblings, 0 replies; 2+ messages in thread
From: Stephen Leake @ 1996-12-06 0:00 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1996-12-06 0:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-12-05 0:00 String Allocation when Interfacing C with Ada Paul Chardon
1996-12-06 0:00 ` Stephen Leake
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox