non-local pointers to c(m)alloc'ed arrays 
Author Message
 non-local pointers to c(m)alloc'ed arrays

Here is a simplified version of what I want to do:

int main()
        {
        int n, s, a, *sum;
        sum=sumfn(&n, &s, &a);
                .
                .
                .
        }

int *sumfn(int *sn, int *ss, int *sa)
        {
        int *j=calloc(sn*ss+sa, sizeof(int));
                .
                .
                .
        return (j);
        }

Since I'm not using alloca'd memory nor am I free'ing the memory, I can
effectively transfer the array to main() merely by returning its address to
a pointer in main(). I'm fairly certain this is legal although I would like
confirmation just in case.

--
Matthew Shelton

Homepage: www.people.memphis.edu/~mlsheltn



Sat, 21 Oct 2000 03:00:00 GMT  
 non-local pointers to c(m)alloc'ed arrays

Quote:

> Here is a simplified version of what I want to do:

> int main()
>         {
>         int n, s, a, *sum;
>         sum=sumfn(&n, &s, &a);
>                 .
>                 .
>                 .
>         }

> int *sumfn(int *sn, int *ss, int *sa)
>         {
>         int *j=calloc(sn*ss+sa, sizeof(int));
>                 .
>                 .
>                 .
>         return (j);
>         }

> Since I'm not using alloca'd memory nor am I free'ing the memory, I can
> effectively transfer the array to main() merely by returning its address to
> a pointer in main(). I'm fairly certain this is legal although I would like
> confirmation just in case.

Yes, it's legal, but be careful how you think about this. "Transfer to"
doesn't mean anything here. calloc returns a pointer to memory allocated
from the free store. You use the value contained in that pointer to
access the memory. If you copy that value somewhere else you can use the
copied value as well as the original value. In particular:

void use(int *iptr)
{
*iptr = 3;

Quote:
}

int main()
{
int *ip = calloc(1, sizeof(int));
use(ip);
printf("%d\n", *ip);
free(ip);
return 0;

Quote:
}

If you think of what's happening here in terms of transferring memory
you'll be lost: the memory allocated by calloc gets transferred to the
function use but it never comes back. That's not right: use can use the
memory, and main can use the memory. It doesn't belong to any one
function. Instead, think of which functions have pointers to this
memory. Any function with a pointer to this memory can use it.


Sat, 21 Oct 2000 03:00:00 GMT  
 non-local pointers to c(m)alloc'ed arrays

Quote:

> Here is a simplified version of what I want to do:

> int main()
>         {
>         int n, s, a, *sum;
>         sum=sumfn(&n, &s, &a);
>                 .
>                 .
>                 .
>         }

> int *sumfn(int *sn, int *ss, int *sa)
>         {
>         int *j=calloc(sn*ss+sa, sizeof(int));
>                 .
>                 .
>                 .
>         return (j);
>         }

> Since I'm not using alloca'd memory nor am I free'ing the memory, I can
> effectively transfer the array to main() merely by returning its address to
> a pointer in main(). I'm fairly certain this is legal although I would like
> confirmation just in case.

Yes. sumfn returns a (int *), sum is a (int *), so no problem
assigning sum to the return of sumfn. If I understand the paragraph
above correctly, you're concerned about using a pointer that was
never explicitly associated with an allocation - it's no problem at all.

What is a problem however is your use of sn, ss, and sa in
the expression that is the first argument to calloc(). I think
you'll find you want derefs in there (*sn, *ss, *sa, etc).

-usman



Sat, 21 Oct 2000 03:00:00 GMT  
 non-local pointers to c(m)alloc'ed arrays



Quote:
>Here is a simplified version of what I want to do:
>int main()
>    {
>    int n, s, a, *sum;
>    sum=sumfn(&n, &s, &a);
>            .
>            .
>            .
>    }
>int *sumfn(int *sn, int *ss, int *sa)
>    {
>    int *j=calloc(sn*ss+sa, sizeof(int));
>            .
>            .
>            .
>    return (j);
>    }
>Since I'm not using alloca'd memory nor am I free'ing the memory, I can
>effectively transfer the array to main() merely by returning its address to
>a pointer in main(). I'm fairly certain this is legal although I would like
>confirmation just in case.

Your intention is correct, but your impelementation isn't. As far as I
understand you want to allocate an array of n*s+a ints.

Thus your function should look like this

int *sumfn(int sn, int ss, int sa)
        {
        int *j=calloc(sn*ss+sa, sizeof(int));
                .
                .
                .
        return j;
        }

        sum = sumfn(n,s,a);
        ...
        free(sum);

Regards
Horst

 *** Las orillas del Nahuel Huapi ***



Sun, 22 Oct 2000 03:00:00 GMT  
 non-local pointers to c(m)alloc'ed arrays


Quote:

>Here is a simplified version of what I want to do:

>int main()
>    {
>    int n, s, a, *sum;
>    sum=sumfn(&n, &s, &a);
>            .
>            .
>            .
>    }

>int *sumfn(int *sn, int *ss, int *sa)
>    {
>    int *j=calloc(sn*ss+sa, sizeof(int));
>            .
>            .
>            .
>    return (j);
>    }

>Since I'm not using alloca'd memory nor am I free'ing the memory, I can
>effectively transfer the array to main() merely by returning its address to
>a pointer in main(). I'm fairly certain this is legal although I would like
>confirmation just in case.

Yes this is OK. but you must be careful to remember to free the
allocated memory once you are finished with it. For example, in
main, free(sum);

However, in your sample code ablove, the calloc() statement
is illegal. You multiple sn and ss and then add sa. These
operations are all illegal since the variables are pointers.

The valid pointer operations are assignment of pointers of the
same type, adding  or substracting a pointer and an integer,
substracting or comparing two pointers to members of the
same array, and assigning or comparing to zero.

Perhaps you meant to write the statement as:
        int *j=calloc((*sn)*(*ss)+(*sa), sizeof(int));
Even this is hazardous as overflow can easily
occur. You might ought to include overflow
protection in the sumfn().

Al Bowers                                
Tampa, FL

http://www.gate.net/~abowers/index.html



Sun, 22 Oct 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How access alloc'ed string as array?

2. HELP: Dynamically Alloc'd array

3. Non-return of static local malloc'd variable

4. Determining Malloc'ed size via pointer

5. can {m,c}alloc'ed arrays be freed without knowing size?

6. dyn mem alloc - pointer to pointer problem

7. Malloc-ed pointer suddenly became 'unmalloc-ed' !!!??? (Longish)

8. dup()'ed pipe()'s to stdio

9. Dynamic Alloc - Arrays

10. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

11. HELP: ways to alloc multidimensional arrays

12. global alloc and numeric arrays

 

 
Powered by phpBB® Forum Software