Thread: I use stupid thread titles.

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Exclamation I use stupid thread titles.

    Code:
    long double *v[1000];
    const int ind =9000000;
    main()
    {
    	for(int s=1;s<10000;s++)
    	{
    	
    		v[s]=new long double[ind/s];
    		if(v[s]) 
    		{
    			cout<<  v[s]  <<endl;
    			for(int i=0;i<ind/s;i++) *(v[s]+i)=(long double)i;
    			cout<<*(v[s]+s)<<endl;
    			
    		}
    	}
    	
    	
    	return 0;
    }
    Why this code generates error?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You are trying to allocate 9999 times 4.5 million doubles, propably 8 bytes each.
    Do you have that much memory in your box ?
    Kurt
    EDIT: sorry I read [ind/s]; as ind / 2. But it's still a lot.
    Last edited by ZuK; 01-25-2006 at 11:45 AM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by siavoshkc
    Code:
    long double *v[1000];
    
    
    for(int s=1;s<10000;s++)
    {
        v[s]=new long double[ind/s];
    Why this code generates error?
    A big problem I would say is that once s is equal to or bigger than 1000 you are accessing unsafe memory areas. Did you mean for that to be 1000 and not 10000?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What is it?

    Blood... Jill look around see if you can find any other clues. I'll be examining this. Hope this is not Chris' blood.

    Oh and other than memory issues, there is no reason for that to generate an error. Unless of course you're talking about an error similar to "'cout' undeclared (first used yada-yada)"
    Sent from my iPadŽ

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I get a strange error when I run the program: An error window with two button but no text on it. I know for big memory allocations we need Win32 functions but my program should not generate error.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just do the math on how much memory you allocate!
    Code:
    #include <iostream>
    using namespace std;
    const int ind =9000000;
    int main()
    {
      unsigned long sum = 0;
      for(int s=1;s<10000;s++)
      {
        sum += ind/s;
      }
      cout << sum << " doubles would be allocated\n";
      cout << "Taking a total of " << sum*sizeof(double) << " bytes" << endl;
    
      return 0;
    }
    
    $ ./a.exe
    88082647 doubles would be allocated
    Taking a total of 704661176 bytes
    Just 0.75GB, no biggie

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It doesn't matter how much memory it will take. Because if there is not enough memory, new simply should return a null pointer. And if the memory is big enough, again there should be no problem.
    As I know if you use new for even 2GB, then Windows should use it's swap(page) file to give the program enough memory.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Check this from the FAQ it tells you about trying to allocate more memory than you have.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Let me ask a question:
    Please tell me what error this program generates on your PCs if any?
    I think there is a problem with my system.
    Last edited by siavoshkc; 01-26-2006 at 04:45 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    This generates no visible errors for me.
    Here is the output.


    88082647 doubles would be allocated
    Taking a total of 704661176 bytes

    -EDIT-

    I just realised that is the second piece of code I tested.

    Here is a little bit of the first piece of code.

    00490040
    1
    04940040
    2
    06BA0040
    3
    08290040
    4
    093C0040
    5
    0A180040
    6
    0AD00040
    7
    0B6D0040
    8
    0BF70040
    9
    0C720040
    10
    0CE00040
    11
    0D440040
    12
    0DA00040
    13
    0DF50040
    14
    0E440040
    15
    0E8E0040
    16
    0ED30040
    17
    0F140040
    18
    0F520040
    19
    0F8C0040
    20
    0FC30040
    21
    0FF80040
    22
    102A0040
    23
    105A0040
    24
    10880040
    25
    10B40040
    26
    10DF0040
    27
    11080040
    28
    11300040
    29
    11560040
    30
    117B0040
    31
    119F0040
    32
    11C20040
    33
    11E40040
    34
    12050040
    35
    12250040
    36
    12440040
    37
    12620040
    That is just a little of the output.

    It just keeps going allocating memory.

    I didn't let it run long enough to see if it will make my system crash/freeze. But I have 512MB ram, you can figure out how much space the program tries to allocate by looking at it, then figure out if your system has enough.
    Last edited by John_; 01-26-2006 at 04:57 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Please tell me what error this program generates on your PCs if any?
    I don't need to run anything to tell you that

    for(int s=1;s<10000;s++)
    Won't fit into
    long double *v[1000];

  12. #12
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Think of it this way:

    You're saying:

    10000-1000=0.

    But it doesnt. You are moving 9000 places past the amount of memory you alocated. And you should never alocate so much memory, especialy if you dont need it.

    Mmm, Imagine this:

    I'm a surgeon, I have a line where I'm suposed to end the cut, but the nurse drew a line 9x longer than what you were suposed to cut. Sence you dont know where the end is suposed to be, you cut through the whole line. Now, you end up cutting an organ and killing the patient.

    The same idea applies to your program, its trying to cut somewhere that isint where its suposed to cut. Its cutting into large chunks of memory that you shouldent acess. So, you end up killing the program.

    Hope that makes sence.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Lightbulb

    Code:
    long double *v[10000];
    const int ind =10000000;
    main()
    {	
    	int nfaloc=0;
    	int sum=0;
    	
    	for(int s=1;s<10000;s++)
    	{
    	
    		v[s]=new (nothrow) long double[ind/s];
    		if(v[s]) 
    		{
    			
    			for(int i=0;i<ind/s;i++) *(v[s]+i)=(long double)i;
    			
    			sum+=(ind/s);
    			int fortest=0;
    			cout<<  ((ind/s)*8)/(1024) 
    				<<" KBs at "<< v[s] 
    				<<"  Test: "; 
    			for(i=0;i<ind/s;i++) {
    				if(*(v[s]+i)!=(long double)i) {fortest=1;break;}
    				
    			}
    			if(fortest==1) cout<<"Failed. ";
    			else cout<<"OK. ";
    			cout << "Total: "<<(sum*8)/(1024*1024)<<" MBs allocated and tested."<<endl;
    		}
    		else
    			nfaloc++;
    
    	}
    	
    	cout<<nfaloc<<" Number of allocations failed!"<<endl;
    	cout<< "Press a key..."  <<endl;
    	getch();
    	return 0;
    }
    Set windows not to create page file and run it.
    Last edited by siavoshkc; 01-26-2006 at 07:25 AM. Reason: (nothrow)
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    On a proper compiler, new throws an exception when unable to allocate space. And for heaven's sake, use constants!
    Code:
    const int WHATEVER = 10000;
    long double *v[WHATEVER];
    	for(int s=1;s<WHATEVER;s++)
    Well, I corrected all errors (like your incorrect for-scoping) and ran it on my Linux box. (Without disabling swap - Linux's out-of-memory handler can be unpleasant.) Allocated 746 MBs just fine, although the swapping slowed it to a crawl, and it froze my computer for several seconds after exiting, while all the swapped-out applications were brought back in.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Wow, sorry, I wrote the code wrongly in the first post. It was 10000 not 1000. It was right in my own code. This is why I didn't understand you.:-(
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Need some help with truncation of thread titles
    By major_small in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-24-2006, 11:00 AM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  4. Thread confusion
    By pyrolink in forum C Programming
    Replies: 0
    Last Post: 01-29-2006, 09:42 PM
  5. Thread inside a thread ?
    By lsme in forum Linux Programming
    Replies: 3
    Last Post: 12-08-2004, 11:08 PM