array pointeur crashes

New to FreeBASIC? Post your questions here.
Post Reply
Baptiste
Posts: 17
Joined: May 15, 2019 10:58

array pointeur crashes

Post by Baptiste »

hello ,
I start in the use of pointers and to familiarize myself with this technique, I try to carry out a small program of intersection of two arrays.
the program crashes

could a charitable soul for a very poor programmer like me, help me?

Code: Select all

dim a(1000) as integer
dim b(1000) as integer
dim q as integer
dim i as Integer
dim j as integer
dim i1 as Integer
dim j1 as integer

for i1 = 0 to 9
    for j1  = 0 to 9
        a(i1*10 + j1) = (i1 + 1) * (j1 + 1)
    next j1
next i1 

for i1  = 0 to 9
    for j1 = 0 to 9
        b(i1*10 + j1) = (i1 + 1) * (j1 + 1) * 2
    next j1
next i1
for i1=0 to 9
   print a(i1),b(i1)
next
input q


dim a_ptr as integer ptr
dim b_ptr as integer ptr

a_ptr = @a(0)
b_ptr = @b(0)

dim intersection(10) as integer
dim k as integer = 0
for i as integer = 0 to ubound(a)
    for j as integer = 0 to ubound(b)
        if *(a_ptr + i) = *(b_ptr + j) then
            intersection(k) = *(a_ptr + i)
            k = k + 1
        end if
    next j
next i

for k1 as integer =0 to k-1
   print intersection(k1);" ";
next k1
input q 
stop
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: array pointeur crashes

Post by fxm »

The 'a()' and 'b()' arrays are only initialized for indexes from 0 to 99.
The intersection number is >> 10.

Code: Select all

dim a(99) as integer
dim b(99) as integer
dim i as Integer
dim j as integer
dim i1 as Integer
dim j1 as integer

for i1 = 0 to 9
    for j1  = 0 to 9
        a(i1*10 + j1) = (i1 + 1) * (j1 + 1)
    next j1
next i1 

for i1  = 0 to 9
    for j1 = 0 to 9
        b(i1*10 + j1) = (i1 + 1) * (j1 + 1) * 2
    next j1
next i1
for i1=0 to 9
   print a(i1),b(i1)
next
sleep


dim a_ptr as integer ptr
dim b_ptr as integer ptr

a_ptr = @a(0)
b_ptr = @b(0)

dim intersection(499) as integer
dim k as integer = 0
for i as integer = 0 to ubound(a)
    for j as integer = 0 to ubound(b)
        if *(a_ptr + i) = *(b_ptr + j) then
            intersection(k) = *(a_ptr + i)
            k = k + 1
        end if
    next j
next i

for k1 as integer =0 to k-1
   print intersection(k1);" ";
next k1
sleep
Test your code by compiling it with the '-exx' option to view out-of-bounds array accesses at runtime.
Baptiste
Posts: 17
Joined: May 15, 2019 10:58

Re: array pointeur crashes

Post by Baptiste »

Thanks fxm.
I changed the code and it works.

Code: Select all

 dim a(10) as integer
dim b(10) as integer
dim q as integer
dim i as Integer
dim j as integer
dim i1 as Integer
dim j1 as integer

for i1 = 0 to 9
    for j1  = 0 to 9
        a(i1 ) = (i1 + 1) * (j1 + 1) 
    next j1
next i1 

for i1  = 0 to 9
    for j1 = 0 to 9
        b(i1 ) = (i1 + 1) * (j1 + 1) * 2
    next j1
next i1
for i1=0 to 9
   print a(i1),b(i1)
next
input q


dim a_ptr as integer ptr
dim b_ptr as integer ptr

a_ptr = @a(0)
b_ptr = @b(0)

dim intersection(10) as integer
dim k as integer = 0
for i as integer = 0 to 9'ubound(a)
    for j as integer = 0 to 9'ubound(b)
        if *(a_ptr + i) = *(b_ptr + j) then
            intersection(k) = *(a_ptr + i)
            k = k + 1
        end if
    next j
next i

for k1 as integer =0 to k-1
   print intersection(k1);" ";
next k1
input q 
stop
 
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: array pointeur crashes

Post by fxm »

To be compatible in the general case of any initialization of the 2 arrays to be compared, one cannot therefore predict the number of intersections.
There are then 2 solutions:
- Either fix the number of possible intersections to the maximum value corresponding to the squared size of the 2 arrays to be compared:

Code: Select all

dim intersection(10*10) as integer
- Either define the array of intersections as dynamic and increase its size with each intersection found:

Code: Select all

dim intersection() as integer
dim k as integer = 0
for i as integer = 0 to 9'ubound(a)
    for j as integer = 0 to 9'ubound(b)
        if *(a_ptr + i) = *(b_ptr + j) then
            redim preserve intersection(k)
            intersection(k) = *(a_ptr + i)
            k = k + 1
        end if
    next j
next i
Post Reply