The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Dynamically allocating memory to by reference variables
Discuss Dynamically allocating memory to by reference variables in the C Programming forum on Dev Shed. Dynamically allocating memory to by reference variables C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 2nd, 2012, 12:12 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 23 m 35 sec
Reputation Power: 0
|
|
|
Dynamically allocating memory to by reference variables
I need to dynamically assign memory to an array that is defined in a calling function. I am relatively new to C and would be very grateful for any advice about doing this.
I have tried something like the following:
Code:
int main()
{
int n = 4; // dimension of square matrix
double** matrix; // The matrix
mk_matrix(&matrix, n);
return 0;
}
void mk_matrix (double*** matrix, int n)
{
*matrix = (double*)malloc(n*sizeof(double)); // assign memory for rows
for(i=0;i<n;i++)
{
*matrix[i]=(double*)malloc(n*sizeof(double)); // assign memory for columns
}
}
Thank you in advance.
|

December 2nd, 2012, 12:17 PM
|
 |
Contributed User
|
|
|
|
Observe.
Code:
int main()
{
int n = 4; // dimension of square matrix
double** matrix; // The matrix
mk_matrix(&matrix, n);
return 0;
}
void mk_matrix (double*** matrix, int n)
{
//!! no cast of malloc, and different type in sizeof
*matrix = malloc(n*sizeof(double*)); // assign memory for rows
for(i=0;i<n;i++)
{
//!! () around the pointer, no cast of malloc
(*matrix)[i]=malloc(n*sizeof(double)); // assign memory for columns
}
return;
}
You don't need to cast the return result of malloc in a C program. If you "need" a cast, you're doing something wrong - so wrong that just sweeping the problem under the carpet with a cast is the wrong thing to do.
|

December 2nd, 2012, 12:56 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 23 m 35 sec
Reputation Power: 0
|
|
Thanks very much Salem. Much appreciated.
Please could you also advise me how to then free the memory I have allocated?
I've got:
Code:
void free_matrix(double*** matrix, int n) {
for(i=0;i<n;i++) {
free(*matrix[i]);
}
free(*matrix);
}
Thanks!
|

December 2nd, 2012, 02:10 PM
|
 |
Contributed User
|
|
|
|
|
Well you can get away with
void free_matrix(double** matrix, int n)
which simplifies things somewhat.
>free(*matrix[i]);
Recall the use of () in the malloc calls previously shown.
|

December 3rd, 2012, 05:34 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 3
Time spent in forums: 23 m 35 sec
Reputation Power: 0
|
|
|
Once again, thank you very much. This will be a very useful thing to be able to do.
|

December 4th, 2012, 01:50 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem Observe.
Code:
int main()
{
int n = 4; // dimension of square matrix
double** matrix; // The matrix
mk_matrix(&matrix, n);
return 0;
}
void mk_matrix (double*** matrix, int n)
{
//!! no cast of malloc, and different type in sizeof
*matrix = malloc(n*sizeof(double*)); // assign memory for rows
for(i=0;i<n;i++)
{
//!! () around the pointer, no cast of malloc
(*matrix)[i]=malloc(n*sizeof(double)); // assign memory for columns
}
return;
}
You don't need to cast the return result of malloc in a C program. If you "need" a cast, you're doing something wrong - so wrong that just sweeping the problem under the carpet with a cast is the wrong thing to do. |
salem can you just explain why casting will be not needed , that means if i write the code like follows i think there is no error :
void mk_matrix (double*** matrix, int n)
{
*matrix = (double **)(malloc(n*sizeof(double*));
for(i=0;i<n;i++)
{
(*matrix)[i]=(double *)malloc(n*sizeof(double));
}
|

December 4th, 2012, 02:24 AM
|
 |
Contributed User
|
|
|
|
Try compiling it without the casts!
If you get something like
bar.c:4:13: warning: initialization makes pointer from integer without a cast
Then you need to FIX your code by having #include <stdlib.h> at the top of the file.
If your machine has larger pointers than integers, then having malloc declared as implicitly returning an int will lose information when you cast. Your resultant 'pointers' will be both non-null AND garbage.
If you get something like
bar.cpp:4:22: error: invalid conversion from ‘void*’ to ‘char*’
Then you need to FIX your build system so that you compile your C code with a C compiler (and not a C++ compiler). This might mean for example renaming prog.cpp to be prog.c
C and C++ are different languages, despite their recent common heritage.
http://en.wikipedia.org/wiki/Compat...f_C_and_C%2B%2B
http://david.tribble.com/text/cdiffs.htm
If you just casually compile your C code with a C++ compiler, you're just digging a big hole for yourself. Even if you don't care about the portability of the code, you'll be learning a lot of poor style which will cause you trouble when you come to use a real C compiler.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|