How to pass by reference a dinamic array to a C routine

M

Morgan

I have read much posts on the argument but no one
clearly says if this operation is possible or not.

Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.

So I can allocate memory only after reading the
first array, but then I cannot bring out of my
reader routine the arrays created.

Maybe I was not so clear, all my problem is
represented by the following example.
Why neither test1 nor test2 routine can bring
out the pointer to the beginning of the array?
Printf calls never print the right values :-((

Maybe I am not so able with C...

....tanx in advance,

Morgan.

EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>

using namespace std;

void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
for(int i=0;i<3;i++) printf("%d ",a);

test2(a);
for(int i=0;i<3;i++) printf("%d ",a);

system("PAUSE");
return 0;
}

//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
for(int i=0;i<3;i++) a1=2*i;
}

//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;

a2 = new int[3];
for(int i=0;i<3;i++) a2=2*i;
a1 = a2;
}
_______________________________________________
EXAMPLE END.
 
D

Darrell Grainger

I have read much posts on the argument but no one
clearly says if this operation is possible or not.

Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.

So I can allocate memory only after reading the
first array, but then I cannot bring out of my
reader routine the arrays created.

Maybe I was not so clear, all my problem is
represented by the following example.
Why neither test1 nor test2 routine can bring
out the pointer to the beginning of the array?
Printf calls never print the right values :-((

Maybe I am not so able with C...

...tanx in advance,

Morgan.

EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>

There is no <iostream> in C language and you are missing the <stdio.h>
header for your printf. I'm not even sure if the C++ standard indicates
that using said:
using namespace std;

void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
for(int i=0;i<3;i++) printf("%d ",a);


Since this is C language I can immediately say that this is undefined
behaviour. The three lines above do the following:

1) Create an UNINITIALIZED pointer to int called a
2) You pass some random value to test1() which has absolutely
no way of changing the value of a
3) You proceed to dereference a and still have not initialized it
test2(a);
for(int i=0;i<3;i++) printf("%d ",a);


Same thing again.
system("PAUSE");
return 0;
}

If you want to initialize a with the results of a malloc in test1 you will
have to pass the address of a to test1. The function test1 will then
receive a pointer to a pointer to int.
//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];
for(int i=0;i<3;i++) a1=2*i;
}

//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;

a2 = new int[3];
for(int i=0;i<3;i++) a2=2*i;
a1 = a2;
}
_______________________________________________
EXAMPLE END.


Just a style comment: This code is at best C++ code. If you want to use
things like <iostream> and new[]/delete[] then fully embrace object
oriented programming and go to the comp.lang.c++ newsgroup. If you want to
learn C programming then quit mixing C and C++ code.

When I look at our code it is like someone who writes stories but they
write them in French and Italian, changing languages mid-paragraph.
 
D

Default User

Morgan said:
#include <iostream>
#include <stdlib.h>

using namespace std;


This is C++ code, and hence off-topic in comp.lang.c. The group you want
(in case you need another one besides a.c.l.l.c-c++) is comp.lang.c++.
Those whose answer in the first group should remove the crosspost to
c.l.c.



Brian Rodenborn
 
R

Richard Heathfield

Morgan said:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.

Despite your subject title, your code is C++ code, not C code. It is,
therefore, off-topic in comp.lang.c++.

Followups set to alt.comp.lang.learn.c-c++ only.

<snip>
 
P

Peter Pichler

[I am reading this on comp.lang.c, please bear it in mind.]


....something about dynamically allocating arrays according to numbers read
from a file, providing the following code:
EXAMPLE BEGIN
_______________________________________________
#include <iostream>

Eh? I think you misspeled #include said:
#include <stdlib.h>

using namespace std;
Eh?

void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);

Undefined behaviour. Using an uninitialized variable.
for(int i=0;i<3;i++) printf("%d ",a);

test2(a);


As above.
for(int i=0;i<3;i++) printf("%d ",a);

system("PAUSE");


Working in DOS, right? ;-)
return 0;
}

//
// First test routine to pass the array in "a"
///////////////////////////////////////////////
void test1(int *a1)
{
a1 = new int[3];

Eh? I think you misspelled a1 = malloc(3 * sizeof *a1);

But even that would only help you leak memory like a sieve.

Let me explain. Your function allocates an array and stores the pointer in
a1 (by the way, you are not checking that the allocation succeeded). Because
C <off-topic>and C++</off-topic> passes parameters by value, a1 in your
function is a copy of a in main. Whatever you do to a1 does not affect the
value of a.

There are two ways around it. The ugly way is to pass a pointer to a
variable that you want to change and dereference the pointer in your test1.
The prettier way is to make test1 return the value, rather than alter it
through th eparameter.
for(int i=0;i<3;i++) a1=2*i;
}

//
// Second test routine to pass the array in "a"
///////////////////////////////////////////////
void test2(int *a1)
{
int *a2;

a2 = new int[3];
for(int i=0;i<3;i++) a2=2*i;
a1 = a2;
}


Same comments as above, an additional local variable does not change a
thing.
 
C

CBFalconer

Morgan said:
.... snip ...

Maybe I am not so able with C...

...tanx in advance,

Morgan.

EXAMPLE BEGIN
_______________________________________________
#include <iostream>
#include <stdlib.h>

using namespace std;

.... snip ...

This is not C, so cannot be compiled with C. F'ups set.
 
M

Martin Ambuhl

Morgan said:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.

For those not noticing, or for whom the subject line is too long for your
client's window or any of several other reasons it might not be immediately
obvious, the subject line is:
Subject: How to pass by reference a dinamic array to a C routine
Since C does not have "pass by reference," and any one having "read much
posts" would know this, I can't see why this post showed up in comp.lang.c.

Continuing anyway ...

#include <iostream>
^^^^^^^^^^
This is not a standard C header.
#include <stdlib.h>

using namespace std;
^^^^^^^^^^^^^^^^^^^^^
This line is nothing but a syntax error in C.
void test1(int *);
void test2(int *);

//
// Main routine
///////////////////////////////////////////////
int main(int argc, char *argv[])
{
int *a;

test1(a);
for(int i=0;i<3;i++) printf("%d ",a);

^^^^^^
printf has its prototype in <stdio.h>. You did not include it, nor did you
test2(a);
for(int i=0;i<3;i++) printf("%d ",a);
system("PAUSE");
return 0;
}

// First test routine to pass the array in "a"
void test1(int *a1)
{
a1 = new int[3];
^^^^^^^^^^
syntax error. Why did you post to comp.lang.c, again?

[remaining non-C crap snipped]
 
C

CBFalconer

Martin said:
Morgan said:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.
.... snip ...
Since C does not have "pass by reference," and any one having
"read much posts" would know this, I can't see why this post
showed up in comp.lang.c.

Continuing anyway ...
#include <iostream>
^^^^^^^^^^
This is not a standard C header.
.... snip ...
a1 = new int[3];
^^^^^^^^^^
syntax error. Why did you post to comp.lang.c, again?

Why did you fail to set follow-ups to eliminate c.l.c?
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top