malloc for multidimensional array !!please help

  • Follow


I am having big problem retrieving data assgined to dynamic 2-d array .

i am calculating and saving data in to dynamic 2-d array.
but when i retrieve them, it doesnt give correct values.

you can run following code in C compiler and see the difference

follwoing is my code, please have a look and please reply to
sanwa001@fiu.edu


TIA



#include <stdio.h>
#include <memory.h>

main()

{


  double *a;

  int i, j, lx=5;

  a= (double *)malloc(lx*2);
for (i=0; i<lx; i++)
{
    for(j=0;j<lx; j++)
    {
      *(a+i+j)= i+j*0.3;
      printf("a[%d][%d]=%f\n",i,j,*(a+i+j));

    }

}


printf("copy data \n\n\n");

for (i=0; i<lx; i++)
{
    for(j=0;j<lx; j++)
    {
      printf("a[%d][%d]=%f\n",i,j,*(a+i+j));

    }

}

free(a);
}

0
Reply shadab.anwar (1) 12/4/2006 7:49:20 AM

shadab said:

> I am having big problem retrieving data assgined to dynamic 2-d array .
> 
> i am calculating and saving data in to dynamic 2-d array.
> but when i retrieve them, it doesnt give correct values.

foo.c:6: warning: return-type defaults to `int'
foo.c:6: warning: function declaration isn't a prototype
foo.c: In function `main':
foo.c:13: warning: implicit declaration of function `malloc'
foo.c:13: warning: cast does not match function type
foo.c:38: warning: implicit declaration of function `free'
foo.c:39: warning: control reaches end of non-void function

Fix these problems first, then post again if you're still having trouble.

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
0
Reply rjh (10789) 12/4/2006 7:58:06 AM


shadab wrote:
> I am having big problem retrieving data assgined to dynamic 2-d array .
>
> i am calculating and saving data in to dynamic 2-d array.
> but when i retrieve them, it doesnt give correct values.
>
> you can run following code in C compiler and see the difference
>
> follwoing is my code, please have a look and please reply to
> sanwa001@fiu.edu
<snip>
> #include <stdio.h>
> #include <memory.h>

Replace above header with stdlib.h

> main()

Replace above with int main(void)

> {
>   double *a;
>   int i, j, lx=5;
>
>   a= (double *)malloc(lx*2);

Do: a = malloc(lx * 2);
Check for success.

> for (i=0; i<lx; i++)
> {
>     for(j=0;j<lx; j++)
>     {
>       *(a+i+j)= i+j*0.3;

Completely wrong. Do:
    a[i][j] = i + j * 0.3;

>       printf("a[%d][%d]=%f\n",i,j,*(a+i+j));

Again:
    printf("a[%d][%d] = %f\n", i, j, a[i][j]);

>     }
> }
> printf("copy data \n\n\n");
> for (i=0; i<lx; i++)
> {
>     for(j=0;j<lx; j++)
>     {
>       printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
> 
>     }
> }

What's the point of repeating the code?

> free(a);
> }

0
Reply santosh.k83 (3969) 12/4/2006 8:10:41 AM

