473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Struct + dynamic memory allocation + free ()

6 New Member
Hi,
I'm studying the basis of C, C++ and Assembly languages at my university (I have two exams about these subjects,

for now) ... and I have a problem ^^.
I wrote a program in C (not so optimized, this isn't our present goal for my professors) that recieves in input an

integer (matrix size) and a double pointer to a square matrix and print on screen the co-ordinate of each square

sub-matrix from size 2 to n - 1 size (where n is the integer that my function recieves in input) that has inside

the biggest sum of its own members and the sum itself.
Then I modified the program so I created a struct in which I dynamically allocated two arrays in which i store the

co-ordinate that my function finds. So I can allocate them, I can populate themm I can access their elements but I

can't free them (I recieve an error regarding the corruption of the heap).
I can't figure out the problem ... I can dynamically allocate the matrix and free it, why I can't free my two

arrays?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct cont_
  6. {
  7.     int *datax;
  8.     int *datay;
  9. } cont;
  10.  
  11. int** mtx_a (int num);
  12. int mtx_i (int num, int **mtx);
  13. void mtx_p (int num, int **mtx);
  14. int mtx_c (int num, int **mtx, cont data);
  15. void mtx_d(int num, int **mtx);
  16.  
  17. cont mtx_sa (int num);
  18. int mtx_ss (int num, int x, int y, cont data);
  19. void mtx_sd (cont data);
  20.  
  21. int main ()
  22. {
  23.     // (1) Dichiaro le variabili.
  24.     int num = 0;
  25.     int check = 0;
  26.  
  27.     // (2) Chiedo all'utente di inserire il lato della matrice.
  28.     do
  29.     {
  30.         printf("Inserire la lunghezza del lato della matrice quadrata: ");
  31.         scanf("%d", &num);
  32.  
  33.         if (num <= 0)
  34.             printf("Il numero deve essere maggiore di 0\n");
  35.  
  36.     } while (num <= 0);
  37.  
  38.     // (I) Store Allocation
  39.     cont data = mtx_sa (num);
  40.  
  41.     // (3) Dichiaro dopppio puntatore ed alloco spazio per matrice.
  42.     int **test = mtx_a (num);
  43.  
  44.     // (4) Inizializzo matrice.
  45.     mtx_i (num, test);
  46.  
  47.     // (5) Stampo la matrice.
  48.     mtx_p (num, test);
  49.  
  50.     // (6) Eseguo operazioni su sotto-matrici quadrate.
  51.     mtx_c (num, test, data);
  52.  
  53.     int i = 0;
  54.     for (i = 2; i < num; i++)
  55.     {
  56.         printf("%d # %d\n", data.datax [i], data.datay [i]);
  57.     }
  58.  
  59.     // (III) Store 3
  60.     mtx_sd (data);
  61.  
  62.     // (7) Libero lo spazio allocato dinamicamente per la matrice.
  63.     mtx_d (num, test);
  64.  
  65.     // (9) Metto in pausa l'applicazione per verificare i risultati e ritorno.
  66.     system ("pause");
  67.     return 0;
  68. }
  69.  
  70. int** mtx_a (int num)
  71. {
  72.     int i  = 0;
  73.     int j = 0;
  74.  
  75.     int **tmp = (int **) calloc (num, sizeof (int *));
  76.     for (i = 0; i < num; i++)
  77.     {
  78.         tmp[i] = (int *) malloc (num * sizeof (int));
  79.     }
  80.  
  81.     return tmp;
  82. }
  83.  
  84. int mtx_i (int num, int **mtx)
  85. {
  86.     int i = 0;
  87.     int j = 0;
  88.     int val = 0;
  89.  
  90.     srand ((unsigned) time (NULL));
  91.  
  92.     for (i = 0; i < num; i++)
  93.     {
  94.         for ( j = 0; j < num; j++)
  95.         {
  96.             mtx [i][j] = val;
  97.             val = (val + j + rand ()) % 9;
  98.         }
  99.     }
  100.  
  101.     return 0;
  102. }
  103.  
  104. void mtx_p (int num, int **mtx)
  105. {
  106.     int i = 0;
  107.     int j = 0;
  108.  
  109.     for (i = 0; i < num; i++)
  110.     {
  111.         for (j = 0; j < num; j++)
  112.         {
  113.             printf("%d", mtx [i][j]);
  114.  
  115.             if (j == num - 1)
  116.                 printf("\n");
  117.             else
  118.                 printf(" ");
  119.         }
  120.     }
  121.     printf("\n");
  122.  
  123.     return;
  124. }
  125.  
  126. int mtx_c (int num, int **mtx, cont data)
  127. {
  128.     int i = 0;
  129.     int j = 0;
  130.     int z = 0;
  131.  
  132.     int side = num - 1;
  133.     int lap = side * side;
  134.     int sommaT = 0;
  135.     int sommaD = 0;
  136.     int I = 0;
  137.     int J = 0;
  138.     int lato = 2;
  139.     int dis = 1;
  140.     int pos1D = 0;
  141.     int pos2D = 0;
  142.  
  143.     do
  144.     {    
  145.         for (z = lap; z > 0; z--)
  146.         {
  147.             for (i = I; i <= I + dis; i++)
  148.             {
  149.                 for (j = J; j <= J + dis ; j++)
  150.                 {
  151.                     sommaT = sommaT + mtx [i][j];
  152.                 }
  153.             }
  154.  
  155.             if (sommaT > sommaD)
  156.             {
  157.                 sommaD = sommaT;
  158.                 pos1D = I;
  159.                 pos2D = J;
  160.             }
  161.  
  162.             J++;
  163.  
  164.             if (J >= side)
  165.             {
  166.                 I = I++;
  167.                 J = 0;
  168.             }
  169.  
  170.             sommaT = 0;
  171.         }
  172.  
  173.         // Store 2
  174.         mtx_ss ((num + 1) - side, pos1D, pos2D, data);
  175.  
  176.         printf("Lato: %d # Somma sotto-matrice qudarata: %d. Coord. x: %d # y: %d\n\n", ((num + 1) - side), 
  177.  
  178. sommaD, pos1D, pos2D);
  179.  
  180.         side--;
  181.         lap = side * side;
  182.         lato++;
  183.         sommaD = 0;
  184.         I = 0;
  185.         J = 0;
  186.         dis++;
  187.  
  188.     } while (side >= 2);
  189.  
  190.     return 0;
  191. }
  192.  
  193. void mtx_d(int num, int **mtx)
  194. {
  195.     int i = 0;
  196.  
  197.     for (i = 0; i < num; i++)
  198.     {
  199.         free (mtx [i]);
  200.     }
  201.     free (mtx);
  202.  
  203.     return;
  204. }
  205.  
  206. cont mtx_sa (int num)
  207. {
  208.     cont data;
  209.  
  210.     data.datax = (int *) calloc (num + 1, sizeof (int));
  211.     data.datay = (int *) calloc (num + 1, sizeof (int));
  212.  
  213.     data.datax [0] = 0;
  214.     data.datay [0] = 0;
  215.     data.datax [1] = 0;
  216.     data.datay [1] = 0;
  217.     data.datax [num] = 0;
  218.     data.datay [num] = 0;
  219.     data.datax [num + 1] = 0;
  220.     data.datay [num + 1] = 0;
  221.  
  222.     return data;
  223. }
  224.  
  225. int mtx_ss (int num, int x, int y, cont data)
  226. {
  227.     data.datax [num] = x;
  228.     data.datay [num] = y;
  229.  
  230.     return 0;
  231. }
  232.  
  233. void mtx_sd (cont data)
  234. {    
  235.     free (data.datax);
  236.     free (data.datay);
  237.  
  238.     return;
  239. }
  240.  
Nov 30 '09 #1
1 2638
gianx80
6 New Member
Solved. Lines 206 - 220 should be:

Expand|Select|Wrap|Line Numbers
  1. cont mtx_sa (int num)
  2. {
  3.     cont data;
  4.  
  5.     data.datax = (int *) calloc (num + 1, sizeof (int));
  6.     data.datay = (int *) calloc (num + 1, sizeof (int));
  7.  
  8.     data.datax [0] = 0;
  9.     data.datay [0] = 0;
  10.     data.datax [1] = 0;
  11.     data.datay [1] = 0;
  12.     data.datax [num - 1] = 0;
  13.     data.datay [num - 1] = 0;
  14.     data.datax [num] = 0;
  15.     data.datay [num] = 0;
  16.  
Nov 30 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: s.subbarayan | last post by:
Dear all, I happen to come across this exciting inspiring article regarding memory leaks in this website: http://www.embedded.com/story/OEG20020222S0026 In this article the author mentions:...
14
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
17
by: farshid | last post by:
I have written a long program with c, and am using dynamic memory allocation. This program is supposed to be run over and over (300 times) for a long simulation. But the program stops after 120...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
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...
0
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 ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.