Arrays, Functions, and Returning Pointers via Parameters 
Author Message
 Arrays, Functions, and Returning Pointers via Parameters

I'm having a difficult time understand how to dynamically allocate an
array using
a function parameter instead of returning a pointer to an array.
The program at the end of this message demonstrates what I'm trying to
do.

Both TestArrayAllocMethod1 and TestArrayAllocMethod2 are virtually the
same except for how the array is allocated:  using ArrayAllocMethod1 or
ArrayAllocMethod2.
TestArrayAllocMethod1 works but TestArrayAllocMetho2 doesn't work and
will crash.
I'm having a difficult time figuring out why TestArrayAllocMethod2
crashes.
I'm sure it's simple  in C but I mainly program in another language. ;-)

I'm using LCC.

Thanks for any help offered.

Sincerely,
Michael Caracena

#include <stdio.h>
#include <stdlib.h>

char *ArrayAllocMethod1 ( size_t ArraySize )
{
 char *result;

 result = malloc ( ArraySize );

 return result;

Quote:
}

void ArrayAllocMethod2 ( char *Array, size_t ArraySize )
{
 Array = malloc ( ArraySize );

Quote:
}

void FreeArray ( char *Array )
{
 free ( Array );

Quote:
}

void TestArrayAllocMethod1 ( void )
{
 char *pArray = 0;
 int ArraySize = 1000000;
 int i;

 pArray = ArrayAllocMethod1 ( ArraySize );

 printf ( "TestArrayAllocMethod1:  Accessing Elements ... " );

    for ( i = 0; i < ArraySize; i++ )
 {
     pArray[i] = rand() % (2 << 8);
 }

 printf ( "Success!\n" );

 FreeArray ( pArray );

Quote:
}

void TestArrayAllocMethod2 ( void )
{
 char *pArray = 0;
 int ArraySize = 1000000;
 int i;

 ArrayAllocMethod2 ( pArray, ArraySize );

 printf ( "TestArrayAllocMethod2:  Accessing Elements ... " );

    for ( i = 0; i < ArraySize; i++ )
 {
     pArray[i] = rand() % (2 << 8);
 }

 printf ( "Success!\n" );

 FreeArray ( pArray );

Quote:
}

int main ( void )
{

 TestArrayAllocMethod1 ();
TestArrayAllocMethod2 ();

    system ("PAUSE");

 return 0;

Quote:
}



Mon, 01 Aug 2005 07:39:08 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:

> I'm having a difficult time understand how to dynamically allocate an
> array using
> a function parameter instead of returning a pointer to an array.
> The program at the end of this message demonstrates what I'm trying to
> do.

> Both TestArrayAllocMethod1 and TestArrayAllocMethod2 are virtually the
> same except for how the array is allocated:  using ArrayAllocMethod1 or
> ArrayAllocMethod2.
> TestArrayAllocMethod1 works but TestArrayAllocMetho2 doesn't work and
> will crash.
> I'm having a difficult time figuring out why TestArrayAllocMethod2
> crashes.
> I'm sure it's simple  in C but I mainly program in another language. ;-)

> I'm using LCC.

> Thanks for any help offered.

> Sincerely,
> Michael Caracena

> #include <stdio.h>
> #include <stdlib.h>

> char *ArrayAllocMethod1 ( size_t ArraySize )
> {
> char *result;

> result = malloc ( ArraySize );

> return result;
> }

> void ArrayAllocMethod2 ( char *Array, size_t ArraySize )
> {
> Array = malloc ( ArraySize );
> }

Parameters in C are passed by value, so your changes to "Array" are
local to this function, and lost when it returns.  You need to update an
object in the calling function, so you need to take a pointer to it as
the parameter:

void ArrayAllocMethod2 ( char **Array, size_t ArraySize )
{
    *Array = malloc ( ArraySize );

Quote:
}

You will need to modify the way you call this function, below:

- Show quoted text -

Quote:
> void FreeArray ( char *Array )
> {
> free ( Array );
> }

> void TestArrayAllocMethod1 ( void )
> {
> char *pArray = 0;
> int ArraySize = 1000000;
> int i;

> pArray = ArrayAllocMethod1 ( ArraySize );

> printf ( "TestArrayAllocMethod1:  Accessing Elements ... " );

>    for ( i = 0; i < ArraySize; i++ )
> {
>     pArray[i] = rand() % (2 << 8);
> }

> printf ( "Success!\n" );

> FreeArray ( pArray );
> }

