473,698 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ 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 2665
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
2772
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: "At a certain point in the code you may be unsure if a particular block is no longer needed. If you free() this piece of memory, but continue to access it (probably via a second pointer to the same
14
476
by: chai | last post by:
Can anyone help me in finding elements stored in a dynamic array in.
11
3044
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
2509
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 cycles due to memory leackage. I am not very expert in programing but it seems that my free() function does not do anything to my program. One example of how I do memory allocation and free is this: int *D; D= (int *) malloc(size). void myfunction{...
24
19077
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
4
5065
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 deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
1
7964
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
14
3823
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 this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
10
4420
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 the allocation is impossible. Sworna vidhya
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.