C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 2nd, 2012, 12:12 PM
farleytowers farleytowers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 farleytowers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old December 2nd, 2012, 12:17 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 49 m 45 sec
Reputation Power: 1774
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.
Comments on this post
farleytowers agrees!
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 2nd, 2012, 12:56 PM
farleytowers farleytowers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 farleytowers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #4  
Old December 2nd, 2012, 02:10 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 49 m 45 sec
Reputation Power: 1774
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.
Comments on this post
farleytowers agrees!

Reply With Quote
  #5  
Old December 3rd, 2012, 05:34 AM
farleytowers farleytowers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 farleytowers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old December 4th, 2012, 01:50 AM
abhiakhi abhiakhi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 abhiakhi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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));
}

Reply With Quote
  #7  
Old December 4th, 2012, 02:24 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 49 m 45 sec
Reputation Power: 1774
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Dynamically allocating memory to by reference variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap