LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C: malloc arrays- unexpected output (https://www.linuxquestions.org/questions/programming-9/c-malloc-arrays-unexpected-output-428015/)

kpachopoulos 03-24-2006 09:02 AM

C: malloc arrays- unexpected output
 
Hi,
i've written this:
Code:

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

#define CHUNKS 10

int *allocate_int_array(int chunks_num)
{
        return (int *)malloc(chunks_num*sizeof(int));
}



main()
{
        int *exp_array=allocate_int_array(CHUNKS);

       
       
        int i;
       
        for (i=0; i<10; i++)
        {               
                *exp_array=i;
                exp_array+=sizeof(int *);
        }
       
        int j;
        for (j=10;j>0;j--)
        {
                printf("%i \n",*exp_array);
                exp_array-=sizeof(int *);
        }
       
        free(*exp_array);
}

When i execute it i get:
Code:

$ ./c1
0
9
8
7
6
5
4
663604
2
1

Why don't i get this? What is this 663604? (looks like an address)
Code:

9
8
7
6
5
4
3
2
1
0


bc8o8 03-24-2006 10:13 AM

The lines
Code:

===snip===
  exp_array+=sizeof(int *);
===snip===
  exp_array-=sizeof(int *);
===snip===

are incrementing your array location by sizeof(int *) which (on my system) is 4. So you are skipping valid locations and reading/writing other areas in memory. What you want to do instead is actually just increment the pointer -- pointers are smart enough to know their size, so it will increment appropriately.

For example the following should do what you want
Code:

for (i=0; i<10; i++)
{               
        *exp_array=i;
        exp_array++;
}

int j;
for (j=10;j>0;j--)
{
        printf("%i \n",*exp_array);
        exp_array--;
}

although, instead of moving your exp_array pointer around, a better approach would be to just use exp_array[ i ] = i; and either printf("%i \n",exp_array[ i ]); or printf("%i \n",exp_array+i); (which are identical)

addy86 03-24-2006 04:49 PM

Quote:

Originally Posted by bc8o8
Code:

int j;
for (j=10;j>0;j--)
{
        printf("%i \n",*exp_array);
        exp_array--;
}


This will still not work; instead:
Code:

{
        exp_array--;
        printf("%i \n",*exp_array);
}


freegianghu 03-24-2006 09:30 PM

Quote:

Originally Posted by nocturna_gr
Hi,
i've written this:
Code:

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

#define CHUNKS 10

int *allocate_int_array(int chunks_num)
{
        return (int *)malloc(chunks_num*sizeof(int));
}



main()
{
        int *exp_array=allocate_int_array(CHUNKS);

       
       
        int i;
       
        for (i=0; i<10; i++)
        {               
                *exp_array=i;
                exp_array+=sizeof(int *); // exp_array++;
        }
       
        int j;
        for (j=10;j>0;j--)
        {
                printf("%i \n",*exp_array);
                exp_array-=sizeof(int *); // exp_array--;
        }
       
        free(*exp_array); // free(exp_array);
}

................

Cheers,
GH.


All times are GMT -5. The time now is 08:21 PM.