> void TestArrayAllocMethod2 ( void )
> {
> char *pArray = 0;
> int ArraySize = 1000000;
> int i;

> ArrayAllocMethod2 ( pArray, ArraySize );

ArrayAllocMethod2 ( &pArray, ArraySize );

        - Kevin.

- Show quoted text -

Quote:

> printf ( "TestArrayAllocMethod2:  Accessing Elements ... " );

>    for ( i = 0; i < ArraySize; i++ )
> {
>     pArray[i] = rand() % (2 << 8);
> }

> printf ( "Success!\n" );

> FreeArray ( pArray );
> }

> int main ( void )
> {

> TestArrayAllocMethod1 ();
> TestArrayAllocMethod2 ();

>    system ("PAUSE");

> return 0;
> }



Mon, 01 Aug 2005 08:51:13 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:

> I'm having a difficult time understand how to dynamically allocate an
> array using
> a function parameter instead of returning a pointer to an array.
> The program at the end of this message demonstrates what I'm trying to
> do.

> Both TestArrayAllocMethod1 and TestArrayAllocMethod2 are virtually the
> same except for how the array is allocated:  using ArrayAllocMethod1 or
> ArrayAllocMethod2.
> TestArrayAllocMethod1 works but TestArrayAllocMetho2 doesn't work and
> will crash.
> I'm having a difficult time figuring out why TestArrayAllocMethod2
> crashes.
> I'm sure it's simple  in C but I mainly program in another language. ;-)

> I'm using LCC.

> Thanks for any help offered.

> Sincerely,
> Michael Caracena

> #include <stdio.h>
> #include <stdlib.h>

> char *ArrayAllocMethod1 ( size_t ArraySize )
> {
>  char *result;

>  result = malloc ( ArraySize );

>  return result;

You could just do return malloc ( ArraySize );

Quote:
> }

> void ArrayAllocMethod2 ( char *Array, size_t ArraySize )
> {
>  Array = malloc ( ArraySize );

Parameters are local variables, essentially. Any changes to then do not
take efffect. Use a char ** parameter.

Quote:
> }

--
Freenet distribution (temporary): http://24.25.175.161:8891/wbQUz9JhjVM/
Fifth Law of Procrastination:
        Procrastination avoids boredom; one never has the feeling that
        there is nothing important to do.


Mon, 01 Aug 2005 09:05:14 GMT  
 Arrays, Functions, and Returning Pointers via Parameters
It works.
Thank you.

Sincerely,
Michael Caracena



Mon, 01 Aug 2005 12:55:15 GMT  
 Arrays, Functions, and Returning Pointers via Parameters
Thanks for all the help so far.

Unfortunately, I'm still having a difficult time.

I'm trying to test various compilers ( Delphi, LCC, Visual C++, Dev-C++,
etc. ) with performance tests measuring
"Operations per Second" for key features of each language.
I plan on releasing the source code later for other people's use.

I'm having a difficult time allocating 3 arrays,
filling them with "random" numbers,
and returning them using 1 function.
Here is a routine that I thought would work based on the examples
provided,
but crashes when I try to fill the arrays ( using *pA[i] for example )
with different numbers:

int Allocate3ArraysForTesting ( char **pA, char **pB, char **pC, int
ArraySize )
{
 int i;

 *pA = 0;
 *pB = 0;
 *pC = 0;

 printf ( "Allocating Arrays ... " );
 *pA = malloc ( ArraySize );
 *pB = malloc ( ArraySize );
 *pC = malloc ( ArraySize );

 if ( *pA && *pB && *pC )
 {
  printf ( "Success!\n" );
  printf ( "Filling Arrays ... ");

     // Fill Arrays with random numbers
     for ( i = 0; i < ArraySize; i++ )
  {
   *pA[i] = rand() % 256;
       *pB[i] = rand() % 256;
       *pC[i] = rand() % 256;
  }

  printf ( "Success!\n");

  return 1; /* success */
 }
 else
 {
  printf ( "Failure.\n" );

  return 0; /* failure */
 }

Quote:
}

Thanks for any help provided.

Sincerely,

Michael Caracena



Tue, 02 Aug 2005 05:51:49 GMT  
 Arrays, Functions, and Returning Pointers via Parameters
Thanks for all the help so far.

Unfortunately, I'm still having a difficult time.

I'm trying to test various compilers ( Delphi, LCC, Visual C++, Dev-C++,
etc. ) with performance tests measuring
"Operations per Second" for key features of each language.
I plan on releasing the source code later for other people's use.

I'm having a difficult time allocating 3 arrays,
filling them with "random" numbers,
and returning them using 1 function.
Here is a routine that I thought would work based on the examples
provided,
but crashes when I try to fill the arrays ( using *pA[i] for example )
with different numbers:

int Allocate3ArraysForTesting ( char **pA, char **pB, char **pC, int
ArraySize )
{
 int i;

 *pA = 0;
 *pB = 0;
 *pC = 0;

 printf ( "Allocating Arrays ... " );
 *pA = malloc ( ArraySize );
 *pB = malloc ( ArraySize );
 *pC = malloc ( ArraySize );

 if ( *pA && *pB && *pC )
 {
  printf ( "Success!\n" );
  printf ( "Filling Arrays ... ");

     // Fill Arrays with random numbers
     for ( i = 0; i < ArraySize; i++ )
  {
   *pA[i] = rand() % 256;
       *pB[i] = rand() % 256;
       *pC[i] = rand() % 256;
  }

  printf ( "Success!\n");

  return 1; /* success */
 }
 else
 {
  printf ( "Failure.\n" );

  return 0; /* failure */
 }

Quote:
}

Thanks for any help provided.

Sincerely,

Michael Caracena



Tue, 02 Aug 2005 05:51:51 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:

... snip ...

> I'm having a difficult time allocating 3 arrays, filling them
> with "random" numbers, and returning them using 1 function.
> Here is a routine that I thought would work based on the examples
> provided, but crashes when I try to fill the arrays ( using
> *pA[i] for example ) with different numbers:

> int Allocate3ArraysForTesting ( char **pA, char **pB, char **pC,
>                                 int ArraySize )
> {
>  int i;

>  *pA = 0;       /* USELESS, you initialize them below */
>  *pB = 0;
>  *pC = 0;

>  printf ( "Allocating Arrays ... " );
>  *pA = malloc ( ArraySize );
>  *pB = malloc ( ArraySize );
>  *pC = malloc ( ArraySize );

>  if ( *pA && *pB && *pC )  {
>     printf ( "Success!\n" );
>     printf ( "Filling Arrays ... ");

>     // Fill Arrays with random numbers
>     for ( i = 0; i < ArraySize; i++ ) {
>        *pA[i] = rand() % 256;
>        *pB[i] = rand() % 256;
>        *pC[i] = rand() % 256;
>     }
>     printf ( "Success!\n");
>     return 1; /* success */
>  }
>  else  {
>     printf ( "Failure.\n" );
>     return 0; /* failure */
>  }
> }

(slightly reformatted) I see no reason for a crash.  However you
have not shown the calling code, nor the #includes.  BTW, avoid
those // comments.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Tue, 02 Aug 2005 09:46:06 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:

>      /* Fill Arrays with random numbers */
>      for ( i = 0; i < ArraySize; i++ )
>   {
>    *pA[i] = rand() % 256;
>        *pB[i] = rand() % 256;
>        *pC[i] = rand() % 256;
>   }

I have fixed the problem.
There should be () around each array pointer like so:

   (*pA)[i] = rand() % 256;
   (*pB)[i] = rand() % 256;
   (*pC)[i] = rand() % 256;

Sincerely,

Michael Caracena



Tue, 02 Aug 2005 12:54:34 GMT  
 Arrays, Functions, and Returning Pointers via Parameters
in comp.lang.c i read:

Quote:
>I'm trying to test various compilers ( Delphi, LCC, Visual C++, Dev-C++,
>etc. ) with performance tests measuring
>"Operations per Second" for key features of each language.

language?  implementation perhaps.

Quote:
>Here is a routine that I thought would work based on the examples
>provided, but crashes when I try to fill the arrays ( using *pA[i] for
>example ) with different numbers:

your function looked fine.  provided you are calling it like:

  char *a1, *a2, *a3;
  if (Allocate3ArraysForTesting ( &a1, &a2, &a3, rand() )) {
    /* use a1, a2, and a3 */
  }

and provided that <stdlib.h> was included.

--
bringing you boring signatures for 17 years



Tue, 02 Aug 2005 13:10:15 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:


> >      /* Fill Arrays with random numbers */
> >      for ( i = 0; i < ArraySize; i++ )
> >   {
> >    *pA[i] = rand() % 256;
> >        *pB[i] = rand() % 256;
> >        *pC[i] = rand() % 256;
> >   }

> I have fixed the problem.
> There should be () around each array pointer like so:

>    (*pA)[i] = rand() % 256;
>    (*pB)[i] = rand() % 256;
>    (*pC)[i] = rand() % 256;

and I feel foolish.  I should have spotted that.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Tue, 02 Aug 2005 14:03:41 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:
> in comp.lang.c i read:

> >I'm trying to test various compilers ( Delphi, LCC, Visual C++, Dev-C++,
> >etc. ) with performance tests measuring
> >"Operations per Second" for key features of each language.

> language?  implementation perhaps.

What can I say, the sentence was terrible.
What I'm meant to write was that
I'm writing routines in each language to test which compiler
will produce the fastest code for various operations.

Sincerely,

Michael Caracena



Tue, 02 Aug 2005 14:55:00 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:

> I'm having a difficult time allocating 3 arrays,
> filling them with "random" numbers,
> and returning them using 1 function.
> Here is a routine that I thought would work based on the examples
> provided,
> but crashes when I try to fill the arrays ( using *pA[i] for example )
> with different numbers:

> int Allocate3ArraysForTesting ( char **pA, char **pB, char **pC, int
> ArraySize )
> {
>  int i;

>  *pA = 0;
>  *pB = 0;
>  *pC = 0;

>  printf ( "Allocating Arrays ... " );
>  *pA = malloc ( ArraySize );
>  *pB = malloc ( ArraySize );
>  *pC = malloc ( ArraySize );

>  if ( *pA && *pB && *pC )
>  {
>   printf ( "Success!\n" );
>   printf ( "Filling Arrays ... ");

>      // Fill Arrays with random numbers
>      for ( i = 0; i < ArraySize; i++ )
>   {
>    *pA[i] = rand() % 256;
>        *pB[i] = rand() % 256;
>        *pC[i] = rand() % 256;

I think you want:

    (*pA)[i] = rand() % 256;

Unary '*' is lower than '[]' in the order of evaluation.

Personally, I'd probably use intermediaries as it's usually
easier to understand.  As in:

int make_rand_uchar_array (unsigned char **array, size_t size)
{
    unsigned char *data = (size) ? malloc (size) : NULL;

    if (data != NULL) {
        size_t i;
        for (i = 0; i < size; ++i)
            data[i] = rand() % UCHAR_MAX;
        *array = data;
    }

    return (data != NULL || size == 0);

Quote:
}

(Notes: I replaced the magic number with a platform dependent constant
from limits.h and used unsigned chars for the desired semantics.  Also,
malloc(0) is implementation defined, so I opt to allocate nothing
successfully in that case -- others might prefer malloc(1) or
assert(size > 0).)

--



Tue, 02 Aug 2005 18:50:23 GMT  
 Arrays, Functions, and Returning Pointers via Parameters

Quote:

> I think you want:

>     (*pA)[i] = rand() % 256;

> Unary '*' is lower than '[]' in the order of evaluation.

> Personally, I'd probably use intermediaries as it's usually
> easier to understand.  As in:

> int make_rand_uchar_array (unsigned char **array, size_t size)
> {
>     unsigned char *data = (size) ? malloc (size) : NULL;

>     if (data != NULL) {
>         size_t i;
>         for (i = 0; i < size; ++i)
>             data[i] = rand() % UCHAR_MAX;
>         *array = data;
>     }

>     return (data != NULL || size == 0);
> }

I like the intermediaries approach better.
Thanks for the tip.

Sincerely,

Michael Caracena



Wed, 03 Aug 2005 15:03:14 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Best way to return a multi dimensionnal array via a function

3. Returning arrays via functions

4. Pointers: return of pointer to array of pointers to main

5. Returning pointer to array of structure from function

6. function returning pointer array

7. Pointers, Arrays, and function returns

8. Returning and manipulating array/pointer from function to main

9. Returning Int array pointers from functions

10. Question about signal()/pointers to functions that return pointers to functions

11. Function returns pointer to dynamic array

12. function pointers and function pointers array

 

 
Powered by phpBB® Forum Software