malloc
Moderator: Board moderators
-
- Experienced poster
- Posts: 169
- Joined: Wed Oct 31, 2001 2:00 am
- Location: Singapore
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
Hi
For a 3 by 4 array (i.e sthing like array[3][4])
@START_OF_SOURCE_CODE
#define COLS 4
#define ROWS 3
int **array;
int i,j;
/* Allocates 3 int pointers */
/* calloc initializes the memory to 0x0, while malloc only allocates */
array = (int**)calloc(ROWS, sizeof(int*));
/* I'm not sure about the order of calloc parameters, if it is (nb_elements, size) or the contrary, but it doesn't matter at all */
/* Then for each row */
for(i=0; i<ROWS;i++)
/* Allocates COLS integers */
array = (int*)calloc(COLS,sizeof(int));
@END_OF_SOURCE_CODE
Here it is. Hope it will help
Good bye
For a 3 by 4 array (i.e sthing like array[3][4])
@START_OF_SOURCE_CODE
#define COLS 4
#define ROWS 3
int **array;
int i,j;
/* Allocates 3 int pointers */
/* calloc initializes the memory to 0x0, while malloc only allocates */
array = (int**)calloc(ROWS, sizeof(int*));
/* I'm not sure about the order of calloc parameters, if it is (nb_elements, size) or the contrary, but it doesn't matter at all */
/* Then for each row */
for(i=0; i<ROWS;i++)
/* Allocates COLS integers */
array = (int*)calloc(COLS,sizeof(int));
@END_OF_SOURCE_CODE
Here it is. Hope it will help

Good bye
As you may (or might not) know, C lets you index a pointer with array semantics as if it were an array, and vice-versa (using pointer arithmetics with arrays). This is because in C the typical array indexing with "[]" is some kind of sintactic sugar for pointer arith. So you may do the following if you want to malloc() a 2D array of WIDTH*HEIGHT items:
[c]
#define WIDTH 10
#define HEIGHT 20
...
int *array = (int*) malloc(WIDTH*HEIGHT*sizeof(int));
[/c]
and then you can do array-like indexing with the brackets:
[c]
array[2][3] = 34;
[/c]
This is the usual way for doing such things in C.
[c]
#define WIDTH 10
#define HEIGHT 20
...
int *array = (int*) malloc(WIDTH*HEIGHT*sizeof(int));
[/c]
and then you can do array-like indexing with the brackets:
[c]
array[2][3] = 34;
[/c]
This is the usual way for doing such things in C.
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
malloc
For char string,
[cpp]
char **TmpName = (char**)malloc(10);
for (int i=0; i<10; i++)
TmpName = (char*)malloc(80);
//endfor
[/cpp]
-novice
[cpp]
char **TmpName = (char**)malloc(10);
for (int i=0; i<10; i++)
TmpName = (char*)malloc(80);
//endfor
[/cpp]
-novice

-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
But this code should works in any compiler
Best regards
DM
PS. Moebius forgot about one asteriks
Code: Select all
#include <stdlib.h>
int main(void)
{
int **a = (int **)calloc(10*10,sizeof(int));
a[0][2] = 3;
return 0;
}
DM
PS. Moebius forgot about one asteriks

