Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

mapping bytes to an array of structs

Posted on 2013-11-09
9
Medium Priority
?
262 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
8 Comments
 
LVL 60

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 60

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

782 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