This is the first I have to allocate a 3D array somehow my imagination
isn't sufficient to clearly visualize the process so I've written the code
based on assumptions without actually understanding it.
It SEEMS to work but especially as far as mem. allocation is concerned that
doesn't mean a thing.
Could you please look at my code and tell me if it's correct or not?
int *** World;
void world_new(int x, int y, int z)
{
int i, j;
World = malloc(x * sizeof *World);
for (i = 0; i < x; i++) {
World[i] = malloc(y * sizeof **World);
for (j = 0; j < y; j++) {
World[i][j] = malloc(z * sizeof ***World);
}
}
}
(No need to point out the lack of malloc error checks. My project uses a
warper around malloc which handles those.)
copx 7 1395
copx said: This is the first I have to allocate a 3D array somehow my imagination isn't sufficient to clearly visualize the process so I've written the code based on assumptions without actually understanding it. It SEEMS to work but especially as far as mem. allocation is concerned that doesn't mean a thing. Could you please look at my code and tell me if it's correct or not?
int *** World;
I would get world_new to return int *** instead of nailing the function to
this file scope variable. That gives you much more flexibility. void world_new(int x, int y, int z) { int i, j;
Consider making x, y, z, i, and j size_t's rather than ints. World = malloc(x * sizeof *World);
You'll need to check that this succeeded before continuing. Don't forget to
#include <stdlib.h> for the malloc prototype. for (i = 0; i < x; i++) { World[i] = malloc(y * sizeof **World);
You'll need to check that this succeeded before continuing.
I would suggest that you stick to the pattern
P = malloc(n * sizeof *P)
In this case, that would mean:
World[i] = malloc(y * sizeof *World[i]);
That way, you don't have to count stars. for (j = 0; j < y; j++) { World[i][j] = malloc(z * sizeof ***World);
You'll need to check that this succeeded before continuing.
I would suggest that you stick to the pattern
P = malloc(n * sizeof *P)
In this case, that would mean:
World[i][j] = malloc(z * sizeof *World[i][j]);
That way, you don't have to count stars.
}
}
}
(No need to point out the lack of malloc error checks. My project uses a warper around malloc which handles those.)
Ah, you're a bit late. :-)
Anyway, it seems to me that you have the basic idea. Nice one.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005 http://www.cpax.org.uk
email: rjh at above domain
"Richard Heathfield" <in*****@invalid.invalid> schrieb im Newsbeitrag
news:dg**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
[snip] Anyway, it seems to me that you have the basic idea. Nice one.
Thanks.
copx
copx wrote: This is the first I have to allocate a 3D array somehow my imagination isn't sufficient to clearly visualize the process so I've written the code based on assumptions without actually understanding it. It SEEMS to work but especially as far as mem. allocation is concerned that doesn't mean a thing. Could you please look at my code and tell me if it's correct or not?
int *** World;
void world_new(int x, int y, int z) { int i, j;
World = malloc(x * sizeof *World);
for (i = 0; i < x; i++) { World[i] = malloc(y * sizeof **World);
for (j = 0; j < y; j++) { World[i][j] = malloc(z * sizeof ***World); }
}
}
(No need to point out the lack of malloc error checks. My project uses a warper around malloc which handles those.)
copx
I do it like this.Is that right ?
-------------------------------------------------------------------------------
int ***world;
int i ,j, k;
world = (int***)malloc(10*sizeof(int**));
for(i = 0; i < 10; i++)
world[i] = (int**)malloc(10*sizeof(int*));
for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
world[i][j] = (int*)malloc(10*sizeof(int));
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
for(k = 0; k < 10; k++)
world[i][j][k] = 0;
}
}
for(i = 0; i < 10; i++)
{
printf("\n");
for(j = 0; j < 10; j++)
{
printf("\n");
for(k = 0; k < 10; k++)
printf(" %d ",world[i][j][k]);
}
}
for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
free(world[i][j]);
for(i = 0; i < 10; i++)
free(world[i]);
free(world);
-------------------------------------------------------------------------------
copx wrote: This is the first [time that] I have [had] to allocate a 3D array. Somehow, my imagination isn't sufficient to clearly visualize the process so I've written the code based [up]on assumptions without actually understanding it. It SEEMS to work but, especially as far as memory allocation is concerned, that doesn't mean a thing. Could you please look at my code and tell me if it's correct or not?
int *** World;
void world_new(int x, int y, int z) {
World = malloc(x*sizeof(*World));
for (int i = 0; i < x; ++i) { World[i] = malloc(y*sizeof(**World));
for (int j = 0; j < y; ++j) { World[i][j] = malloc(z*sizeof(***World)); } } }
(No need to point out the lack of malloc error checks. My project uses a warper around malloc which handles those.)
cat main.c
#include <stdlib.h>
#include <stdio.h>
int World_fprintf(FILE* fp,
size_t x, size_t y, size_t z, int a[x][y][z]) {
int characters = 0;
for (size_t i = 0; i < x; ++i) {
for (size_t j = 0; j < y; ++j) {
for (size_t k = 0; k < z; ++k)
characters += fprintf(fp, " %d", a[i][j][k]);
fprintf(fp, "\n");
}
fprintf(fp, "\n");
}
return characters;
}
int main(int argc, char* argv[]) {
const
size_t x = 3, y = 4, z = 5;
int World[x][y][z];
for (size_t i = 0; i < x; ++i)
for (size_t j = 0; j < y; ++j)
for (size_t k = 0; k < z; ++k)
World[i][j][k] = 100*i + 10*j + k;
World_fprintf(stdout, x, y, z, World);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c ./main
0 1 2 3 4
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34
100 101 102 103 104
110 111 112 113 114
120 121 122 123 124
130 131 132 133 134
200 201 202 203 204
210 211 212 213 214
220 221 222 223 224
230 231 232 233 234
On 23 Sep 2005 02:06:49 -0700, "Tera" <so******@gmail.com> wrote: I do it like this.Is that right ? ------------------------------------------------------------------------------- int ***world; int i ,j, k; world = (int***)malloc(10*sizeof(int**));
Casting the return from malloc is hardly ever right and this is not
one of the exceptions.
Specifying the type as the operand of sizeof will cause maintenance
problems if the type of world ever changes. Using *world makes the
code self adjusting.
And of course you need to check the result of each call to malloc. for(i = 0; i < 10; i++) world[i] = (int**)malloc(10*sizeof(int*)); for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) world[i][j] = (int*)malloc(10*sizeof(int));
for(i = 0; i < 10; i++) { for(j = 0; j < 10; j++) { for(k = 0; k < 10; k++) world[i][j][k] = 0; } }
for(i = 0; i < 10; i++) { printf("\n"); for(j = 0; j < 10; j++) { printf("\n"); for(k = 0; k < 10; k++) printf(" %d ",world[i][j][k]);
Your output will be all scrunched together.
} }
for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) free(world[i][j]); for(i = 0; i < 10; i++) free(world[i]); free(world); -------------------------------------------------------------------------------
<<Remove the del for email>>
just for test
Barry Schwarz wrote: On 23 Sep 2005 02:06:49 -0700, "Tera" <so******@gmail.com> wrote:
I do it like this.Is that right ? ------------------------------------------------------------------------------- int ***world; int i ,j, k; world = (int***)malloc(10*sizeof(int**));
Casting the return from malloc is hardly ever right and this is not one of the exceptions.
Specifying the type as the operand of sizeof will cause maintenance problems if the type of world ever changes. Using *world makes the code self adjusting.
And of course you need to check the result of each call to malloc.
for(i = 0; i < 10; i++) world[i] = (int**)malloc(10*sizeof(int*)); for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) world[i][j] = (int*)malloc(10*sizeof(int));
for(i = 0; i < 10; i++) { for(j = 0; j < 10; j++) { for(k = 0; k < 10; k++) world[i][j][k] = 0; } }
for(i = 0; i < 10; i++) { printf("\n"); for(j = 0; j < 10; j++) { printf("\n"); for(k = 0; k < 10; k++) printf(" %d ",world[i][j][k]);
Your output will be all scrunched together.
} }
for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) free(world[i][j]); for(i = 0; i < 10; i++) free(world[i]); free(world); -------------------------------------------------------------------------------
<<Remove the del for email>>
On 24 Sep 2005 06:11:07 -0700, "Tera" <so******@gmail.com> wrote: just for test
snip 60 lines
Did you need to quote all that?
<<Remove the del for email>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: fivelitermustang |
last post by:
Actually, how would I go about allocating a four-dimensional dynamic
array?
I only know how to make two dimensional dynamic arrays:
double **v;
v = new double*;
for (int i=0; i<C; i++)
{
v =...
|
by: Erik S. Bartul |
last post by:
lets say i want to fill up a multidimentional array, but i wish to allocate
memory for it on the fly.
i assume i declare,
char **a;
but how do i allocate memory for the pointers, so i can...
|
by: fix |
last post by:
Hi all,
I am writing a program using some structs, it is not running and I
believe it is because there's some memory leak - the debugger tells me
that the code causes the problem is in the malloc...
|
by: junky_fellow |
last post by:
What is the correct way of dynamically allocating a 2d array ?
I am doing it the following way. Is this correct ?
#include <stdlib.h>
int main(void)
{
int (*arr)(3);
arr =...
|
by: Peter Oliphant |
last post by:
I'm using VS C++.NET 2005 Express in /clr mode. Say I have a PointF array:
array<PointF>^ point_array = gcnew array<PointF>(100) ;
Now I want to re-allocate point_array, and in doing so also...
|
by: Francine.Neary |
last post by:
I'm experimenting with a program that manipulates a large matrix, i.e.
a 2-deep array. I started by just allocating this in one go, but later
it occurred to me that, as there are still some very...
|
by: sumitmishra |
last post by:
HI FRIENDS
i have a problem . i am trying to allocate array inside the for loop .
Below is my code here int** character has been allocated inside the for loop and then i have used the delete...
|
by: Francois Grieu |
last post by:
Hello, I'm asking myself all kind of questions on allocating
an array of struct with proper alignment.
Is the following code oorrect ?
I'm most interested by the statement
t =...
|
by: Chris Saunders |
last post by:
Here is the declaration of a struct from WinIoCtl.h:
//
// Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION
//
typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {
//
|
by: charlie |
last post by:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |