473,785 Members | 2,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Again on passing arrays as arguments

Thanks to all of you because I solved the problem related with my
previous post.
I simply made confusion with pointers to pointers and then succeeded
passing the reference to the first element pointer.
:-))
You were right about by mixed code: quickly writing an example to
clarify the matter I made a C++ program, sorry for the inconvenience.

Anyway, although it all works fine with monodimensional arrays,
I still have a problem with irregular matrices (2D arrays with the
first dimension fixed, the second one variable).

I wrote a simple example code which perfectly works in the main
routine, while it arises an access violation if I put it in a
subroutine.
My purpose is to create an irregular matrix like this:

p[0][0] (no element)
p[1][0] p[1][1]

The two code blocks (in "main" and in "test") are identical, just
"p" changes in "*m".
The example is below.

Could you explain me why this occurs ?

Thanx again,
Morgan.
_______________ _______________ _______________ _______________ _____
int main(int argc, char *argv[])
{
int **p;

p = (int **)malloc(2*siz eof(int *));
if (p == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[0] = (int *)malloc(sizeof (int));
if (p[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[1] = (int *)malloc(2*size of(int));
if (p[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}

p[0][0] = 1; printf("%d\n",p[0][0]);
p[1][0] = 2; printf("%d\n",p[1][0]);
// Next line is normally executed here !!!!!!!!!!!!!!! !!!!
p[1][1] = 3; printf("%d\n",p[1][1]);

free(p);

test(&p);

printf("%d\n",p[0][0]);
printf("%d\n",p[1][0]);
printf("%d\n",p[1][1]);

free(p);

system("PAUSE") ;
return 0;
}

void test(int ***m)
{
*m = (int **)malloc(2*siz eof(int *));
if (*m == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[0] = (int *)malloc(sizeof (int));
if (*m[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[1] = (int *)malloc(2*size of(int));
if (*m[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}

*m[0][0] = 1;
*m[1][0] = 2;
// Next line causes an access violation here !!!!!!!!!!!!!!! !!!!
*m[1][1] = 3;
// For the exactness it seems to me that the access violation
// arises after the last line, coming back to main... :-( !?
}
_______________ _____________HE LP !!!____________ _______________ __
Nov 14 '05 #1
2 4315
On Sun, 08 Feb 2004 06:29:30 -0800, Morgan wrote:
Thanks to all of you because I solved the problem related with my
previous post.
I simply made confusion with pointers to pointers and then succeeded
passing the reference to the first element pointer.
:-))
You were right about by mixed code: quickly writing an example to
clarify the matter I made a C++ program, sorry for the inconvenience.
If it's a C++ program, why'd you cross-post to comp.lang.c? The code below
looks an awful lot like C, so I'm kind of confused. Also, in acllc-c++, we
ask that you prefix your subject line with either [C] or [C++] so we can
easily tell which language you're using.
Anyway, although it all works fine with monodimensional arrays, I still
have a problem with irregular matrices (2D arrays with the first
dimension fixed, the second one variable).

I wrote a simple example code which perfectly works in the main routine,
while it arises an access violation if I put it in a subroutine. My
purpose is to create an irregular matrix like this:

p[0][0] (no element)
p[1][0] p[1][1]

The two code blocks (in "main" and in "test") are identical, just "p"
changes in "*m".
The example is below.

Could you explain me why this occurs ?

Thanx again,
Morgan.
_______________ _______________ _______________ _______________ _____ int
main(int argc, char *argv[])
You're not using argc or argv. If you don't use them, leave them out.
{
int **p;

p = (int **)malloc(2*siz eof(int *));
In C, don't cast the return from malloc; it gains you nothing, and it can
hide bugs (which it did in this case). In C++, use `new' instead. If you
use malloc, you should remember to include <stdlib.h> (or <cstdlib> in C++).
if (p == NULL) {printf("Error: Out of Memory \n"); exit(1);}
You need <stdio.h> (or <cstdio> in C++) for printf. 1 is a non-portable
exit code. The only portable codes are 0 (indicates success),
EXIT_SUCCESS, and EXIT_FAILURE.
p[0] = (int *)malloc(sizeof (int));
if (p[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[1] = (int *)malloc(2*size of(int));
if (p[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
Please you more vertical space. It's your friend, really. Until you become
much more familiar with the language, one statement per line is a good
rule of thumb.
p[0][0] = 1; printf("%d\n",p[0][0]);
p[1][0] = 2; printf("%d\n",p[1][0]);
// Next line is normally executed here !!!!!!!!!!!!!!! !!!!
It took me a while to figure out what you meant by that comment. I don't
know if you're a native English speaker or not, but you meant, "Next line
is executed normally here." Inverting the order of those two words changes
the meaning drastically.
p[1][1] = 3; printf("%d\n",p[1][1]);

free(p);
Memory leak: you forgot to free() p[0] and p[1] first.
test(&p);

printf("%d\n",p[0][0]);
printf("%d\n",p[1][0]);
printf("%d\n",p[1][1]);

free(p);

system("PAUSE") ;
return 0;
}

void test(int ***m)
{
*m = (int **)malloc(2*siz eof(int *));
if (*m == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[0] = (int *)malloc(sizeof (int));
if (*m[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[1] = (int *)malloc(2*size of(int));
if (*m[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
You've got duplicated code here. Why not call test() twice instead of
having cut-and-pasted code?
*m[0][0] = 1;
*m[1][0] = 2;
// Next line causes an access violation here !!!!!!!!!!!!!!! !!!!
*m[1][1] = 3;
// For the exactness it seems to me that the access violation // arises
after the last line, coming back to main... :-( !?
You've got the order of operator precedence wrong. You meant

(*m)[0][0] = 1;
(*m)[1][0] = 2;
(*m)[1][1] = 3;
}
_______________ _____________HE LP !!!____________ _______________ __


Josh
Nov 14 '05 #2

"Morgan" <up*****@tin.it > wrote in message
news:ec******** *************** **@posting.goog le.com...
Thanks to all of you because I solved the problem related with my
previous post.
I simply made confusion with pointers to pointers and then succeeded
passing the reference to the first element pointer.
:-))
You were right about by mixed code: quickly writing an example to
clarify the matter I made a C++ program, sorry for the inconvenience.
I am reading this in alt.comp.learn. c-c++ and it looks like you have also
posted this to comp.lang.c.
The people in comp.lang.c won't be happy if your posting C++ code to their
newsgroup.
Anyway, although it all works fine with monodimensional arrays,
I still have a problem with irregular matrices (2D arrays with the
first dimension fixed, the second one variable).

I wrote a simple example code which perfectly works in the main
routine, while it arises an access violation if I put it in a
subroutine.
My purpose is to create an irregular matrix like this:

p[0][0] (no element)
p[1][0] p[1][1]

The two code blocks (in "main" and in "test") are identical, just
"p" changes in "*m".
The example is below.

Could you explain me why this occurs ?

Thanx again,
Morgan.
_______________ _______________ _______________ _______________ _____
int main(int argc, char *argv[])
{
int **p;

p = (int **)malloc(2*siz eof(int *));
if (p == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[0] = (int *)malloc(sizeof (int));
if (p[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[1] = (int *)malloc(2*size of(int));
if (p[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}

p[0][0] = 1; printf("%d\n",p[0][0]);
p[1][0] = 2; printf("%d\n",p[1][0]);
// Next line is normally executed here !!!!!!!!!!!!!!! !!!!
p[1][1] = 3; printf("%d\n",p[1][1]);

free(p);

test(&p);

printf("%d\n",p[0][0]);
printf("%d\n",p[1][0]);
printf("%d\n",p[1][1]);

free(p);

system("PAUSE") ;
return 0;
}

void test(int ***m)
{
*m = (int **)malloc(2*siz eof(int *));
if (*m == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[0] = (int *)malloc(sizeof (int));
if (*m[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[1] = (int *)malloc(2*size of(int));
if (*m[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}

*m[0][0] = 1;
*m[1][0] = 2;
// Next line causes an access violation here !!!!!!!!!!!!!!! !!!!
*m[1][1] = 3;
// For the exactness it seems to me that the access violation
// arises after the last line, coming back to main... :-( !?
}
_______________ _____________HE LP !!!____________ _______________ __


First decide if your writing C code or C++ code.

If your writing C++ code try something like this:

#include <iostream>

/* function declaration & definition */
void foo(int** p){
std::cout<< "p[0][0] = " << p[0][0] << '\n' ;
std::cout<< "p[1][1] = " << p[1][1] << '\n' ;
p[0][1] = 21;
p[1][0] = 31;
}
/* simply prints two values and alters two */
int main(){
/** establish some values for dimensions **/
unsigned int DIM1 =2;
unsigned int DIM2 =2;

/** create array **/
int** ArrayIdent = new int*[DIM1];
for(unsigned int i=0; i<DIM1; ++i)
ArrayIdent[i] = new int[DIM2];

/** assign some values **/
ArrayIdent[0][0] = 1;
ArrayIdent[0][1] = 2;
ArrayIdent[1][0] = 3;
ArrayIdent[1][1] = 4;

/** pass to function **/
foo(ArrayIdent) ;

/** print a couple of values **/
std::cout<< "ArrayIdent[0][1] = " << ArrayIdent[0][1] << '\n' ;
std::cout<< "ArrayIdent[1][0] = " << ArrayIdent[1][0] << '\n' ;

/** free memory **/
for(unsigned int i=0; i<DIM1; ++i)
delete [] ArrayIdent[i];
delete [] ArrayIdent;

return 0;
}
I know it's logical to think of an extra level of indirection to pass the
address of a pointer but you don't need it here, simply pass the array
identifier by value(as this is the address* of the array).

*virtual address or whatever type of addressing your system uses. Should be
fine in most cases but I believe there are some systems out there which use
varaible sized pointers and caution should be exercised in this case when
passing any pointer. (i.e passing 32-bit pointers to 64-bit routines and
vice versa).
HTH.

Nov 14 '05 #3

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

Similar topics

9
2689
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as open, even though it's at least a year old and Guido doesn't seem very hot on the idea). I'm not too sure of its ideas on raising exceptions in generators from outside (although it looks like it might be convenient in some cases), but being able...
2
29599
by: aliensite | last post by:
I am trying to pass arrays of various lengths to a function. However the values are read as a string. What is the easiest way to pass the values? <script type="text/javascript"> <!-- function foo() { var args = "start "; for( var i=0 ; i < arguments.length; i++ ) { if(i==0)
58
10181
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
1
2538
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could show me a example of how to do this. I was told to pass the arrays from function to function, but i do not know how .... Thanxs if you can help. My code (First two functions only - there's more):
2
1566
by: windandwaves | last post by:
Hi Folk I have the following function var a = new Array(4); a = new Array(45); //status (on or off) a = new Array(45); //name of the region a = new Array(45); //on mouse over a = new Array(45); //on mouse out a = false;
4
6382
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically allocated a set of pointer to pointers as my multi-dimensional arrays. Here is my code (i have omitted checking calloc's return value to make this shorter): int **filter; filter = calloc( ylength, sizeof(int*) ); for( i = 0 ; i < ylength...
39
19647
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1) What's the difference between these 3 statements: (i) memcpy(&b, &KoefD, n); // this works somewhere in my code
4
2506
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
2
4436
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
0
10325
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...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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
8972
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
7499
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
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.