Does this use dynamic two-dimension array correctly?

L

lovecreatesbea...

Do the following two functions use dynamic two-dimension array
correctly?

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

#define ROW 3
#define COL 5

int dyn2dary(void);
int dyn2dary_2(void);

int main(void)
{
dyn2dary();
printf("%c", '\n');
dyn2dary_2();
return 0;
}

int dyn2dary(void)
{
int r = 0, c = 0;
char *p = malloc(ROW * COL * sizeof *p);

if (!p) return -1;
for (r = 0; r < ROW; r++){
for (c = 0; c < COL; c++){
p[r * COL + c] = 'a' + r * COL + c;
printf("%c ", p[r * COL + c]);
}
printf("%c", '\n');
}
free(p);
return 0;
}

int dyn2dary_2(void)
{
int r = 0, c = 0;
char (*a)[COL];
char *p = malloc(ROW * COL * sizeof *p);

if (!p) return -1;
a = (char (*) [COL])p;
for (r = 0; r < ROW; r++){
for (c = 0; c < COL; c++){
a[r][c] = 'a' + r * COL + c;
printf("%c ", a[r][c]);
}
printf("%c", '\n');
}
free(a);
return 0;
}
 
M

Malcolm McLean

Do the following two functions use dynamic two-dimension array
correctly?

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

#define ROW 3
#define COL 5

int dyn2dary(void);
int dyn2dary_2(void);

int main(void)
{
dyn2dary();
printf("%c", '\n');
dyn2dary_2();
return 0;
}

int dyn2dary(void)
{
int r = 0, c = 0;
char *p = malloc(ROW * COL * sizeof *p);

if (!p) return -1;
for (r = 0; r < ROW; r++){
for (c = 0; c < COL; c++){
p[r * COL + c] = 'a' + r * COL + c;
printf("%c ", p[r * COL + c]);
}
printf("%c", '\n');
}
free(p);
return 0;
}

int dyn2dary_2(void)
{
int r = 0, c = 0;
char (*a)[COL];
char *p = malloc(ROW * COL * sizeof *p);

if (!p) return -1;
a = (char (*) [COL])p;
for (r = 0; r < ROW; r++){
for (c = 0; c < COL; c++){
a[r][c] = 'a' + r * COL + c;
printf("%c ", a[r][c]);
}
printf("%c", '\n');
}
free(a);
return 0;
}

I had to check the second one to see if the syntax actually worked. It is
too complicated, and will break if COL is not a compile time constant, which
rather defeats the purpose of a dynamic array.

However you can argue that you are more likely to know the number of columns
than the number of rows at compile time, and you can use 2d array access
rather than calculate everything explicitly.
 
L

lovecreatesbea...

Do the following two functions use dynamic two-dimension array
correctly?

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

#define ROW 3
#define COL 5

int dyn2dary(void);
int dyn2dary_2(void);

int main(void)
{
dyn2dary();
printf("%c", '\n');
dyn2dary_2();
return 0;
}

int dyn2dary_2(void)
{
int r = 0, c = 0;
char (*a)[COL];
char *p = malloc(ROW * COL * sizeof *p);

if (!p) return -1;
a = (char (*) [COL])p;
for (r = 0; r < ROW; r++){
for (c = 0; c < COL; c++){
a[r][c] = 'a' + r * COL + c;
printf("%c ", a[r][c]);
}
printf("%c", '\n');
}
free(a);

I have changed this to

free(p);
 
B

Barry Schwarz

Do the following two functions use dynamic two-dimension array
correctly?

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

#define ROW 3
#define COL 5

int dyn2dary(void);
int dyn2dary_2(void);

int main(void)
{
dyn2dary();
printf("%c", '\n');
dyn2dary_2();
return 0;
}

int dyn2dary_2(void)
{
int r = 0, c = 0;
char (*a)[COL];
char *p = malloc(ROW * COL * sizeof *p);

The usual idiom in this situation is
a = malloc(ROW * sizeof *a);
which allocates the exact same amount of space without the
introduction of the extraneous p.
if (!p) return -1;
a = (char (*) [COL])p;
for (r = 0; r < ROW; r++){
for (c = 0; c < COL; c++){
a[r][c] = 'a' + r * COL + c;

Anally retentive nit: there is no guarantee that the expression will
fit in a char.
printf("%c ", a[r][c]);

Since there is no guarantee that the letters are contiguous in the
character set, the value of a[r][c] may not be printable (a[2][2] is
not in US EBCDIC).
I have changed this to

free(p);

Since a and p both point to the same byte in memory, this change makes
no difference.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top