|
(no subject): msg#00133programming.swig
Hi, first of all I apologize in advance if the problems I encountered has already been discussed here. 1 .I have a problem when I try to work with C-structures using SWIG-wrapped C-functions in Perl. In a nutshell, why it makes a difference if I call: # prints actual content of the structure SwigTest::print_string("$Cstruct::cstruct->{key}"); or # prints nothing! SwigTest::print_string($Cstruct::cstruct->{key}); In the latter case a null(!) pointer is passed to the function. The function prototype is void print_string(const char * str); and the struct is defined as typedef struct cstruct { char *key; } cstruct_t; The argument passed to the function will be cast in char * pointer. So, whatever it is, it will not be zero after casting. But if it is zero, how comes that "quoting" the argument converts it to a nonempty string ? If instead of C-structure a Perl hash is used, there problem disappear. Environment: perl v5.8.5 built for sun4-solaris, SWIG Version 1.3.22. Compiled with g++ [sparc-sun-solaris2.8], gcc version 3.4.1. Here is the full source: //--------------SwigTest.c--------------// #include <stdio.h> #include "SwigTest.h" void print_string(const char * str) { if(0==str) printf("ZERO POINTER!\n", str); else if(0==*str) printf("Empty string!\n", str); else printf("%s\n", str); } cstruct_t *cstruct_make() { cstruct_t *cstruct; cstruct=(cstruct_t *)malloc(sizeof(cstruct_t)); return(cstruct); } //--------------------------------------// //--------------SwigTest.h--------------// typedef struct cstruct { char *key; } cstruct_t; void print_string(const char * str); cstruct_t * cstruct_make(); //--------------------------------------// //--------------SwigTest.i--------------// %module SwigTest %{ #include <stdio.h> #include "SwigTest.h" %} typedef struct cstruct { char *key; } cstruct_t; %name(print_string) extern void print_string(const char *str); %name(cstruct_make) extern cstruct_t * cstruct_make(void); //--------------------------------------// //--------------Cstruct.pm--------------// package Cstruct; use strict; use SwigTest; use vars qw($hr $cstruct); # This package provides access to two variables # One is an ordinary perl hash, with one element # The other is a hash tied via SWIG to a C structure containing one element my %hash; $hr =\%hash; print "$hr\n"; $hr->{key} = "Contents of hash element with key \"key\""; $cstruct=SwigTest::cstruct_make() or die; $cstruct->{key}="Contents of hash element linked via Swig to C structure element"; print "[Loaded: ", __FILE__, "]\n"; 1; //--------------------------------------// __________________________________ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail _______________________________________________ Swig maillist - Swig@xxxxxxxxxxxxxxx http://mailman.cs.uchicago.edu/mailman/listinfo/swig |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Preventing a user to refer to non-existent struct members: 00133, Marcelo Matus |
|---|---|
| Next by Date: | Re: Preventing a user to refer to non-existent struct members: 00133, Robert Lupton |
| Previous by Thread: | wrapping static librariesi: 00133, Scott Murman |
| Next by Thread: | (no subject): 00133, Bryga 66 |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |