Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, below code is for checking transaction id is getting duplicate or not(scenario).
but from the starting run time execution itself getting core dumb. pls help me.

#include<iostream>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
using namespace std;
pthread_mutex_t g_TxIdMutex;
long g_TxnSeq = 0;

long GetTxnId()
{
   long l_CurrTime = time(NULL);
   pthread_mutex_lock(&g_TxIdMutex);
   int l_TxnSeq = (g_TxnSeq = (g_TxnSeq+1)%100);
   pthread_mutex_unlock(&g_TxIdMutex);
   return (l_CurrTime%100000)*100+l_TxnSeq;
}

void print(long a, long b)
{
   printf("Matching is found for a : %ld and b : %ld\n", a, b);
}

int main()
{
   int j=0;
   cout<<__LINE__<<endl;
   long arr[100000000000];
   for(int i=0;i<10000000000;i++)
   {
      arr[i] = GetTxnId();
      j = 0;
      while(1)
      {
         if(i==j)
            break;
        if(arr[i] == arr[j])
            print(arr[i], arr[j]);

         ++j;
      }
   }
}


What I have tried:

Core dumb issue and code buck analysis
Posted
Updated 24-Nov-21 6:08am
Comments
Richard MacCutchan 23-Nov-21 7:47am    
What is your question?
Stefan_Lang 24-Nov-21 10:23am    
"Core dumb issue" => Why isn't the core more intelligent? ;-p
Richard MacCutchan 24-Nov-21 10:38am    
:)

Quote:
long arr[100000000000];
On a 64-bit machine, your program is asking the OS for about 800 GB of memory.
Could you spot the problem?
 
Share this answer
 
It's not "core dumb", it's "core dump" - and it's a map of the memory your app was using when the problem was found.
And ... your loop is way too big: 2,147,483,648 is the largest number you can store in an integer, and you are trying to store 10,000,000,000. When you exceed 2,147,483,648 you don't get 2,147,483,649 - you get a negative value and your app crashes when you try to use it as an array index.

Declaring an array as long makes each individual element bigger, but it doesn't mean that the index variable automatically becomes long as well ...
 
Share this answer
 
When you compile programs, particularly when learning, you should turn on warnings to help you diagnose problems. If you had you would probably get a warning about the following line of code
C++
for(int i=0; i<10000000000; i++)
for which I get the warning that the comparison i < 10000000000 is always true due to the range of i. So that would be one clue.
Further to what CPalini has said in Solution 1, the reason you are getting a core dump is that you've asked for the 80 GB on the stack not on the heap (i.e. using malloc()). On Windows the maximum stack size is usually about 1 MB, whereas on Linux the default stack size is 8MB. So even though the program compiles, when you try to run it, it runs into a stack size violation and terminates. If you were to use malloc() to try to allocate memory to your program it might work - as long as you're on a 64-bit OS. A 32 Bit OS can only allocate 4 GB per process. Even if you're on a 64-bit system, malloc might return a good value for an 80GB data segment, but you still might get a program crash when trying to access all data elements, unless you have at least 80GB of RAM+swap - which even today is rare for a user workstation.
 
Share this answer
 
Multiple issues and code smells:
- mixing long and int in calculations
- array too large
- array size does not match loop count
- using magical numbers
- using type int instead of size_t for array index values
- mixing iostream and printf
- printing to console at the core of heavy looping
- besides, your call to the print function will always print two identical numbers
- using mutex locks without using threads

I'd also add using classical loop iteration rather than range loops, but I admit that it isn't trivial to reform that particular nested loop.

Several of these issues should be indicated by the compiler. Ignore them at your own peril, but don't be surprised if your program crashes if you ignore these warnings!

On a sidenote: if you want 'random' transaction ids, use std::rand() or boost::random(). The function time() is not remotely good enough, and adding a counter doesn't change that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900