Dynamic memory C, realloc exception - c

I am trying to write code using dynamic memory allocation, and facing the problem that when realloc() is called in genPointLinkArray(), the program throws an exception: ConsoleApplication1.exe has triggered a breakpoint.
I am using Visual Studio 15 and running the program in Debug mode.
struct graphPoint
{
point p;
int value;
};
struct graphLink
{
point start;
graphPoint end;
};
graphLink* genPointLink(char map[10][10], point start, graphPoint primaryPoint[22], int *linksNum)
{
graphLink *lin = (graphLink*)0;
(*linksNum) = 0;
for (int i = 0; i < 22; i++)
{
if (!ifWay(map, start, primaryPoint[i].p))
{
(*linksNum)++;
if(*linksNum == 1)
lin = (graphLink*)malloc(sizeof(graphLink));
else
{
lin = (graphLink*)realloc(lin, (*linksNum) * sizeof(graphLink));
}
(lin + (*linksNum - 1))->start = start;
(lin + (*linksNum - 1))->end = primaryPoint[i];
}
}
return lin;
}
graphLink* genPointLinkArray(char map[10][10], graphPoint primaryPoint[22], int *linksNum)
{
graphLink *links = (graphLink*)0, *l;
int currentNum = 0;
*linksNum = 0;
for (int i = 0; i < 22; i++)
{
l = genPointLink(map, primaryPoint[i].p, primaryPoint, &currentNum);
if(currentNum != 0)
if (*linksNum == 0)
links = (graphLink*)calloc(currentNum, sizeof(graphLink));
else
links = (graphLink*)realloc(links, ((currentNum + *linksNum) * sizeof(graphLink)));
*linksNum = *linksNum + currentNum;
for (int i = 0; i < currentNum; i++)
{
links[*linksNum - 1 + i].end = l[i].end;
links[*linksNum - 1 + i].start = l[i].start;
}
}
for (int i = 0; i < *linksNum; i++)
{
printf("(%d ; %d) -> (%d ; %d) : %d\n", links[i].start.x, links[i].start.y, links[i].end.p.x, links[i].end.p.y, links[i].end.value);
}
return links;
}

Your problem is the following part of code
*linksNum = *linksNum + currentNum;
for (int i = 0; i < currentNum; i++)
{
links[*linksNum - 1 + i].end = l[i].end;
links[*linksNum - 1 + i].start = l[i].start;
}
You update the *linksNum before the loop, so inside the loop you are addressing the links "array" out of bound: Undefined Behavior.

Related

Segfault in Merge - Sort in C

I am trying to sort an array of structures of size 5500 using merge sort.
However, I am getting a segmentation fault pretty quickly because I am not allowed to use VLA. so I have to create 2 extra arrays of size 5500 each time I call merge-sort recursively.
I would appreciate a fix for my problem. I will provide my code here:
void merge(Student rightArr[], Student leftArr[], Student mergedArr[], int sizeOfRight, int sizeOfLeft) {
int rightArrIndex = 0;
int leftArrIndex = 0;
int mergedArrIndex = 0;
while (leftArrIndex < sizeOfLeft && rightArrIndex < sizeOfRight) {
char *ptrLeft, *ptrRight;
long gradeLeft = strtol(leftArr[leftArrIndex].grade, &ptrLeft, BASE_COUNT);
long gradeRight = strtol(rightArr[rightArrIndex].grade, &ptrRight, BASE_COUNT);
if (gradeLeft > gradeRight) {
mergedArr[mergedArrIndex] = rightArr[rightArrIndex];
rightArrIndex++;
} else {
mergedArr[mergedArrIndex] = leftArr[leftArrIndex];
leftArrIndex++;
}
mergedArrIndex++;
}
if (leftArrIndex == sizeOfLeft) {
for (int i = mergedArrIndex; i < (sizeOfLeft + sizeOfRight); i++) {
mergedArr[i] = rightArr[rightArrIndex];
rightArr++;
}
} else {
for (int i = mergedArrIndex; i < (sizeOfLeft + sizeOfRight); i++) {
mergedArr[i] = leftArr[leftArrIndex];
leftArr++;
}
}
}
void mergeSort(Student studentsArray[], int amountOfStudents) {
if (amountOfStudents <= 1) {
return;
}
int leftSize = (amountOfStudents / 2);
int rightSize = (amountOfStudents - leftSize);
Student leftArr[5500], rightArr[5500];
for (int i = 0; i < leftSize; i++) {
leftArr[i] = studentsArray[i];
}
for (int i = 0; i < rightSize; i++) {
rightArr[i] = studentsArray[i + leftSize];
}
mergeSort(leftArr, leftSize);
mergeSort(rightArr, rightSize);
merge(rightArr, leftArr, studentsArray, rightSize, leftSize);
}
Ok, I think this should do what you want. It assumes that Student and BASE_COUNT have been defined:
#include <stdlib.h>
#include <stdio.h>
void merge(Student studentsArr[],
int leftSize, int rightSize,
Student scratchArr[])
{
Student *leftArr = studentsArr;
Student *rightArr = studentsArr + leftSize;
int leftIx = 0, rightIx = 0, mergeIx = 0, ix;
while (leftIx < leftSize && rightIx < rightSize) {
long gradeLeft = strtol(leftArr[leftIx].grade, NULL, BASE_COUNT);
long gradeRight = strtol(rightArr[rightIx].grade, NULL, BASE_COUNT);
if (gradeLeft <= gradeRight) {
scratchArr[mergeIx++] = leftArr[leftIx++];
}
else {
scratchArr[mergeIx++] = rightArr[rightIx++];
}
}
while (leftIx < leftSize) {
scratchArr[mergeIx++] = leftArr[leftIx++];
}
// Copy the merged values from scratchArr back to studentsArr.
// The remaining values from rightArr (if any) are already in
// their proper place at the end of studentsArr, so we stop
// copying when we reach that point.
for (ix = 0; ix < mergeIx; ix++) {
studentsArr[ix] = scratchArr[ix];
}
}
void mergeSortInternal(Student studentsArray[],
int amountOfStudents,
Student scratchArr[])
{
if (amountOfStudents <= 1) {
return;
}
int leftSize = amountOfStudents / 2;
int rightSize = amountOfStudents - leftSize;
mergeSortInternal(studentsArray, leftSize, scratchArr);
mergeSortInternal(studentsArray + leftSize, rightSize, scratchArr);
merge(studentsArray, leftSize, rightSize, scratchArr);
}
#define MAX_ARR_SIZE 5500
void mergeSort(Student studentsArray[], int amountOfStudents)
{
if (amountOfStudents <= 1) {
return;
}
if (amountOfStudents > MAX_ARR_SIZE) {
fprintf(stderr, "Array too large to sort.\n");
return;
}
Student scratchArr[MAX_ARR_SIZE];
mergeSortInternal(studentsArray, amountOfStudents, scratchArr);
}
The top-level sort function is mergeSort, defined as in the original post. It declares a single scratch array of size MAX_ARR_SIZE, defined as 5500. The top-level function is not itself recursive, so this scratch array is only allocated once.

Create variable amount of struct objects inside a struct (C)

I haven't written any C for more than a decade, but here I am...
I want to be able to create and access the following items of a data structure:
FilterCoefficients[0].TargetSampleNum = 0;
FilterCoefficients[0].SourceWeights[0].Weight = 0.812;
FilterCoefficients[0].SourceWeights[0].SourceSampleNum = 0;
FilterCoefficients[0].SourceWeights[1].Weight = 0.153;
FilterCoefficients[0].SourceWeights[1].SourceSampleNum = 1;
FilterCoefficients[1].TargetSampleNum = 1;
FilterCoefficients[1].SourceWeights[0].Weight = 0.352;
FilterCoefficients[1].SourceWeights[0].SourceSampleNum = 0;
FilterCoefficients[1].SourceWeights[1].Weight = 0.721;
FilterCoefficients[1].SourceWeights[1].SourceSampleNum = 1;
[...]
The indices have to be dynamically allocated (amount of needed space changes during runtime). I am attempting to create said data structure with the following:
typedef struct SampleWeight_t
{
unsigned long SourceSampleNum;
double Weight;
} SampleWeight;
typedef struct FilterCoefficients_t
{
unsigned long TargetSampleNum;
SampleWeight* SourceWeights;
} FilterCoefficients;
However, I am having difficulties creating the structure. I am getting Break Point exceptions when malloc-ing or free-ing the structure.
FilterCoefficients* FilterCoefficients;
SampleWeight* SampleWeight;
FilterCoefficients = malloc(sizeof(FilterCoefficients) * target_width);
if (FilterCoefficients == NULL) {
//errhandler
}
for (int i = 0; i < target_width; i++) {
FilterCoefficients[i].SourceWeights = malloc(sizeof(SampleWeight) * (int)ceil(scalingFactorWidth * window_width)); // **exception usually here**
if (FilterCoefficients[i].SourceWeights == NULL) {
//errhandler
}
FilterCoefficients[i].TargetSampleNum = i;
for (int j = i - filter_width; j < i + filter_width; j++) {
FilterCoefficients[i].SourceWeights[j + filter_width].Weight = bilinear_filter(0.5 + scalingFactorWidth2 * (j - 0.5));
if (j > 0) {
FilterCoefficients[i].SourceWeights[j + filter_width].SourceSampleNum = j;
}
else {
FilterCoefficients[i].SourceWeights[j + filter_width].SourceSampleNum = 0;
}
}
}
for (int i = 0; i < target_width; i++) {
free(FilterCoefficients[i].SourceWeights); // **exception usually here**
}
free(FilterCoefficients);
Any help which is gonna point me to a solution is appreciated.
This compiles and executes. I had to simplify.
#include <stdio.h>
#include <stdlib.h>
typedef struct SampleWeight_t
{
unsigned long SourceSampleNum;
double Weight;
} SampleWeight;
typedef struct FilterCoefficients_t
{
unsigned long TargetSampleNum;
SampleWeight* SourceWeights;
} FilterCoefficients;
int main ( void) {
FilterCoefficients* FilterCoeff;
int window_width = 35;
int target_width = 55;
FilterCoeff = malloc(sizeof(FilterCoefficients) * target_width);
if (FilterCoeff == NULL) {
//errhandler
}
for (int i = 0; i < target_width; i++) {
FilterCoeff[i].SourceWeights = malloc(sizeof(SampleWeight) * window_width);
if (FilterCoeff[i].SourceWeights == NULL) {
//errhandler
}
FilterCoeff[i].TargetSampleNum = i;
for (int j = 0; j < window_width; j++) {
FilterCoeff[i].SourceWeights[j].Weight = i * j;
if (j > 0) {
FilterCoeff[i].SourceWeights[j].SourceSampleNum = j;
}
else {
FilterCoeff[i].SourceWeights[j].SourceSampleNum = 0;
}
}
}
for (int i = 0; i < target_width; i++) {
free(FilterCoeff[i].SourceWeights); // **exception usually here**
}
free(FilterCoeff);
}

Why is there segmentation fault(core dumped) in my multithreaded scheduling program in C

I have to write a multi-threaded program in C in which one thread displays working of FCFS Scheduling and the other shows SJF scheduling. Now if I run the two types of scheduling as separate C programs, I get no errors and the programs run smoothly. But when I put them in two different functions and use the concept of multi-threading, the terminal prints the error "Segmentation fault (core dumped)" Please help me out
#include <stdio.h>
#include <pthread.h>
void *fcfs(void *);
void *sjf(void *);
int pid[10],at[10],bt[10];
int pid1[10],at1[10],bt1[10];
void main()
{
pthread_t fcfsT,sjfT;
pthread_attr_t attr;
int lower = 0, upper = 20, count = 10;
int i;
for(i=0;i<10;i++)
{
pid[i]=i+1;
}
for (i = 0; i < count; i++) {
at[i] = (rand() % (upper - lower + 1)) + lower;
bt[i] = (rand() % (upper - lower + 1)) + lower;
}
for(i=0;i<10;i++)
{
pid1[i]=pid[i];
at1[i]=at[i];
bt1[i]=bt[i];
}
pthread_attr_init(&attr);
pthread_create(&fcfsT,&attr,fcfs, NULL);
pthread_create(&sjfT,&attr,sjf,NULL);
pthread_join(fcfsT,NULL);
pthread_join(sjfT,NULL);
}
void *fcfs(void *p)
{
int ct[10],a,wt[10],tat[10],i,j=0;
for (i = 0; i < 10; ++i)
{
for (j = i + 1; j < 10; ++j)
{
if (at[i] > at[j])
{
a = at[i];
at[i] = at[j];
at[j] = a;
a = bt[i];
bt[i] = bt[j];
bt[j] = a;
a = pid[i];
pid[i] = pid[j];
pid[j] = a;
}
}
}
ct[0]=at[0]+bt[0];
for(i=1;i<10;i++)
{
if(at[i]<ct[i-1])
ct[i]=ct[i-1]+bt[i];
else
ct[i]=at[i]+bt[i];
}
for (i = 0; i < 10; ++i)
{
for (j = i + 1; j < 10; ++j)
{
if (pid[i] > pid[j])
{
a = pid[i];
pid[i] = pid[j];
pid[j] = a;
a = at[i];
at[i] = at[j];
at[j] = a;
a = bt[i];
bt[i] = bt[j];
bt[j] = a;
a = ct[i];
ct[i] = ct[j];
ct[j] = a;
}
}
}
for(i=0;i<10;i++)
{
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
}
printf("PID\tAT\tBT\tCT\tTAT\tRT:\n\n");
for(i=0;i<10;i++)
{
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",pid[i],at[i],bt[i],ct[i],tat[i],wt[i]);
}
pthread_exit(0);
}
void *sjf(void *g)
{
int ct1[10],b,wt1[10],tat1[10],z,q=0,minimum,location,temp[3]={0,0,0};
for (z = 0; z < 10; ++z)
{
for (q = z + 1; q < 10; ++q)
{
if (bt1[z] > bt1[q])
{
b = bt1[z];
bt1[z] = bt1[q];
bt1[q] = b;
b = at1[z];
at1[z] = at1[q];
at1[q] = b;
b = pid1[z];
pid1[z] = pid1[q];
pid1[q] = b;
}
}
}
for (z = 0; z < 10; ++z)
{
for (q = z + 1; q < 10; ++q)
{
if (bt1[z] == bt1[q])
{
if(at1[q]<at1[z])
{
b = bt1[z];
bt1[z] = bt1[q];
bt1[q] = b;
b = at1[z];
at1[z] = at1[q];
at1[q] = b;
b = pid1[z];
pid1[z] = pid1[q];
pid1[q] = b;
}
}
}
}
minimum = at1[0];
for ( z = 1 ; z < 10 ; z++ )
{
if ( at1[z] < minimum )
{
minimum = at1[z];
location = z;
}
}
temp[0] = at1[location];
temp[1] = bt1[location];
temp[2] = pid1[location];
for(z=location;z>0;z--)
{
at1[z]=at1[z-1];
bt1[z]=bt1[z-1];
pid1[z]=pid1[z-1];
}
at1[0]=temp[0];
bt1[0]=temp[1];
pid1[0]=temp[2];
ct1[0]= at1[0]+bt1[0];
for(z=1;z<10;z++)
{
if(at1[z]>ct1[z-1])
ct1[z] = at1[z]+bt1[z];
else
ct1[z] = bt1[z]+ ct1[z-1];
}
for (z = 0; z < 10; ++z)
{
for (q = z + 1; q < 10; ++q)
{
if (pid1[z] > pid1[q])
{
b = pid1[z];
pid1[z] = pid1[q];
pid1[q] = b;
b = at1[z];
at1[z] = at1[q];
at1[q] = b;
b = bt1[z];
bt1[z] = bt1[q];
bt1[q] = b;
b = ct1[z];
ct1[z] = ct1[q];
ct1[q] = b;
}
}
}
for(z=0;z<10;z++)
{
tat1[z]=ct1[z]-at1[z];
wt1[z]=tat1[z]-bt1[z];
}
printf("pid1\tAT\tBT\tCT\tTAT\tRT:\n\n");
for(z=0;z<10;z++)
{
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",pid1[z],at1[z],bt1[z],ct1[z],tat1[z],wt1[z]);
}
pthread_exit(0);
}
In sjf(), if at1[0] is the minimum, location will never be initialized, thus the loop below could readily generate invalid addresses.
for(z=location;z>0;z--)
{
at1[z]=at1[z-1];
bt1[z]=bt1[z-1];
pid1[z]=pid1[z-1];
}
When you run in “process mode”, it is very likely that your initial stack is zero-filled, thus the flaws in your program were hidden. The initial thread stack may have other stale data in it.
As the comments point out, you should try and use some level of compiler diagnostic (and initiative) before SO. Everybody is willing to help, but you are expected to develop the skills to help out too.

Trying to find number of possible paths going through all points exactly once starting at the center of a 5x5 array. Diagonal movement is also allowed

#include <iostream>
using namespace std;
long long int acc = 0;
bool isValid(int x, int y, int m[5][5])
{
if(x > -1 && x < 5 && y > -1 && y < 5 && m[x][y] == 0)
{
return true;
}
else
{
return false;
}
}
void start(int x, int y, int m[5][5],int count)
{
if(isValid(x,y,m))
{
count++;
if(count == 25)
{
acc++;
}
else
{
m[x][y] = 1;
start(x+1,y,m,count);
start(x-1,y,m,count);
start(x,y+1,m,count);
start(x,y-1,m,count);
start(x+1,y+1,m,count);
start(x-1,y-1,m,count);
start(x+1,y-1,m,count);
start(x-1,y+1,m,count);
}
}
}
int main()
{
int map[5][5];
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
map[i][j] = 0;
}
}
start(2,2,map,0);
cout<<acc;
}
or another code which i tried
#include <iostream>
#include <vector>
using namespace std;
long long int acc = 0;
class Point
{
public:
int row;
int column;
int count;
int map[5][5];
};
vector<Point> pts;
bool isValid(Point *b)
{
if(b->column > -1 && b->column <5 && b->row > -1 && b->row < 5 && b->map[b->column][b->row] != 1)
{
return true;
}
else
{
return false;
}
}
void start(vector<Point> A)
{
if(A.size() == 0)
{
cout<<acc;
}
Point *temp = &A.back();
A.pop_back();
if(isValid(temp))
{
temp->map[temp->column][temp->row] = 1;
temp->count = temp->count + 1;
if(temp->count == 25)
{
acc++;
}
else
{
Point *p = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
p->map[i][j] = temp->map[i][j];
}
}
p->column = temp->column + 1;
p->row = temp->row;
p->count = temp->count;
A.push_back(*p);
Point *q = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
q->map[i][j] = temp->map[i][j];
}
}
q->column = temp->column - 1;
q->row = temp->row;
q->count = temp->count;
A.push_back(*q);
Point *r = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
r->map[i][j] = temp->map[i][j];
}
}
r->column = temp->column;
r->row = temp->row + 1;
r->count = temp->count;
A.push_back(*r);
Point *s = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
s->map[i][j] = temp->map[i][j];
}
}
s->column = temp->column;
s->row = temp->row - 1;
s->count = temp->count;
A.push_back(*s);
Point *t = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
t->map[i][j] = temp->map[i][j];
}
}
t->column = temp->column + 1;
t->row = temp->row + 1;
t->count = temp->count;
A.push_back(*t);
Point *u = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
u->map[i][j] = temp->map[i][j];
}
}
u->column = temp->column - 1;
u->row = temp->row - 1;
u->count = temp->count;
A.push_back(*u);
Point *v = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
v->map[i][j] = temp->map[i][j];
}
}
v->column = temp->column + 1;
v->row = temp->row - 1;
v->count = temp->count;
A.push_back(*v);
Point *w = new Point;
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
w->map[i][j] = temp->map[i][j];
}
}
w->column = temp->column - 1;
w->row = temp->row + 1;
w->count = temp->count;
A.push_back(*w);
}
}
cout<<A.size();
start(A);
}
int main()
{
pts.clear();
Point *p = new Point;
p->map[2][2] = 0;
p->column = 2;
p->row = 2;
p->count = 0;
cout<<p<<endl;
pts.push_back(*p);
start(pts);
}
the first runs for about 150 iterations and then outputs 0, to indicate no completepaths, which is definitely wrong.
the second seems an error in pointers and addresses, which i still cannot get my head around.

Having issues with pointers

I'm fairly new to programming and i'm having problem with pointers. My code below works with the exception for that my counts doesn't follow with my article number when i sort it. I probably need pointers to get this working but I don't know how.
Can anyone help me?
void printMenu(void)
{
printf("\nMENU:\n");
printf("(D)isplay the menu\n");
printf("(G)enerate inventory\n");
printf("(P)rint inventory\n");
printf("(L)inear search article\n");
printf("(B)inary search article\n");
printf("(I)nsertion sort inventory\n");
printf("B(u)bble sort inventory\n");
printf("(M)erge sort inventory\n");
printf("(Q)uit program\n");
}
void generateInventory(article inventory[], int noOfArticles,
int minArticleNumber, int maxArticleNumber, int maxNoOfArticles)
{
int i, j;
int idCount[] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
for (i = 0; i < noOfArticles; i++)
{
inventory[i].id = rand() % (maxArticleNumber - minArticleNumber + 1) +
minArticleNumber;
idCount[inventory[i].id - 1] = idCount[inventory[i].id - 1] + 1;
for (j = 0; j <= i; ++j)
{
if (idCount[inventory[i].id - 1] > 1)
{
inventory[i].id = rand() % (maxArticleNumber + minArticleNumber);
}
}
inventory[i].counts = rand() % maxNoOfArticles;
}
}
void printInventory(const article inventory[], int noOfArticles)
{
int i;
printf("\nINVENTORY\n");
printf("%7s %8s\n", "Article", "Count");
for (i = 0; i < noOfArticles; i++)
{
printf("%7d %8d\n", inventory[i].id, inventory[i].counts);
}
}
int getArticleId()
{
int id;
printf("\nGive article id: ");
scanf("%d", &id);
return id;
}
void printSearchResult(const article inventory[], int index)
{
if (index == -1)
{
printf("\nArticle not found\n");
}
else
{
printf("\nArticle id: %d\n", inventory[index].id);
printf("Article counts: %d\n", inventory[index].counts);
}
}
int linearSearchInventory(const article inventory[], int noOfArticles, int id)
{
int i = 0;
int index = -1;
while (index == -1 && i < noOfArticles)
{
if (id == inventory[i].id)
{
index = i;
}
i++;
}
}
int binarySearchInventory(const article inventory[], int noOfArticles, int id)
{
int index = -1;
int left = 0;
int right = noOfArticles - 1;
int middle;
while (index == -1 && left <= right)
{
middle = (left + right) / 2;
if (id == inventory[middle].id)
{
index = middle;
}
else if (id < inventory[middle].id)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return index;
}
void insertionSortInventory(article inventory[], int noOfArticles)
{
int i, j;
int next;
for (i = 1; i < noOfArticles; i++)
{
next = inventory[i].id;
j = i - 1;
while (j >= 0 && next < inventory[j].id)
{
inventory[j + 1].id = inventory[j].id;
j = j - 1;
}
inventory[j + 1].id = next;
}
}
void bubbleSortInventory(article inventory[], int noOfArticles)
{
int c, d, t;
for (c = 0; c < (noOfArticles - 1); c++)
{
for (d = 0; d < noOfArticles - c - 1; d++)
{
if (inventory[d].id > inventory[d + 1].id)
{
t = inventory[d].id;
inventory[d].id = inventory[d + 1].id;
inventory[d + 1].id = t;
}
}
}
}
void mergeSortInventory(article inventory[], int noOfArticles)
{
int temp[noOfArticles / 2];
int nLeft, nRight;
int i, iLeft, iRight;
if (noOfArticles > 1)
{
nLeft = noOfArticles / 2;
nRight = (int) ceil((double) noOfArticles / 2);
mergeSortInventory(inventory, nLeft);
mergeSortInventory(&inventory[noOfArticles / 2], nRight);
for (i = 0; i < nLeft; i++)
{
temp[i] = inventory[i].id;
}
i = 0;
iLeft = 0;
iRight = 0;
while (iLeft < nLeft && iRight < nRight)
{
if (temp[iLeft] < inventory[noOfArticles / 2 + iRight].id)
{
inventory[i].id = temp[iLeft];
iLeft = iLeft + 1;
}
else
{
inventory[i].id = inventory[noOfArticles / 2 + iRight].id;
iRight = iRight + 1;
}
i = i + 1;
}
while (iLeft < nLeft)
{
inventory[i].id = temp[iLeft];
i = i + 1;
iLeft = iLeft + 1;
}
}
}
If I'm correct in what you're asking, you want to keep the idCount array relational to the inventory array. I assume, since you're using article as a type that you've either typedef'd a variable to be an article, which would be pointless, or more likely you've built a struct of type article, then made an array of those structs, and called the array inventory.
If this is the case, then the most likely method of keeping them relational is to just include the count in the article struct.
There are methods of making the arrays relational without doing that, but they're pointless, because a simple four-line struct would do the trick, even if that struct was a wrapper around a different struct, or a header for another struct.
When sorting your records, you only assign the id member of your struct:
inventory[foo].id = inventory[bar].id;
You should assign the complete struct:
inventory[foo] = inventory[bar];
Just remember that temporaries must be of type article an not int so they allso can be assigne a complete struct and not only an id value

Resources