|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Vc 2008 Alternate to For Loop
Hi Everybody,
I am a newbie to this forum, i am saravnaan from india, i am having an array of values (integer values) about more than two hundred thousand(array size), so i want to check a value against that array of values, if i use the for loop, then the system will crashing, is there an alternative to the for loop which can work for thase large arrays. any help is appreciated. thank you. |
|
#2
|
||||
|
||||
|
You're asking the wrong question. Yes there are alternatives to the for loop (such as while). However, your real question should be, "why is my program crashing in the for loop?" For loops by themselves do not cause crashes. Usually, the cause of the crash has to do with people dereferencing array elements that do not exist.
Seeking an alternative to the for loop is not going to help you solve the crashing issue at all.
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Fishmonger, superior perl programmer of the month |
|
#3
|
|||
|
|||
|
vc alternate to for loop
Hi Scorpions4ever ,
Just test with 200,000 values in a for loop, and reply me, my whole system is hanged when i did this, and then the only way to retrieve is Restart the system. not only 200,000 values, i need more than that also. |
|
#4
|
||||
|
||||
|
Quote:
200K integers isn't all that large (roughly 1.6MB with 64 bit ints). Perhaps you should post some code so we can help you figure out where you're going wrong? Have you got a debugger?
__________________
My worst nightmare was a pointless infinite loop. Work in progress; don't poke the curmudgeon! http://www.odonahue.com/ |
|
#5
|
|||
|
|||
|
vc alternate to for loop
INT64 arr[200001];
INT64 itr; for(itr=0;itr<200000;itr++) { arr[itr]=itr; if(arr[itr]==199999) MessageBox(L"ss",L"ss",0); } Hi, this is the code i wrote, then the Stack Overflow error is came, give me any suggestion. Thanks. |
|
#6
|
||||
|
||||
|
Quote:
BTW put your codes in the code tags before posting them like I have done, and trust me you will be helped more often.Read how to post a question in the forum. I didn't use your code, but found no problem running the following in my VC++ 6.0, try it out: Code:
#include<stdio.h>
int main()
{
int arr[200001];
int itr=0;
for(itr=0;itr<200000;itr++)
{
arr[itr]=itr;
if (arr[itr]==199999) printf("almost there");
}
return 0;
}
Last edited by RAJ_55555 : October 29th, 2009 at 04:31 AM. Reason: OOPS! |
|
#7
|
||||
|
||||
|
Ah. Stack overflow is more specific than "system is crashing". If you are running VC++ 2008, then you should be able fire up the debugger when the program faults and get a peak at the call stack. Are you sure it hits this stack overflow in that loop? We may need to know what your compiler options are set to. In project properties, drill down to C/C++ General and disable the "Suppress Startup Banner" option, then rebuild your project and post the resulting build log.
|
|
#8
|
||||
|
||||
|
Where is INT64 declared and what is it defined as?
You really should post a complete program that reproduces this issue. |
|
#9
|
|||
|
|||
|
vc alternate to for loop
This is MFC based Dialog app.
this is the code: // AlternateDlg.cpp : implementation file // #include "stdafx.h" #include "Alternate.h" #include "AlternateDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg: oDataExchange(CDataExchange* pDX){ CDialog: oDataExchange(pDX);} BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CAlternateDlg dialog CAlternateDlg::CAlternateDlg(CWnd* pParent /*=NULL*/) : CDialog(CAlternateDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CAlternateDlg: oDataExchange(CDataExchange* pDX){ CDialog: oDataExchange(pDX);} BEGIN_MESSAGE_MAP(CAlternateDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDOK, &CAlternateDlg::OnBnClickedOk) END_MESSAGE_MAP() // CAlternateDlg message handlers BOOL CAlternateDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CAlternateDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CAlternateDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CAlternateDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CAlternateDlg::OnBnClickedOk() { INT64 arr[200001]; INT64 itr; for(itr=0;itr<200000;itr++) { arr[itr]=itr; if(arr[itr]==199999) MessageBox(L"ss",L"ss",0); } OnOK(); } I changed the Compiler Option what you specified, after that too the Stack Overflow error is coming. |
|
#10
|
||||
|
||||
|
You forgot the "code" tags. You also need to turn off smilies or emoticons or whatever they are called. You have a problematic loop termination condition. While logically you would expect the condition to be true as it runs over the loop, in reality you can wind up with subtle timing conditions that can lead to unexpected behavior. Try something like this:
Code:
void CAlternateDlg::OnBnClickedOk()
{
INT64 arr[200001];
INT64 itr;
for(itr=0;itr<200000;itr++)
{
arr[itr]=itr;
if(arr[itr]>=199999)
MessageBox(L"ss",L"ss",0);
}
OnOK();
}
__________________
Free code: http://sol-biotech.com/code/. Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html. Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html. It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it. --Me, I just made it up The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man. --George Bernard Shaw |
|
#11
|
||||
|
||||
|
Quote:
Hmm it says Stack Overflow error, 99.99% it's the programmers fault. #1 You're getting this error because you are using more memory than is/can be allocated to you, basically out of your scope. #2 If I were writing code that required me to have an array size of 2*10^5 then I am doing something wrong. A> it's contiguous block of memory. B> I would use some other data structure. #3 You are using INT64 that's 8 bytes. Now I am not saying you should change your type but definitely do not use arrays, for such large indexes. #4 Now this one is a shot in the dark, the reason you're getting this error is because 8*2*10^5 (the contiguous block) doesn't fit into the call stack. Now if you were to halve your array size it works prefectly. Try to keep the Size of array inversely proportional to size of data type of array! I rarely use arrays, use vector if you have to, I am pretty sure your application isn't time sensitive enough to worry about speed, precision is more important. -Mav -----------------EDIT ------------------------------ Typographical error ![]()
__________________
The only Verdict is Vengeance a Vendetta held as a Votive, not in Vain, for the Value and Veracity of such shall one day Vindicate the Vigilant and the Virtuous Mav RLZ AC/DC RLZ Last edited by IOI-RLZ : October 29th, 2009 at 02:53 PM. |
|
#12
|
||||
|
||||
|
Quote:
Just a heads up, if you say that your system crashes/hangs it gives the impression that something was working or is still working, and freezes up because of lack of resources, or is in deadlock. For future reference it's always better to have a test copy of your code that reproduces the exact response(if you can), in as few lines of code as possible. And not a bad idea to debug. More often than not you will solve your own problem yourself. -Mav -------------------EDIT -------------------------------- Quote:
![]() Last edited by IOI-RLZ : October 29th, 2009 at 02:51 PM. |
|
#13
|
|||
|
|||
|
Hi Buddies,
Thank you for your replies, I have done a small mistake with my code, actually INT64 arr[200000]; create an array of reference, not heap memory, thats what it couldn't allocate that much of memory, now i tried with 'new' operator to allocate memory for the array and now it is working fine for any size. below is the modified code: void CAlternateDlg::OnBnClickedOk() { INT64 *arr = new INT64; INT64 itr; arr = new INT64[50000000]; for(itr=0;itr<50000000;itr++) { arr[itr]=itr; if(arr[itr]==4999999) MessageBox(L"ss",L"ss",0); } OnOK(); } Once again thanks for your replies. |
|
#14
|
||||
|
||||
|
You got a whopping memory leak there, you forgot to delete the memory!
|
|
#15
|
||||
|
||||
|
At this rate you will run out of heap too.
-Mav |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Vc 2008 Alternate to For Loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|