passing 2d array and realloc

  • Thread starter Newsgroup Posting ID
  • Start date
N

Newsgroup Posting ID

i'm new to c and have gotten my program to run but by passing hardcoded
array sizes through the routines, i want to make the array extendable by
using realloc but i'd be doing it two routines down, i need to figure out
how to pass the array such that i know how many elements are in it before i
realloc and that the array gets passed back up the routines in tact. i've
used sizeof but it only works in the first routine, the 2nd and 3rd routines
result in a size of 4 (presumably the size of the pointer to the array) and
how would i free the realloc'd memory?

if anyone has any ideas it would be appreciated.

thanks

cut down versions below


int IsSMTP( void ) //main routine
{
char Routine[] = "IsSMTP";
BOOL ReturnValue = FALSE;
int TagFieldCount = 1;

char TagField[MAX_ARRAY][MAXENVVALUE];

// build the tag field array
TagFieldCount = BuildArrayFLD( TagField, "SMTP" );

return( ReturnValue);
}

int BuildArrayFLD( char Array[MAX_ARRAY][MAXENVVALUE], char *BuildCategory )
//called by issmtp
{
char Routine[] = "BuildArrayFLD";
int ReturnValue = 0;
char BuildView[] = "BFLD";
char BuildField[] = "server";

ReturnValue = BuildArrayFromViewCategory( Array, BuildView, BuildCategory,
MAX_FLD, BuildField );
return( ReturnValue );
}


int BuildArrayFromViewCategory( char Array[MAX_ARRAY][MAXENVVALUE], char
*ViewName, char *Category, int ArrayMax, char *FieldName )
{
char Routine[] = "BuildArrayFromViewCategory";

//this is where the array values are currently set and it would be where it
would be extended via realloc

}
 
M

Mike Wahler

Newsgroup Posting ID said:
i'm new to c and have gotten my program to run but by passing hardcoded
array sizes through the routines, i want to make the array extendable by
using realloc but i'd be doing it two routines down, i need to figure out
how to pass the array such that i know how many elements are in it before i
realloc and that the array gets passed back up the routines in tact. i've
used sizeof but it only works in the first routine, the 2nd and 3rd routines
result in a size of 4 (presumably the size of the pointer to the array)

Good guess. When you pass an array to a function, it 'decays'
into a pointer to its first element. You'll need to pass the
array size as an additional argument (Use type 'size_t').

void foo(int arr[], size_t elems)
{
printf("%lu\n", (unsigned long)elems);
}

int main()
{
int a[10] = {0};
foo(sizeof a / sizeof *a); /* prints 10 */
return 0;
}

If function 'foo()' were to resize the array with 'realloc()'
then you'd need to pass back this new size to the caller so
it knows the new size. You could do this with a return value,
or a pointer to the new size (as a parameter).
how would i free the realloc'd memory?

Use 'free()'

-Mike
 
D

Dave Thompson

i'm new to c and have gotten my program to run but by passing hardcoded
array sizes through the routines, i want to make the array extendable by
using realloc but i'd be doing it two routines down, i need to figure out
how to pass the array such that i know how many elements are in it before i
realloc and that the array gets passed back up the routines in tact. i've
used sizeof but it only works in the first routine, the 2nd and 3rd routines
result in a size of 4 (presumably the size of the pointer to the array) and
how would i free the realloc'd memory?
You can't legally realloc() a pointer which was formed, as yours is,
from an actual (declared) array object, only a pointer to allocated
space, that is one which was obtained from malloc or calloc, or a
previous realloc, and not free'd or invalidated by a previous
(successful) realloc.

A function parameter declared as an array of T actually is (rewritten
as) a pointer to T; even if you specify the bound, as you did, it is
ignored. This applies only immediately to the parameter; if you
declare a 2D array (array of array), it is really pointer-to-array.
Since an array used as an argument in the call, or indeed almost
anywhere else, decays to a pointer, this matches the parameter.
As you use in some cases in your snipped code but not others.

See any decent textbook or tutorial or sections 6 and 7 of the FAQ, at
the usual places and http://www.eskimo.com/~scs/C-faq/top.html .
//this is where the array values are currently set and it would be where it
would be extended via realloc
// comments are not standard in C90, which almost all implementations
still implement, although an easy and fairly common extension; and a
bad idea in Usenet postings anyway as you can see above.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top