|
Post by bigbass on Apr 27, 2020 3:19:04 GMT 1
this demo worked since 2013 basic-converter.proboards.com/post/12473'--- typedef struct with pointer the second example is the same but uses pointersthat is the example that fails in runtime with ERROR: signal for SEGMENTATION FAULT received - memory invalid or array out of bounds? (program exited with code: 11) 11 says Could not resolve hostname that doesn't give me a clue it does nothing with the web any help or workaround would be great UPDATE I compiled it with debug and got this Program received signal SIGSEGV, Segmentation fault. 0x00014338 in main (argc=1, argv=0x7efff3a4) at fails.bac:31 31 stuff->release = (char*)result
I will abandon using struct and use CLASS Joe
|
|
|
Post by Pjot on Apr 27, 2020 7:55:27 GMT 1
Hi bigbass, If you use pointers in C code, then this means you point to memory where some data is stored. The code must have access to that memory though. This only is possible by allocating the memory. Declaring a pointer does not automatically allocate memory. Please refer to this page to see how it works. So I just tested your demo with BaCon 3.9.3 and it crashes on me also. If you would run your demo in a previous BaCon on a previous platform using Valgrind, then you'll see the same error warnings about unallocated memory. Sometimes Linux is merciful about memory and does not crash, but that is just a matter of luck... HTH Peter
|
|
|
Post by bigbass on Apr 27, 2020 15:58:37 GMT 1
hello Peter
I appreciate you looking at this and your feedback and taking the time to debug my example and the link to the info
well, I guess I got lucky before without a crash ( I wont go that road again) but luck is not something to bank on or continue pushing your luck with. crash and burn is no fun
in theory bacon can compile that and get a result I see why some GUI tool kits moved to C++ for easy memory management
and this isn't a fault of bacon its just more complex I know I could use RECORD with _type also I did a demo with that before
Joe
if forced to do something like this enclose it or use RECORD in c style or CLASS in c++
PRAGMA COMPILER gcc PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-deprecated
OPTION PARSE FALSE
USEC struct point { int x; int y; }; struct point my_point = { 3, 7 }; struct point *p = &my_point; /* p is a pointer to my_point */ //(*p).x = 8; /* set the first member of the struct */ p->x = 8; /* equivalent method to set the first member of the struct */
printf("%d\n",p->x );
ENDUSEC
OUT PUT 8
|
|
|
Post by bigbass on Apr 27, 2020 17:01:41 GMT 1
gave a compile error removed code
|
|
|
Post by alexfish on Apr 27, 2020 17:18:55 GMT 1
Hi Joe
not sure if something emanating again from this forum , but have looked at the bits : no odd chars
anyway from latest fossil
I get this
Invalid preceding regular expression Runtime error: function 'REGEX' at line 777 in 'bacon.bac': Illegal regex expression
BR Alex
|
|
|
Post by bigbass on Apr 27, 2020 18:06:35 GMT 1
Thanks Alex for testing I am just going to remove the non USEC example and let this one go for another time I usually learn more when something goes wrong so all is well Joe
|
|
|
Post by alexfish on Apr 27, 2020 18:31:27 GMT 1
Hi Joe I had the same problem so yesterday looked a bit further basic-converter.proboards.com/thread/1154/bacon-record-reference-pointersmay not be the best since valgrind shows a sligh memloss no much at its base since it stays the same as per def , creating more makes no difference that is the only solution at this moment in time , one thing there is you have to use the point by ref access at least in that way they appear to be secure bit like using Gtk Widget, call func ((Widget *) widget) I think says he. BR Alex
|
|
|
Post by alexfish on Apr 27, 2020 18:52:43 GMT 1
Hi Joe had a look at gobject methods As in reading further on the typedef struct HEREADDED and found out something I was looking for in my own toolkit done with C++ and ended up with crashes solved that by mutating & or define array of , to use , code for this does not look pretty, and will bomb at the first misshap because C++ cannot really create object types at runtime.BR Alex
|
|
|
Post by bigbass on Apr 28, 2020 6:49:48 GMT 1
here is much easier in c++ (baconized)
fighting with forcing the syntax is no fun I just need it to work and follow some standard format
PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive
OPTION PARSE FALSE
'---why c++ is much easier '---The class does the declaring for you
CLASS point public: int x; int y; END CLASS
'---pointer to an object declaration (we use a class called point) DECLARE ptr TYPE point* '--- correct syntax for class in memory ptr = new point()
'--- initialize the x and y in c++ ptr->x = 3 ptr->y = 4
PRINT " x = ",ptr->x FORMAT "%s%d\n" PRINT " y = ", ptr->y FORMAT "%s%d\n"
TERMINAL OUT x = 3 y = 4
|
|
|
Post by bigbass on Apr 28, 2020 16:51:24 GMT 1
Ok finally in just BaCon
with the c style Idea I hope this is stable in memory
that should be enough for struct CLASS and RECORD when using pointers
'---by bigbass 4-28-2020
RECORD point LOCAL x TYPE int LOCAL y TYPE int END RECORD
'--- dont forget the "magic word" _type as a suffix DECLARE my_point TYPE point_type
'---easier to do it like this DECLARE p TYPE point_type*
p = &my_point
'---assign a value to the RECORD '--- using a pointer to a member p->x = 9
PRINT p->x FORMAT "%d\n"
TERMINAL OUTPUT 9
tested the memory
==6700== HEAP SUMMARY: ==6700== in use at exit: 24 bytes in 5 blocks ==6700== total heap usage: 6 allocs, 1 frees, 1,048 bytes allocated ==6700== ==6700== LEAK SUMMARY: ==6700== definitely lost: 0 bytes in 0 blocks ==6700== indirectly lost: 0 bytes in 0 blocks ==6700== possibly lost: 0 bytes in 0 blocks ==6700== still reachable: 24 bytes in 5 blocks ==6700== suppressed: 0 bytes in 0 blocks
|
|
|
Post by bigbass on Apr 28, 2020 20:18:49 GMT 1
Well I am going to do a little luck pushing if it works for you ok if not just forget it and use RECORD that is the most secure way (just for the sake of not giving up only ) It allocated memory in c for this one so it does the same as in c++ but more work and no USEC to be seen anywhere PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <string.h> PRAGMA OPTIONS -Wno-int-conversion
'---use a struct with a pointer! '---allocate pointer in memory
OPTION PARSE FALSE
struct stock { \ char symbol[5]; \ int quantity; \ float price; \ }; struct stock (*invest) '--- Create structure in memory invest=(struct stock * )malloc(sizeof(struct stock)) DECLARE stock TYPE struct
strcpy(invest->symbol,"IBM") invest->quantity = 100 invest->price = 801.19
PRINT invest->symbol FORMAT "%s\n" PRINT invest->quantity FORMAT "%d\n" PRINT invest->price FORMAT "%.2f\n" PRINT invest->quantity*invest->price FORMAT "%.2f\n"
TERMINAL OUT IBM 100 801.19 80119.00
|
|
|
Post by vovchik on Apr 28, 2020 20:35:17 GMT 1
Dear Joe,
Thanks for that very clear and complete example...
With kind regards, vovchik
|
|
|
Post by alexfish on Apr 28, 2020 23:38:13 GMT 1
Hi Joe
looks good but here getting warning pointer without cast : so something simple not working tried void , did not work ?
anyway it does free
PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <string.h> PRAGMA OPTIONS -Wno-int-conversion
'---use a struct with a pointer! '---allocate pointer in memory
OPTION PARSE FALSE
struct stock { \ char symbol[5]; \ int quantity; \ float price; \ }; struct stock (*invest) '--- Create structure in memory invest=(struct stock * )malloc(sizeof(struct stock)) DECLARE stock TYPE struct
strcpy(invest->symbol,"IBM") invest->quantity = 100 invest->price = 801.19
PRINT invest->symbol FORMAT "%s\n" PRINT invest->quantity FORMAT "%d\n" PRINT invest->price FORMAT "%.2f\n" PRINT invest->quantity*invest->price FORMAT "%.2f\n"
free(invest) will look further in valgrind
valgrind first results
==15438== HEAP SUMMARY: ==15438== in use at exit: 26 bytes in 5 blocks ==15438== total heap usage: 6 allocs, 1 frees, 42 bytes allocated ==15438== ==15438== LEAK SUMMARY: ==15438== definitely lost: 0 bytes in 0 blocks ==15438== indirectly lost: 0 bytes in 0 blocks ==15438== possibly lost: 0 bytes in 0 blocks ==15438== still reachable: 26 bytes in 5 blocks ==15438== suppressed: 0 bytes in 0 blocks ==15438== Rerun with --leak-check=full to see details of leaked memory ==15438== ==15438== For counts of detected and suppressed errors, rerun with: -v ==15438== Use --track-origins=yes to see where uninitialised values come from ==15438== ERROR SUMMARY: 6785 errors from 254 contexts (suppressed: 176 from 5)
added re run --track-origins=yes
==15499== Conditional jump or move depends on uninitialised value(s) ==15499== at 0x40178D8: calloc (dl-minimal.c:109) ==15499== by 0x400BBB7: _dl_new_object (dl-object.c:75) ==15499== by 0x4006067: _dl_map_object_from_fd (dl-load.c:1059) ==15499== by 0x400939F: _dl_map_object (dl-load.c:2605) ==15499== by 0x4000FD3: map_doit (rtld.c:719) ==15499== by 0x401049F: _dl_catch_error (dl-error.c:187) ==15499== by 0x4000F5B: do_preload (rtld.c:908) ==15499== by 0x4001CA3: handle_ld_preload (rtld.c:1006) ==15499== by 0x40039B3: dl_main (rtld.c:1750) ==15499== by 0x401713B: _dl_sysdep_start (dl-sysdep.c:249) ==15499== by 0x400137B: _dl_start_final (rtld.c:424) ==15499== by 0x400160B: _dl_start (rtld.c:652) ==15499== Uninitialised value was created by a stack allocation ==15499== at 0x4001BE4: handle_ld_preload (rtld.c:983)
BR Alex
|
|
|
Post by bigbass on Apr 29, 2020 1:47:38 GMT 1
ok I removed the warning -Wno-int-conversion we don't need to quiet the warning its better to fix what causes it when you can
and yes we need the free too thanks Alex for the push
this should do it cleanly now (I hope for everyone)
P.S thanks vovchik hope all is well with you haven't heard from you for a few days
Joe
PRAGMA INCLUDE <stdio.h> PRAGMA INCLUDE <stdlib.h> PRAGMA INCLUDE <string.h> 'PRAGMA OPTIONS -Wno-int-conversion
'---use a struct with a pointer! '---allocate pointer in memory
OPTION PARSE FALSE
struct stock { \ char symbol[5]; \ int quantity; \ float price; \ }; DECLARE stock TYPE struct DECLARE invest TYPE struct stock* struct stock *invest '--- Create structure in memory invest = (struct stock* )malloc(sizeof(struct stock))
strcpy(invest->symbol,"IBM") invest->quantity = 100 invest->price = 801.19
PRINT invest->symbol FORMAT "%s\n" PRINT invest->quantity FORMAT "%d\n" PRINT invest->price FORMAT "%.2f\n" PRINT invest->quantity * invest->price FORMAT "%.2f\n"
free(invest)
|
|
|
Post by alexfish on Apr 29, 2020 1:57:17 GMT 1
Brill
looks like systems go or start:
Forgot to mention if have more than 0, 1 of more try calloc and look at the resources
BR Alex
|
|