If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
ok, no compile errors this time, but I get runtime errors from the following program when trying to printf from the array. Also, I could not scanf into the array.
[c]#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int **a = (int **)calloc(10*10,sizeof(int));
a[0][2] = 3;
printf("%d \n",a[0][2]);
return 0;
}[/c]
[c]#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int **a = (int **)calloc(10*10,sizeof(int));
a[0][2] = 3;
printf("%d \n",a[0][2]);
return 0;
}[/c]
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
Indeed, it does not work. Here's why :
you're allocating 100 consecutive pointers to int, so
a[2] is a pointer to an int. But it doesn't point to any memory part ! It's == 0 (because calloc zeroes everything it allocates, to permit you to see that there's something wrong).
Here's how I'd do what you wanna do (wich is kinda alike to what I've posted before) :
[c]#include <stdio.h>
#include <stdlib.h>
#define COLS 4
#define ROWS 3
int main(){
int **array;
int i;
/* Allocates 3 int pointers */
/* calloc initializes the memory to 0x0, while malloc only allocates */
array = (int**)calloc(ROWS, sizeof(int*));
/* I'm not sure about the order of calloc parameters, if it is (nb_elements, size) or the contrary, but it doesn't matter at all */
/* Then for each row */
for(i=0; i<ROWS;i++)
/* Allocates COLS integers */
array = (int*)calloc(COLS,sizeof(int));
array[0][2] = 3;
printf("%d \n", array[0][2]);
return 0;
}
[/c]
Try that, it compilers perfectly on my computer, witch -Wall and -ansi.
you're allocating 100 consecutive pointers to int, so
a[2] is a pointer to an int. But it doesn't point to any memory part ! It's == 0 (because calloc zeroes everything it allocates, to permit you to see that there's something wrong).
Here's how I'd do what you wanna do (wich is kinda alike to what I've posted before) :
[c]#include <stdio.h>
#include <stdlib.h>
#define COLS 4
#define ROWS 3
int main(){
int **array;
int i;
/* Allocates 3 int pointers */
/* calloc initializes the memory to 0x0, while malloc only allocates */
array = (int**)calloc(ROWS, sizeof(int*));
/* I'm not sure about the order of calloc parameters, if it is (nb_elements, size) or the contrary, but it doesn't matter at all */
/* Then for each row */
for(i=0; i<ROWS;i++)
/* Allocates COLS integers */
array = (int*)calloc(COLS,sizeof(int));
array[0][2] = 3;
printf("%d \n", array[0][2]);
return 0;
}
[/c]
Try that, it compilers perfectly on my computer, witch -Wall and -ansi.
sorry for the n00bish question, but if I use the following code
[c]int **array = (int**)calloc(ROWS, sizeof(int*));
for(i=0; i<ROWS;i++)
array = (int*)calloc(COLS,sizeof(int));[/c]
and I wanted to iterate through this array[][]....which set of []'s would be associated with ROWS, and which with COLS. I tried to find out, setting ROWS to 1 and COLS to 600 and tried assigning and accessing array[0][599], and then array[599][0] and it let me do both even though one of these should be illegal. Not the best way to test it I know.
~Eric
[c]int **array = (int**)calloc(ROWS, sizeof(int*));
for(i=0; i<ROWS;i++)
array = (int*)calloc(COLS,sizeof(int));[/c]
and I wanted to iterate through this array[][]....which set of []'s would be associated with ROWS, and which with COLS. I tried to find out, setting ROWS to 1 and COLS to 600 and tried assigning and accessing array[0][599], and then array[599][0] and it let me do both even though one of these should be illegal. Not the best way to test it I know.
~Eric
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Idea is simple - indexes are in such way, in which you are created this array. So if code look like
int **a = (int **)malloc(ROWS,sizeof(int *));
than first index is for rows ....
so you must always use this array like this:
a[number_of_row][number_of_column]
Best regards
DM
int **a = (int **)malloc(ROWS,sizeof(int *));
than first index is for rows ....
so you must always use this array like this:
a[number_of_row][number_of_column]
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
-
- Experienced poster
- Posts: 145
- Joined: Sat Feb 23, 2002 2:00 am
- Location: Paris, France
- Contact:
FunkyELF wrote:sorry for the n00bish question, but if I use the following code
[c]int **array = (int**)calloc(ROWS, sizeof(int*));
for(i=0; i<ROWS;i++)
array = (int*)calloc(COLS,sizeof(int));[/c]
and I wanted to iterate through this array[][]....which set of []'s would be associated with ROWS, and which with COLS. I tried to find out, setting ROWS to 1 and COLS to 600 and tried assigning and accessing array[0][599], and then array[599][0] and it let me do both even though one of these should be illegal. Not the best way to test it I know.
~Eric
Hi !
To check that sort of stuffs, you can use what we call "malloc debuggers" wich enforce these checks by adding protected memory pages behind each page you allocate. The simplest one is Electric Fence (libefence), but far not the most complete. A really good one (works with C, C++, and almost any language) is valgrind, used to debug KDE3, but is far more complicated, you don't need it. I started debugging thoses tricks with electric fence, and it was exactly what i needed.
Free...
Hi, so many ways to alloc 2d array. But how to free it?
[cpp]
#include <stdio.h>
#include <stdlib.h>
void main()
{
char **x = (char**)malloc(5);
for (int i=0; i<5; i++)
x = (char*)malloc(20);
for (i = 0; i < 5; i++)
sprintf(x, "string#%d", i + 1);
for (i = 0; i < 5; i++)
printf("%s\n", x);
for (i = 0; i < 5; i++)
free(x);
// Is this line necessary?
// My program crash here!
free(x);
}
[/cpp]
-novice
[cpp]
#include <stdio.h>
#include <stdlib.h>
void main()
{
char **x = (char**)malloc(5);
for (int i=0; i<5; i++)
x = (char*)malloc(20);
for (i = 0; i < 5; i++)
sprintf(x, "string#%d", i + 1);
for (i = 0; i < 5; i++)
printf("%s\n", x);
for (i = 0; i < 5; i++)
free(x);
// Is this line necessary?
// My program crash here!
free(x);
}
[/cpp]
-novice
