Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

mapping bytes to an array of structs

Posted on 2013-11-09
9
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 600 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 59

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

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
 

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 1400 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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