Results 1 to 7 of 7

Thread: c++ binary writing of array issue

  1. #1
    Join Date
    Jun 2008
    Beans
    287

    c++ binary writing of array issue

    Hi all,

    Hopefully someone can shine some light on this. The following is a short code demo. It sets up three arrays. A large static one, a small dynamic one and a big dynamic one. When I try to write these out it always fails on the big array. Hence the output is:

    250000
    300000
    -1

    and the produced file varies in size around 560000 bytes.

    So why is the big array failing mid write? I can even put a loop around the small array and run it so that it writes out the same data. Is there limit on the maximum array that be cast or wrote in one block?

    Code:
    #include <fstream>
    #include <iostream> 
    using namespace std; 
     
     
    int main(int argc,char* argv[] )
    {
    	//Binary write file
    	ofstream OUT( "TESTY.bin" , ios::out | ios::binary);
    
    	char sbuf[250000];
    	for(int i=0;i<250000;i++)sbuf[i]=0;
    	
    	//Make small Dynamic Buffer
    	short* sdbuf=new short[25000];	
    	for(unsigned long i=0;i<25000;i++)sdbuf[i]=0;
    	
    	//Make Dynamic Buffer
    	short* dbuf=new short[250000];	
    	for(unsigned long i=0;i<250000;i++)dbuf[i]=0;
    	
    	//Make Static Buffer	
    
    	
    	//Binary writing
    	OUT.write( sbuf, sizeof(sbuf) );
    	cout<<OUT.tellp()<<endl;
    	
    	OUT.write( (char*) &sdbuf, 25000*sizeof(short) );
    	cout<<OUT.tellp()<<endl;
    		
    	OUT.write( (char*) &dbuf, 250000*sizeof(short) )<<endl;
    	cout<<OUT.tellp()<<endl;
    	
    	//Tidy up		
    	OUT.close();
    	if(sdbuf){delete[]sdbuf;sdbuf=0;}
    	if(dbuf){ delete[]dbuf; dbuf=0;}
    	return 0;
    }
    I suffer from web impatience. You have 5 seconds to amaze me.

  2. #2
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: c++ binary writing of array issue

    OUT.write( (char*) &sdbuf, 25000*sizeof(short) );


    OUT.write( (char*) &dbuf, 250000*sizeof(short) )<<endl;
    The addresses being provided to write () to obtain the data from is incorrect because what you are passing it is the address of the pointer to the arrays and not the arrays themselves. So since you are obviously going over the boundary in that respect since a pointer is much smaller than 50000 bytes your program is segfaulting. To fix the problem just get rid of the &s.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  3. #3
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: c++ binary writing of array issue

    Let's have a look at the following lines:

    Code:
    	//Binary writing
    	OUT.write( sbuf, sizeof(sbuf) );
    Here, you write 4 bytes on 32-bit machines and 8 bytes on 64-bit machines to disk, as "sbuf" is a pointer, which has size 4 or 8.

    Code:
    	OUT.write( (char*) &sdbuf, 25000*sizeof(short) );
    Here, you write 25000*sizeof(short) bytes to disk, starting with the position in memory that holds the pointer to sdbuf. Unless you want the address of a pointer (which does not make sense in this case), omit the "&".

    Code:
            OUT.write( (char*) &dbuf, 250000*sizeof(short) )<<endl;
    Same here. I hope that I'm not wrong here in any respect and also hope that this helps you!

  4. #4
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: c++ binary writing of array issue

    Quote Originally Posted by Zugzwang View Post
    Code:
    	//Binary writing
    	OUT.write( sbuf, sizeof(sbuf) );
    Here, you write 4 bytes on 32-bit machines and 8 bytes on 64-bit machines to disk, as "sbuf" is a pointer, which has size 4 or 8.
    But because sbuf is actually referring to a static array and not a dynamic array, I think the actual output of the sizeof () is 250000 isn't it?
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  5. #5
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: c++ binary writing of array issue

    Quote Originally Posted by Zugzwang View Post
    Let's have a look at the following lines:

    Code:
    	//Binary writing
    	OUT.write( sbuf, sizeof(sbuf) );
    Here, you write 4 bytes on 32-bit machines and 8 bytes on 64-bit machines to disk, as "sbuf" is a pointer, which has size 4 or 8.
    this is the only one which is correct as sbuf is an array and no pointer.
    sizeof works on array's (but beware of the implicit conversion to pointers).

  6. #6
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: c++ binary writing of array issue

    Quote Originally Posted by MadCow108 View Post
    this is the only one which is correct as sbuf is an array and no pointer.
    sizeof works on array's (but beware of the implicit conversion to pointers).
    Oh, ok, my fault: I mixed "sbuf" with "sdbuf".

  7. #7
    Join Date
    Jun 2008
    Beans
    287

    Re: c++ binary writing of array issue

    Thanks to all,

    You solved the problem and educated me in an amazingly fast time.
    I suffer from web impatience. You have 5 seconds to amaze me.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •