Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8886133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:29:02+00:00 2026-06-14T21:29:02+00:00

I have been searching along for a way to compress, using the zlib library

  • 0

I have been searching along for a way to compress, using the zlib library (and the function compress) a struct containing float vars.

Every example I saw are showing how to compress a string, specifically an unsigned char*.

My struct is an easy one :

struct Particle{
    float x;
    float y;
    float z;
};

And I am calling the compress function as below :

uLong initSize = sizeof(Particle);
uLongf destSize = initSize * 1.1 + 12;
Bytef *dataOriginal = (Bytef*)malloc( initSize );
Bytef *dataCompressed = (Bytef*)malloc( destSize );

Particle p;
memset( &p, 0, sizeof(Particle) );

p.x = 10.24;
p.y = 23.5;
p.z = 7.4;

memcpy( dataOriginal, &p, sizeof(p) );
compress( dataCompressed, &destSize, dataOriginal, initSize );

But when I try to uncompress my data to see what inside, I can’t get back to my initial float value :

Bytef *decomp = (Bytef*)malloc( initSize );
uncompress( decomp, &initSize, dataCompressed, destSize );

for( int i = 0 ; i < initSize ; i++ ){
    std::cout << (float)decomp[i] << std::endl;
}

If anyone have a solution to this problem, I’m on it since 2 days now…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:29:04+00:00Added an answer on June 14, 2026 at 9:29 pm

    You would need to copy the decompressed data back into the Particle struct, just like you copied it out in the first place. (Or you could just use casts instead of copies). Then you will recover the original floats in the struct. Whatever it is you think you’re doing with ‘decomp[i]` doesn’t make any sense.

    However there are several problems with this. First, this is only assured to work on the same machine, with the same compiler, and even then only within the same version of the compiler. If a different compiler or different version chooses to align the structure differently, then the compressed data will not be transferrable between the two. If there is a different representation of floats between different machines, the compressed data will not be transferrable.

    Furthermore, you will not get any compression when compressing three floats. I presume that this is just a prelude to compressing a large array of such Particle structs. Then maybe you’ll get somewhere with this.

    Better would be to first convert the floats to the precision needed as integers. You should know the range and the useful number of bits for your application. This will compress before even using compress(), by using only the number of bits needed as opposed to 32 per float. Then convert those integers portably to a series of bytes with shift operations. You can then also apply differencing to successive Particles (e.g. x1-x2, y1-y2, z1-z2), which might improve compression if there is a correlation between successive Particles.

    By the way, instead of * 1.1 + 12, you should use compressBound(), which does exactly what you want in a way that is assured by the zlib library for future versions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching for this for the past few days and every tutorial
I have been searching a lot for the fastest way to read and write
Have been searching how to convert a dictionary to a string. But the results
I have been searching for a command that will return files from the current
I have been searching in stackoverflow for a question similar to mine because it
I have been searching several days for an example of how to make a
I have been searching for a solution to this and so far found nothing
I have been searching for quite a bit now but I have never been
I have been searching for how to create an animated widget. I have found
I have been searching for and have not located (I fear it doesn't exist)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.