Thread: dynamic memory

  1. #1
    Registered User falconetti's Avatar
    Join Date
    Nov 2001
    Posts
    15

    dynamic memory

    Hello all !

    I am starting to use my new aquired (and limited) knowledge on dynamic memory. With this program I intend to allocate some memory from the program heap and use it for storing an array of randomly generated integers.
    Function 'generate' prompts user for number of columns and rows. It then creates a unidimensional array of (col * row) elements (integers).
    Function 'print' prints the array as a bidimensional array by accesing array ary.

    When I try to execute it I keep getting a segmentation fault (core dumped) message. I will appreciate any help.

    Thanks in advance and Merry Christmas !

    Code :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void generate (int *size, int *row, int *col, int *ary);
    	
    void print (int *ary, int size, int col);
    
    int main (void)
    {
    	int col;
    	int row;
    	int size;
    	int *ary = NULL;
    	
    	generate (&size, &row, &col, ary);
    	
    	print (ary, size, col);
    
    	return 0;
    } /* main */
    
    void generate (int *size, int *row, int *col, int *ary)
    {
    	int i;
    	
    	printf ("Number of rows: ");
    	scanf ("%d", row);
    	printf ("Number of columns: ");
    	scanf ("%d", col);
    
    	srand (time (NULL));
    	
    	*size = *col * *row;
    
    	if (!(ary = (int *) (calloc (*size, sizeof (int)))))
    		exit (100);
    
    	for (i = 0; i < *size; i++)
    		*(ary + i) = rand () % 10;
    
    	return;
    } /* generate */
    
    void print (int *ary, int size, int col)
    {
    	int i = 0;
    			
    	
    	while (i < size)
    	{
    		printf ("%3d", *(ary + i));
    
    		if (!((i + 1) % col) && i)     /* last column */
    			printf ("\n");
    
    		i++;
    	} /* while */
    
    	return;
    } /* print */
    Last edited by falconetti; 12-21-2001 at 08:38 AM.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you are passing a variable into a function and want to alter what is stored in the original you have to pass a pointer to it. This applies to pointers aswell, you need to pass a pointer to a pointer. Try doing something like this -

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void generate (int *size, int *row, int *col, int **ary);
    	
    void print (int *ary, int size, int col);
    
    int main (void)
    {
    	int col;
    	int row;
    	int size;
    	int *ary = NULL;
    	
    	generate (&size, &row, &col, &ary);
    	
    	print (ary, size, col);
    
    	return 0;
    } /* main */
    
    void generate (int *size, int *row, int *col, int **ary)
    {
    	int i;
    	
    	printf ("Number of rows: ");
    	scanf ("%d", row);
    	printf ("Number of columns: ");
    	scanf ("%d", col);
    
    	srand (time (NULL));
    	
    	*size = *col * *row;
    
    	if (!(*ary = (int *) (calloc (*size, sizeof (int)))))
    		exit (100);
    
    	for (i = 0; i < *size; i++)
    		*(*ary + i) = rand () % 10;
    
    	return;
    } /* generate */
    
    void print (int *ary, int size, int col)
    {
    	int i = 0;
    			
    	
    	while (i < size)
    	{
    		printf ("%3d", *(ary + i));
    
    		if (!((i + 1) % col) && i)     /* last column */
    			printf ("\n");
    
    		i++;
    	} /* while */
    
    	return;
    } /* print */
    zen

  3. #3
    Registered User falconetti's Avatar
    Join Date
    Nov 2001
    Posts
    15

    thanks

    I took some time for me to understand why I should create a new ponter to a pointer , but I got it now.
    The value of the old pointer is changed by the called function

    Tnak you, zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM