2D Dynamically allocated pointer arrays

This is a discussion on 2D Dynamically allocated pointer arrays within the C Programming forums, part of the General Programming Boards category; I figured out what that memory issue was. I was trying to use the free function (see my previous code ...

  1. #16
    Registered User
    Join Date
    May 2005
    Posts
    207
    I figured out what that memory issue was. I was trying to use the free function (see my previous code post).

    When I REM out the free function, the code works without any errors!?

    I've had problems with the free function before; does anyone know what I'm doing wrong?

    mw

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably freeing something you shouldn't. Like Salem said, post your code.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    May 2005
    Posts
    207
    My code hasn't changed since the previous posting. Did you still want me to post it?

    mw

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I suppose that's up to you. If you still think calling free is what's breaking your program. Personally I don't care if you find out the answer and how to program or not. But if you do, then you might want to post it. It's up to you.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    May 2005
    Posts
    207
    You're pretty rude.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    /*
    
    This program demonstrates recursive functions and dynamic, multi-dimensional pointer arrays.
    
    It will calculate the total factorial value for multiple groups of items.
    
    You enter the total number of items, and the number of groups you will have.  Then you enter the
    number of items in each group.  The program error-checks to make sure the number of items
    entered in each groups adds up to be the total number of items.
    
    */
    
    
    //	Prototypes.
    
    unsigned long factr (unsigned long num_items2, unsigned long num_groups2);
    unsigned long factorial_engine (unsigned long n);
    
    void main ()
    {
    	unsigned long num_items = 0;
    	unsigned long num_groups = 0;
    
    	printf ("\n\nHow many items total?\n");
    	scanf ("%u", &num_items);
    
    	printf ("\n\nHow many groups?\n");
    	scanf ("%u", &num_groups);
    
    	if (num_groups > num_items)
    	{
    		printf("\n\nError! Can't have more groups than items!\n\n");
    		exit (1);
    	}
    	else
    	{
    		factr (num_items, num_groups);
    	}
    }
    
    unsigned long factr (unsigned long num_items2, unsigned long num_groups2)
    {
    
    //	These are in order of appearance.
    
    	unsigned long loop = 0;
    	unsigned long total = 0;
    	unsigned long quantity = 0;
    	unsigned long numerator = 0;
    	unsigned long denominator = 1;
    
    //	Array dimensioning.
    
    	unsigned long *group_total;
    	group_total = malloc (num_groups2*2*sizeof(int));
    
    	if(!group_total)
    	{
    		puts ("\n\nERROR! Not enough Memory!\n\n");
    		exit (1);
    	}
    
    //	Array initialization.
    
    	for (loop = 1; loop <= num_items2; loop++)
    	{
    		group_total[loop][1] = 0;
    		group_total[loop][2] = 0;
    	}
    	loop = 0;
    
    //	Array data-entry.
    
    	for (loop = 1; loop <= num_groups2; loop++)
    	{
    		printf ("\n\nHow many in group %d\n", loop);
    		scanf ("%u", &quantity);
    
    		group_total[loop][1] = quantity;
    		total = total + quantity;
    	}
    	loop = 0;
    
    //	Error checking and factorial calculations for each group
    
    	if (total = num_groups2);
    	{
    		for (loop = 1; loop <= num_groups2; loop++)
    		{
    			group_total[loop][2] = factr (quantity);
    		}
    		loop = 0;
    	}
    	else
    	{
    		printf ("\n\nTotal number of items not equal to total items entered!\n\n");
    		free (group_total);
    		exit (1);
    	}
    	total = 0;
    
    //	Final calculations.
    
    	numerator = factr (num_items2);
    
    	for (loop = 1; loop <= num_groups2; loop++)
    	{
    		denominator = denominator * group_total[loop][2];
    	}
    	total = numerator / denominator;
    	printf ("\n\nTotal factorial for this mess: %u\n\n", total);
    	total = 0;
    	loop = 0;
    	free (group_total);
    }
    
    //	Factorial engine.
    
    unsigned long factorial_engine (unsigned long n)
    {
    	unsigned long answer;
    
    	if (n==1) return (1);
    	answer = n==0 ? 1: n * factr (n - 1); // recursive call
    	return (answer);
    }

  6. #21
    Registered User
    Join Date
    May 2005
    Posts
    207
    So you're not going to help me now? I asked if it were alright to post my code again because I didn't want to post it too many times. Your answer was childish.

    If you don't like me saying that then tough. Grow up.

    mw
    Last edited by Lionmane; 06-10-2005 at 10:57 PM.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So you don't post code here when originally asked, and then you expect me to hang around for the rest of my life hoping you actually post it? Is that it? I'm sorry, there were a few other things I felt like doing, rather than hoping you'd get around to listening to people.

    What's that? I shouldn't have read and replied to any one else's posts and just answered yours, in the off chance you actually posted your code? Yeah. I'm the rude one.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    May 2005
    Posts
    207
    So are you going to help me?

    mw

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well let's begin, shall we?

    1) This is wrong. Read the FAQ.
    Code:
    void main ()
    2) This is wrong. Arrays do not start at 1. They start at 0. Also, they do not go through "size", they go through "size - 1".
    Code:
    //	Array initialization.
    
    	for (loop = 1; loop <= num_items2; loop++)
    	{
    		group_total[loop][1] = 0;
    		group_total[loop][2] = 0;
    	}
    	loop = 0;
    Also, there's absolutely no point in setting loop to 0 at the end. Furthermore, what exactly do you think you're doing with the second set of array indexes? Nothing correct, that's for sure. Just because you decide to add more array indexes, doesn't mean it's going to magicly move down to your second array dimension, because there isn't one.

    I mean really, you don't even understand the simplest things:

    3) This is an assignment, not a truth test:
    Code:
    	if (total = num_groups2);
    This says "assign the value in num_groups2 to total", and then as long as that assignment's value is non-zero, considers itself true.


    In short, free is not your problem. The problem is you don't have a clue as to what you're doing, even though you've been shown over and over how to do it right.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    May 2005
    Posts
    207
    Ok, I've corrected those issues. My program compiles correctly and I've tested it enough to know that it works correctly when I test it.

    Honestly, all I did was copy/paste my program from the previous page. I corrected those issues that you mentioned earlier this afternoon, but the code was still essentially the same.

    Now when I try to use the free function I get that error.

    mw

  11. #26
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Lionmane
    You're pretty rude.
    Don't confuse rudeness with blunt honesty

  12. #27
    Registered User
    Join Date
    May 2005
    Posts
    207
    When your signature is "Hello. You're an idiot", then it's pretty safe to assume the whole attitude is wrong.

    Also, there's very little difference between "blunt" and "rude".

    Try acting the way he does when you're around women and see how often you get laid (blunt honesty).

    mw
    Last edited by Lionmane; 06-10-2005 at 11:30 PM.

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Lionmane
    Ok, I've corrected those issues.
    I doubt it. I really doubt you've "corrected those issues", since I bet you don't even know what specificly I was talking about with your incorrect array indexing. (No, not the loop initializer, the subscripting.)


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #29
    Registered User
    Join Date
    May 2005
    Posts
    207
    The declaration:
    int array(10);

    means:
    array(0)
    array(1)
    array(2)
    array(3)
    etc...
    array(9)

    It was easy enough to change, and easy enough to understand.

    Look, if you don't know how to use the free function then why are you posting?

    Oh yeah: I read the FAQ about void main() and I'll correct it as soon as I get to my work computer.

    mw
    Last edited by Lionmane; 06-10-2005 at 11:47 PM.

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The free call is not your problem. I've already said that. You're just being an idiot and ignoring me again. The problem is you don't know how to use your dynamic memory correctly, and you're traipsing around in memory where you don't belong. Still don't believe me? Here, let me show you:
    Code:
    #include <stdlib.h>
    
    int main( void )
    {
        unsigned long num_groups2 = 10;
        unsigned long *group_total;
        group_total = malloc (num_groups2*2*sizeof(int));    
    
        free( group_total );
    
        return 0;
    }
    Compile that, run it. Does it crash? No. It doesn't. That means free is not your problem. Holy deja vu, Batman!


    Quzah.
    Hope is the first step on the road to disappointment.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-16-2007, 08:21 PM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. Checking if memory has been dynamically allocated
    By Xzyx987X in forum C Programming
    Replies: 28
    Last Post: 03-14-2004, 05:53 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 10:26 PM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21