Arrays, Functions, and Returning Pointers via Parameters
Author |
Message |
Michael Caracen #1 / 13
|
 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 |
|
 |
Kevin Easto #2 / 13
|
 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: 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. 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 |
|
 |
bd #3 / 13
|
 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 |
|
 |
Michael Caracen #4 / 13
|
 Arrays, Functions, and Returning Pointers via Parameters
It works. Thank you. Sincerely, Michael Caracena
|
Mon, 01 Aug 2005 12:55:15 GMT |
|
 |
Michael Caracen #5 / 13
|
 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 |
|
 |
Michael Caracen #6 / 13
|
 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 |
|
 |
CBFalcone #7 / 13
|
 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 |
|
 |
Michael Caracen #8 / 13
|
 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 |
|
 |
those who know me have no need of my nam #9 / 13
|
 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 |
|
 |
CBFalcone #10 / 13
|
 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 |
|
 |
Michael Caracen #11 / 13
|
 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 |
|
 |
Eric G. Mille #12 / 13
|
 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 |
|
 |
Michael Caracen #13 / 13
|
 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 |
|
|
|