malloc

Write here if you have problems with your C source code

Moderator: Board moderators

junjieliang
Experienced poster
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore

Post by junjieliang » Mon Feb 11, 2002 11:23 am

Can someone pls tell me how to malloc() a 2-d array?

Thanks.

idler
New poster
Posts: 9
Joined: Thu Feb 21, 2002 2:00 am
Location: China

Post by idler » Fri Feb 22, 2002 7:33 am

Step 1:
#include <stdlib.h>
#include <malloc.h>

Step 2:
int *array_name[...][...]

Step 3:
array_name = (int *)malloc(sizeof(array_name));

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Sat Feb 23, 2002 11:35 pm

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 :smile:
Good bye

Moebius
New poster
Posts: 2
Joined: Sat Nov 02, 2002 1:47 am
Location: Galicia, Spain
Contact:

Post by Moebius » Sat Nov 02, 2002 2:53 am

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.

Stefan Pochmann
A great helper
Posts: 284
Joined: Thu Feb 28, 2002 2:00 am
Location: Germany
Contact:

Post by Stefan Pochmann » Sun Nov 03, 2002 2:32 am

I'd be *very* surprised if that works. Yep, my compiler seconds this opinion. He says "no way I'll compile that for you".

Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

malloc

Post by Fresh » Sun Nov 03, 2002 5:08 pm

For char string,

[cpp]

char **TmpName = (char**)malloc(10);

for (int i=0; i<10; i++)
TmpName = (char*)malloc(80);
//endfor

[/cpp]


-novice :D

dev_mania
New poster
Posts: 2
Joined: Wed Nov 13, 2002 2:43 am

Post by dev_mania » Fri Nov 15, 2002 7:18 am

[c]
#define ROW 5
#define COL 5
....

int (*array)[COL];

array = (int (*)[COL])malloc(sizeof(int) * ROW * COL);

/* Using pointer variable array like general 2-d array
--ex) array[0][0] */
[/c]

FunkyELF
New poster
Posts: 3
Joined: Wed Jul 23, 2003 7:05 pm
Location: Orlando, FL
Contact:

Post by FunkyELF » Wed Jul 23, 2003 7:10 pm

Uhm, I'm trying to do the same thing and malloc a 2-d array. I've tried everything here and could not get it to work. If you're sure your method works, could you post the entire compileable code?

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Thu Jul 24, 2003 8:11 am

But this code should works in any compiler

Code: Select all

#include <stdlib.h>

int main(void)
{
	int **a = (int **)calloc(10*10,sizeof(int));

	a[0][2] = 3;
	return 0;
}
Best regards
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)

FunkyELF
New poster
Posts: 3
Joined: Wed Jul 23, 2003 7:05 pm
Location: Orlando, FL
Contact:

Post by FunkyELF » Thu Jul 24, 2003 9:42 am

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]

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Thu Jul 24, 2003 6:32 pm

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.

FunkyELF
New poster
Posts: 3
Joined: Wed Jul 23, 2003 7:05 pm
Location: Orlando, FL
Contact:

Post by FunkyELF » Fri Sep 05, 2003 5:06 am

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

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Fri Sep 05, 2003 8:04 am

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
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)

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Wed Sep 17, 2003 10:08 pm

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.

Fresh
New poster
Posts: 46
Joined: Mon Apr 15, 2002 10:42 am
Contact:

Free...

Post by Fresh » Thu Jan 08, 2004 4:26 am

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 :-?

Post Reply

Return to “C”