I would like to create a matrix[i][j] of integers by allocating memory
dynamically (malloc or calloc) because i and j are defined during
execution of the program.
I have got not problem to do this in the static way. For instance, if I
want to declare a matrix 20x10 of integers and then to access
matrix[15,8] element:
int matrix[20][10];
int a;
a=matrix[15][8];
But I havn't the clue to do it dynamically in case of i=20 and j=10.
int i,j,a;
i=20;j=10;
Thanks for your help,
Gattaca
Nov 14 '05
14 3134
Case <no@no.no> wrote: BTW, the FAQ (Q6.16) does use a cast:
int **array1 = (int **)malloc(nrows * sizeof(int *));
Why do you think there should be none?
For several reasons. There have been near endless threads about this on
c.l.c over the past years. I'm sure you can find some of them in a
Usenet archive. Let me just present my favourite argument: by adding a
cast to malloc(), which does not need it at all and is pretty common in
C code, you train yourself to think "Oh, yet another cast. Yawn." Then,
when you encounter a _real_ cast, in a situation where it is needed, you
do not, as you should, sit up and pay attention, because something odd
is done to the C type system; you are used to casts doing, in essence,
nothing, when you _should_ consider a cast to be a warning sign. IOW, a
programmer who casts malloc() is like the little boy who cried "Wolf!".
Richard
Richard Bos wrote: Case <no@no.no> wrote:
BTW, the FAQ (Q6.16) does use a cast:
int **array1 = (int **)malloc(nrows * sizeof(int *));
Why do you think there should be none?
For several reasons. There have been near endless threads about this on c.l.c over the past years. I'm sure you can find some of them in a Usenet archive. Let me just present my favourite argument: by adding a cast to malloc(), which does not need it at all and is pretty common in C code, you train yourself to think "Oh, yet another cast. Yawn." Then, when you encounter a _real_ cast, in a situation where it is needed, you do not, as you should, sit up and pay attention, because something odd is done to the C type system; you are used to casts doing, in essence, nothing, when you _should_ consider a cast to be a warning sign. IOW, a programmer who casts malloc() is like the little boy who cried "Wolf!".
I never knew that K&R are both little boys. ;-) I see malloc() casts
all over their famous book. At page 167 they even say:
"The pointer returned by malloc or calloc has the proper
alignment for the object in question, but it *must* be
cast into the appropriate type, as in ..."
Why the heck is this? And was the FAQ written by other little boys?
Case
Richard Bos wrote: Case <no@no.no> wrote:
BTW, the FAQ (Q6.16) does use a cast:
int **array1 = (int **)malloc(nrows * sizeof(int *));
Why do you think there should be none?
For several reasons. There have been near endless threads about this on c.l.c over the past years. I'm sure you can find some of them in a Usenet archive.
I'll try to find some later. Thanks!
Case wrote: I never knew that K&R are both little boys. ;-) I see malloc() casts all over their famous book. At page 167 they even say:
"The pointer returned by malloc or calloc has the proper alignment for the object in question, but it *must* be cast into the appropriate type, as in ..."
From Ritchie's on-line errata list:
142(§6.5, toward the end): The remark about casting the return value of
malloc ("the proper method is to declare ... then explicitly coerce")
needs to be rewritten. The example is correct and works, but the advice
is debatable in the context of the 1988-1989 ANSI/ISO standards. It's
not necessary (given that coercion of void * to ALMOSTANYTYPE * is
automatic), and possibly harmful if malloc, or a proxy for it, fails to
be declared as returning void *. The explicit cast can cover up an
unintended error. On the other hand, pre-ANSI, the cast was necessary,
and it is in C++ also. http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
Brian Rodenborn
On Mon, 24 May 2004 13:33:50 +0200, Case <no@no.no> wrote:
<snipped casting (return from) malloc> BTW, the FAQ (Q6.16) does use a cast:
Not in the latest version, ca 1999 (!), which the web version at http://www.eskimo.com/~scs/C-faq/top.html (still!) is NOT.
See the download/plaintext versions there under versions.html,
periodically posted, and at rtfm.mit.edu and faqs.org.
- David.Thompson1 at worldnet.att.ne t This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jakob Bieling |
last post by:
Hi,
Whenever allocating memory using operator new or operator new I feel
like I should only use it very sparingly, because otherwise memory is wasted
(by additional overhead needed to manage all those allocations) which also
loses some performance. I am specifically talking about many little
allocations (approx. 16-512 bytes). Is my feeling about this justified?
thanks!
--
|
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 = new double;
}
|
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 then allocate to the
pointers to which those pointers point?
essentially lets say i during runtime deturmine i must allocate space for 60
|
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 = malloc(sizeof(*arr) * 4);
/* I want to dynamically allocate
|
by: Paminu |
last post by:
If I have this struct:
#include <stdlib.h>
#include <stdio.h>
#define KIDS 4
typedef struct test {
int x;
int y;
} container;
| |
by: junky_fellow |
last post by:
Hi guys,
Consider the following piece of code:
func()
{
if(x==0) {
int arr;
/* do something */
|
by: K. Jennings |
last post by:
I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that sizeof(u64)
is 8, will
u64 *p = malloc(N) ;
do the trick in general?
Here N is a positive integer, and I am assuming that malloc()
succeeds in allocating the chunk of memory specified.
|
by: UncleRic |
last post by:
Greetings:
I'm trying to get the ASCI C syntax correct here. I want to create memory space for a variable-size array of pointers to structs.
This is what I found in theScripts' archive:
This is what I'm toying with:
#include <stdio.h>
|
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 = malloc(n*sizeof(r))
and (to a degree) by the surrounding error checking.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |