473,653 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing 2d array and realloc

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 = BuildArrayFromV iewCategory( Array, BuildView, BuildCategory,
MAX_FLD, BuildField );
return( ReturnValue );
}
int BuildArrayFromV iewCategory( char Array[MAX_ARRAY][MAXENVVALUE], char
*ViewName, char *Category, int ArrayMax, char *FieldName )
{
char Routine[] = "BuildArrayFrom ViewCategory";

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

}
Nov 13 '05 #1
2 7915

Newsgroup Posting ID <ne************ ****@invalid.bi gpond.com> wrote in
message news:2j******** ************@ne ws-server.bigpond. net.au...
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

Nov 13 '05 #2
On Wed, 17 Sep 2003 12:19:10 GMT, "Newsgroup Posting ID"
<ne************ ****@invalid.bi gpond.com> wrote:
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.ne t
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2468
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks for ( j =0; j<MAX ; j++) { for ( i =0; i<MAX ; i++)
14
8503
by: Sameer | last post by:
Hello, i wish to read a file of int and store into an array dynamically... the size of memory allocated finally, should just be sufficeient to store n integers. I do not know the number of integers in the file... How should I go about creating the array int array dynamically ? the file is
9
9422
by: Colin Doig | last post by:
Hi, I need to pass an array of strings to a function, but I can't get it to work. This is what I wrote : #include <stdio.h> void a_function(char *blah) { printf("%s %s %s", blah, blah, blah); }
3
2862
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); for(count = 0; count < arg_count; count++)
8
3672
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
3
2080
by: Thomas Christmann | last post by:
Hi! Sorry for the weird topic, I don't know how to describe it better... I have a little problem here I can't wrap my mind around. If I do: ------------------------------------- #define DWORD unsigned long #include <stdio.h> #include <malloc.h>
8
1411
by: Magix | last post by:
Hi, I would like to use array of array to store different string, or if you have better idea to store different string in array index. How can I achieve that ? Any example will be greatly appreciate. Thanks.
44
5772
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
5
6198
by: Andreas Schmitt | last post by:
Hi, I recently worked on an open source project and tried to make on of the arrays they are using dynamically allocated to get rid of the max size. I used the realloc instead of the usual C++ new and delete because they asked me to stick to their "coding style" Well whatever..
0
8370
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.