santosh wrote:
> shadab wrote:
> > I am having big problem retrieving data assgined to dynamic 2-d array .
> >
> > i am calculating and saving data in to dynamic 2-d array.
> > but when i retrieve them, it doesnt give correct values.
> >
> > you can run following code in C compiler and see the difference
> >
> > follwoing is my code, please have a look and please reply to
> > sanwa001@fiu.edu
> <snip>
> > #include <stdio.h>
> > #include <memory.h>
>
> Replace above header with stdlib.h
>
> > main()
>
> Replace above with int main(void)
>
> > {
> >   double *a;
> >   int i, j, lx=5;
> >
> >   a= (double *)malloc(lx*2);
>
> Do: a = malloc(lx * 2);
> Check for success.

Whoops. This should be:
a = malloc(lx * sizeof *a);

<snip>

0
Reply santosh.k83 (3969) 12/4/2006 8:14:24 AM

On 4 Dec 2006 00:10:41 -0800, "santosh" <santosh.k83@gmail.com> wrote:

>shadab wrote:
>> I am having big problem retrieving data assgined to dynamic 2-d array .
>>
>> i am calculating and saving data in to dynamic 2-d array.
>> but when i retrieve them, it doesnt give correct values.
>>
>> you can run following code in C compiler and see the difference
>>
>> follwoing is my code, please have a look and please reply to
>> sanwa001@fiu.edu
><snip>
>> #include <stdio.h>
>> #include <memory.h>
>
>Replace above header with stdlib.h
>
>> main()
>
>Replace above with int main(void)
>
>> {
>>   double *a;
>>   int i, j, lx=5;
>>
>>   a= (double *)malloc(lx*2);
>
>Do: a = malloc(lx * 2);
>Check for success.
>
>> for (i=0; i<lx; i++)
>> {
>>     for(j=0;j<lx; j++)
>>     {
>>       *(a+i+j)= i+j*0.3;
>
>Completely wrong. Do:
>    a[i][j] = i + j * 0.3;

Completely wrong. See FAQ 6.16.

http://c-faq.com/aryptr/dynmuldimary.html

-- 
jay
0
Reply jaysome1 (129) 12/4/2006 8:30:34 AM

jaysome wrote:
> On 4 Dec 2006 00:10:41 -0800, "santosh" <santosh.k83@gmail.com> wrote:
> >shadab wrote:
[...]
> Completely wrong. See FAQ 6.16.
>
> http://c-faq.com/aryptr/dynmuldimary.html

Thanks. Sorry everyone. Obviously, I shouldn't answer right after
waking up in future.

0
Reply santosh.k83 (3969) 12/4/2006 10:47:59 AM

shadab wrote:
> I am having big problem retrieving data assgined to dynamic 2-d array .
> 
> i am calculating and saving data in to dynamic 2-d array.
> but when i retrieve them, it doesnt give correct values.
> 
> #include <stdio.h>
> #include <memory.h>

No such thing, should be <stdlib.h>

> main()
> 
> {
> 
> 
>   double *a;
> 
>   int i, j, lx=5;
> 
>   a= (double *)malloc(lx*2);

You want lx rows and lx columns, that's lx * lx values. Not 2 * lx. You 
also need to multiply by the size of the element, since malloc expects a 
number in bytes.

   a = malloc(lx * lx * sizeof (double));
or
   a = malloc(lx * lx * sizeof *a);

> for (i=0; i<lx; i++)
> {
>     for(j=0;j<lx; j++)
>     {
>       *(a+i+j)= i+j*0.3;

You must multiply the row number by the width, so it will skip over that 
many elements to keep each row separate.

Here the calculation is wrong again, should be
   *(a + i*ix + j)
or
   a[i*ix + j]
These two are equivalent.

>       printf("a[%d][%d]=%f\n",i,j,*(a+i+j));

Same here.

>     }
> 
> }
> 
> 
> printf("copy data \n\n\n");
> 
> for (i=0; i<lx; i++)
> {
>     for(j=0;j<lx; j++)
>     {
>       printf("a[%d][%d]=%f\n",i,j,*(a+i+j));

Same here.

>     }
> 
> }
> 
> free(a);

Add:
   return 0;

> }

-- 
Simon.
0
Reply news195 (400) 12/4/2006 11:02:34 AM

For 2D apply these rules:

Size = height * width

Offset = width * y + x

Bye,
  Skybuck. 


0
Reply spam43 (193) 12/4/2006 12:17:16 PM

santosh wrote:
> jaysome wrote:
>> "santosh" <santosh.k83@gmail.com> wrote:
>> >shadab wrote:
> [...]
>> Completely wrong. See FAQ 6.16.
>>
>> http://c-faq.com/aryptr/dynmuldimary.html
> 
> Thanks. Sorry everyone. Obviously, I shouldn't answer right after
> waking up in future.

The next time you do that please note stock prices, winners at the
horse races, etc. and report back here. :=)

-- 
Chuck F (cbfalconer at maineline dot net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>


0
Reply cbfalconer (19183) 12/4/2006 12:27:41 PM

shadab wrote:
> 
> I am having big problem retrieving data assgined to dynamic 2-d array .
> 
> i am calculating and saving data in to dynamic 2-d array.
> but when i retrieve them, it doesnt give correct values.
> 
> you can run following code in C compiler and see the difference
> 
> follwoing is my code, please have a look and please reply to
> sanwa001@fiu.edu
> 
> TIA
> 
> #include <stdio.h>
> #include <memory.h>
> 
> main()
> 
> {
> 
>   double *a;
> 
>   int i, j, lx=5;
> 
>   a= (double *)malloc(lx*2);
> for (i=0; i<lx; i++)
> {
>     for(j=0;j<lx; j++)
>     {
>       *(a+i+j)= i+j*0.3;
>       printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
> 
>     }
> 
> }
> 
> printf("copy data \n\n\n");
> 
> for (i=0; i<lx; i++)
> {
>     for(j=0;j<lx; j++)
>     {
>       printf("a[%d][%d]=%f\n",i,j,*(a+i+j));
> 
>     }
> 
> }
> 
> free(a);
> }

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    double *a;
    unsigned i, j;
    const unsigned lx = 5;
    
    a = malloc(lx * lx * sizeof *a);
    if (a == NULL) {
        puts("a == NULL");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i != lx; ++i) {
        for(j = 0; j != lx; ++j) {
            a[i * lx + j] = i + j * 0.3;
        }
    }
    for (i = 0; i != lx; ++i) {
        for(j = 0; j != lx; ++j) {
            printf("a[%u][%u] = %f\n", i, j, a[i * lx + j]);
        }
    }    
    free(a);    
    return 0;
}

/* END new.c */


-- 
pete
0
Reply pfiland (6614) 12/4/2006 12:50:46 PM

Simon Biber wrote:
> Here the calculation is wrong again, should be
>    *(a + i*ix + j)
> or
>    a[i*ix + j]
> These two are equivalent.

Better

(i*ix+j)[a]

/me ducks

hehehehe

Tom

0
Reply tomstdenis (355) 12/4/2006 1:50:45 PM

10 Replies
62 Views

(page loaded in 0.128 seconds)

Similiar Articles:













7/24/2012 5:22:23 PM


Reply: