On Fri, 6 Feb 2004, Morgan wrote:
[color=blue]
> 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>[/color]
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 <iostream> will #include <stdio.h> as well.
[color=blue]
> 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[i]);[/color]
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
[color=blue]
> test2(a);
> for(int i=0;i<3;i++) printf("%d ",a[i]);[/color]
Same thing again.
[color=blue]
> system("PAUSE");
> return 0;
> }[/color]
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.
[color=blue]
> //
> // 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[i]=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[i]=2*i;
> a1 = a2;
> }
> _______________________________________________
> EXAMPLE END.[/color]
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.
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to
vice.president@whitehouse.gov