• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

mapping bytes to an array of structs

I have a struct defined like so:
struct A{
  int a;
  float b;

};

Open in new window

I have void * pointer to bytes of memory being returned by a function. I would like to map the bytes to an array of struct (my struct A) so that I can later loop through and print out the values.

I tried this:
struct A *download=(struct A*) malloc(sizeof(A)*numElements);
download=(struct A*) ft_returning_bytes();
if(download!=(struct A*)NULL{
   for(int k=0;k<numElements;k++){
        printf("%d %0.2f",download[k].a,dowload[k].b);
  }
}

Open in new window


it prints incorrect values which i am not expecting. How  can this be done in c++?
0
LuckyLucks
Asked:
LuckyLucks
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
Julian HansenCommented:
A few issues.

1. Is there a reason you are using malloc and not new - given this is c++
2. You are assigning memory to download - but the next line you are call ft_returning_bytes - which returns a pointer - the malloc'd memory is now lost as the pointer is overwritten by the return from ft_returning_bytes.
3. What does ft_returning_bytes look like? Without seeing how that is allocating memory we can't determine how to output the data.
0
 
LuckyLucksAuthor Commented:
ft_returning_bytes is glMapBuffer , a function that retruns from gpu.

"glMapBuffer maps to the client's address space the entire data store of the buffer object
            currently bound to target. The data can then be directly read and/or written relative to
            the returned pointer, depending on the specified access policy."

The data being returned is a contigous memory containing int,float,int,float,.....

I didn't want to create a struct for each int , float pair just to print it out. Is there an elegant way to print out the int,float pair without recreating a struct for every pair?

struct A *download;
download=(struct A*) ft_returning_bytes();
if(download!=(struct A*)NULL{
   for(int k=0;k<numElements;k++){
        printf("%d %0.2f",download[k].a,dowload[k].b);
  }
}

Open in new window

0
 
Julian HansenCommented:
Without seeing the code for ft_returning_bytes the latest code snippet you posted is correct.

Here is some code that illustrates the concept. As you can see the loop to dump the struct is the same as the code you have posted. If your code is not working we need to know more about ft_returning_bytes
// 28290116.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

struct A{
  int a;
  float b;

};

void * getBytes()
{
	struct A * pData = (struct A *)malloc(sizeof(struct A) * 10);
	int i;
	for(i=0;i<10;i++) {
		pData[i].a = i;
		pData[i].b = (float)(i*i/123.0);
	}
	return pData;
}

int _tmain(int argc, _TCHAR* argv[])
{
	struct A * pRet = (struct A *)getBytes();
	int i;

	if (pRet) {
		for (i=0;i<10;i++) {
			printf("I: %d = %d, A.a = %d, A.b = %f\n", i, pRet[i].a, pRet[i].b);
		}
	}

	return 0;
}

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sarabandeCommented:
glMapBuffer uses GLfloat and not 'float'. a 'float' in c/c++ normally I 32-bit while a GLfloat could be 64-bit (what is double in c++).

I don't have seen code where a glMapBuffer points to a pair of int and float. instead I saw code like

GLfloat * data = (GLfloat*) glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);

Open in new window


what lets me think that a glMapBuffer normally is a GLfloat array. can you post a hex output of the data in memory?

but even if it is really a pair, you should use types GLint and GLfloat for types and should make sure that the c structure is "packed" what means that the compiler didn't add some space between the integer and the float for to get the members properly aligned - say at 8-byte boundaries. the packing can be added as compiler option (depending on your compiler or ide).

Sara
0
 
LuckyLucksAuthor Commented:
GLfloat array  - GLfloat is just
typedef float GLfloat;


 can you post a hex output of the data in memory? - how can i do this in VS C++?

Also,how can i verify things aren't being packed? Where should i be looking?
0
 
phoffricCommented:
>> can you post a hex output of the data in memory? - how can i do this in VS C++?
See section 2.  Viewing Memory Regions to see how to view data regions in various formats.
0
 
LuckyLucksAuthor Commented:
I also want to award some pts to "by: sarabandePosted on 2013-11-11 at 07:46:30ID: 39638561" .

The memory chunk returned is being padded by an extra 8 bytes per row of what i was expecting.
0
 
phoffricCommented:
>> I also want to award some pts to ...
Hit the RA button and make your request that way. A moderator will assist you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now