Solved

mapping bytes to an array of structs

Posted on 2013-11-09
9
254 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 55

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 55

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 33

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
C++ Class Serialization, Encapsulation and Formatted Output 8 80
designing in object programming 12 94
find Speed using GPS latitude & longitude 4 47
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 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