Solved

mapping bytes to an array of structs

Posted on 2013-11-09
9
252 Views
Last Modified: 2013-11-22
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
Comment
Question by:LuckyLucks
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 total points
ID: 39636596
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
 

Author Comment

by:LuckyLucks
ID: 39637013
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
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 39637911
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
 
LVL 32

Expert Comment

by:sarabande
ID: 39638561
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:LuckyLucks
ID: 39663188
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
 
LVL 32

Accepted Solution

by:
phoffric earned 350 total points
ID: 39663717
>> 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
 

Author Comment

by:LuckyLucks
ID: 39669510
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
 
LVL 32

Expert Comment

by:phoffric
ID: 39669583
>> I also want to award some pts to ...
Hit the RA button and make your request that way. A moderator will assist you.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now