473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

program crashed

14 New Member
Hi,

I was trying to write mesh sort algorithm in C++.
However my program crashed when it runs.

Attached is the code.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int* merge(int a[], int b[], int size1, int size2)
  7. {
  8.  
  9.     int *value = (int*)malloc(size1+size2);
  10.     int i, j, k;
  11.     i=j=k=0;
  12.  
  13.     while(i < size1 || j < size2){
  14.       if(a[i] < b[j])
  15.          value[k++] = a[i++];
  16.       else 
  17.          value[k++] = b[j++];
  18.     }
  19.     while(i < size1 )
  20.          value[k++] = a[i++];
  21.     while(j < size2)
  22.          value[k++] = b[j++];
  23.  
  24.     free(a);
  25.     free(b);
  26.  
  27.     return value;
  28.  
  29. }
  30.  
  31. int* mergeSort(int* input, int start, int end)
  32. {
  33.  
  34.       if(start >=end ){
  35.         return (input+start);
  36.       }
  37.  
  38.       int *a = mergeSort(input, start, (start+end)/2 );
  39.       int *b = mergeSort( input, (start+end)/2+1, end);
  40.  
  41.       int size1 = (end-start)/2+1;
  42.       int size2 = (end-start)/2+1;
  43.  
  44.       return merge(a, b, size1, size2);
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50.     int input[] = {7,9,2,6,1,4,5,8};
  51.     int * output;
  52.     output = mergeSort(input, 0, 7);
  53.  
  54.     for(int j = 0; j < 7; j++)
  55.       cout<< *(output+j);
  56.  
  57.     system("pause");
  58.  
  59.     return 0;
  60.  
  61.  
  62. }
  63.  
I assume it is because of the allocating the memory for storing the value.

I appreciate if someone can help to solve this problem...
Apr 20 '11 #1
3 1864
Banfa
9,065 Recognized Expert Moderator Expert
You mean and not or at line 13.

Line 24 and 25, freeing data that has been allocated elsewhere is mildly strange since you can not be sure how it was allocated.

mergeSort: Line 35 you return whatever input points at; at line 44 you return the result of merge which is an malloc'd buffer. Both of these get passed to merge at some point which assumes they are malloc'd and frees them. However you have no guarantee that input is malloc'd since this function is called externally and in fact the first time it is called from main you pass a pointer to a buffer on the stack that you subsequently try too free.

When you call mergeSort you have no idea if the returned value is a malloc'd buffer or a buffer of whatever type you passed in.


Line 9: when you malloc data ALWAYS check that the returned pointer value is not NULL before you try to use it.
Apr 20 '11 #2
June Ye
14 New Member
Hi Banfa,

Thank you very much for your response.

>You mean and not or at line 13.

That's correct. I made a mistake.

>Line 24 and 25, freeing data that has been allocated elsewhere is mildly strange since you can not be sure how it was allocated.

That's true. However when I omit that part, the program crashed earlier... Not sure how to correct that part.
I know the allocating memory part and freeing it is messed up. Do you have any suggestion about how to correct it?

Thanks a lot!
Apr 20 '11 #3
June Ye
14 New Member
Hi Banfa,

Here is the working program.
Thanks a lot!

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int* merge(int a[], int b[], int size1, int size2)
  7. {
  8.  
  9.     int *value = (int*)malloc((size1+size2)*sizeof(int));
  10.     int i, j, k;
  11.     i=j=k=0;
  12.  
  13.     while(i < size1 || j < size2){
  14.       if(a[i] < b[j])
  15.          value[k++] = a[i++];
  16.       else 
  17.          value[k++] = b[j++];
  18.     }
  19.     while(i < size1 )
  20.          value[k++] = a[i++];
  21.     while(j < size2)
  22.          value[k++] = b[j++];
  23.  
  24.     return value;
  25.  
  26. }
  27.  
  28. int* mergeSort(int* input, int start, int end)
  29. {
  30.  
  31.       if(start >=end ){
  32.         return (input+start);
  33.       }
  34.  
  35.       int *a = mergeSort(input, start, (start+end)/2 );
  36.       int *b = mergeSort( input, (start+end)/2+1, end);
  37.  
  38.       int size1 = (end-start)/2+1;
  39.       int size2 = (end-start)/2+1;
  40.  
  41.       int* result = merge(a, b, size1, size2);
  42.       free (a);
  43.       free (b);
  44.       return result;
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50.     int input[] = {7,9,2,6,1,4,5,8};
  51.     int * output;
  52.     output = mergeSort(input, 0, 7);
  53.  
  54.     for(int i = 0; i < 8; i++)
  55.       cout<< *(output+i);
  56.  
  57.     system("pause");
  58.  
  59.     return 0;
  60.  
  61. }
  62.  
Apr 21 '11 #4

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

Similar topics

0
1191
by: DEATH TO KENT WEST HILL | last post by:
Praise http://linux-ntfs.sourceforge.net/misc.html#praise If you have a success story you'd like to share, let me know (webmaster@flatcap.org). Your project just saved me!! I run red hat...
3
1945
by: John Cho | last post by:
/* This program is a database program that will store video tape names, minutes, year released, and price I want it to be professional so that *YOU* will want to buy it and use it for a video...
3
2187
by: Daragoth | last post by:
Hi, I'm writing a program using Metrowerks CodeWarrior 4.0 that determines the best possible combination of a data set by checking every possible combination. I found there were about 250,000,000...
11
2217
by: Paul Tremblay | last post by:
Hi, Is there a way to ensure that only one instance of an application runs on a physical machine. I would want to do this in a cross platform way. One ide I have is to obtain a list of the...
5
42257
by: Seo Jae Ick | last post by:
Hi? My name is Seo Jae Ick. I have a problem with running a program on Linux RedHat 8.0. GDB have reported this, program exited with code 0377 I think, this statement comes when 1)...
10
1458
by: Wes Johnson | last post by:
I've encountered a problem where I am changing a particular c source document and then running the associated make command in the console. If I then run that program, it is as though the update has...
1
1163
by: tom | last post by:
Hello, I have a VC++ .NET project crashed at run time. Does anybody know how to run debug in .NET that I can get to the line where the program crashes? Thanks.
3
2060
by: Chozomaster747 | last post by:
I'm in my last week in High School and my last program has seemed to have been shot. My teacher blames it on the computers and says that my program should run, but I have an error opening my files. ...
0
1010
by: Curious | last post by:
A program crashed silently. Fortunately, I was able to get a .dmp file. Hopefully it can provide us with info on why it crashed. When I opened the .dmp file in Notepad, it took a long time to...
1
1963
by: raghulvarma | last post by:
Hai friends, I need to know what would happen when my IIS gets crashed? what would happen to all the projects present inside it?How should I rectify that?What should I do when I am...
0
7207
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
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7361
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
5026
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
4693
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...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
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 ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
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...

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.