hi,

i have some data in variables of different types.(short, int, long, float, char, double). now i have to create one pointer, have to allocate 1mb memory and have to store one by one in that pointer. How to do this.. plz give some clues to start..

which type i have to create a pointer..

Recommended Answers

All 17 Replies

You need char* pointer, of course. If you want to get packed data, use memcpy() to move your data in the buffer. But you can't use these packed data directly (wrong alignment for double, for example).

char* pData = malloc(1000000);
size_t size;
...
short is[2];
double dub;
...
size = sizeof is;
memcpy(pData,is,size);
pData += size;
memcpy(pData,&dub,sizeof dub);
pData += sizeof dub;
... /* and so on */

Better form these mechanics into a proper function(s).
Don't forget to check the buffer overflow. No this code in my snippet but it's obviously how to do it (I hope;)))...

Thanks for reply. i will check this..
thank u very much

Some addition/correction: save the original pointer to buffer (to free buffer or write it etc). Increase a copy of this pointer value, or (better) maintain size_t type (or int) byte counter and use it in memcpy:

const size_t BUFSIZE = 1000000;
size_t datasize, nextpos, freepos = 0;
char* pData = malloc(BUFSIZE);
...
datasize = sizeof is;
nextpos = freepos + datasize;
if (nextpos > BUFSIZE) {
   /* Buffer overflow, don't write the next portion */
}
memcpy(pData+freepos,is,datasize);
freepos = nextpos;
...

i implemented the above said logic in my program. its giving 3 errors. i have mentioned errors below the code

const size_t BUFSIZE = 1048576;
size_t datasize, nextpos, freepos=0;
char *constDataArea=malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
	printf("OUT of memory\n");
	exit(0);				
}
datasize=sizeof(data.Short);
nextpos = freepos + datasize;
memcpy(constDataArea+freepos,data.Short,datasize);
freepos=nextpos;
if (nextpos > BUFSIZE)
{  
	printf("OUT of memory\n");
	exit(0);				
}
datasize=sizeof(data.Double);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,data.Double, datasize);
freepos=nextpos;

Errors:
1)Line 3: error c2440: 'intialising' :from void * to char * is not possible to change
2)Line 11: error c2664 : 'memcpy' : from short to const void * change is not possible
3)Line 20: error c2664 : 'memcpy' : from double to const void * change is not possible

memcpy requires the header file string.h
malloc requires the header file stdlib.h

You need to do some typecasting to remove those errors. Apparently you are compiling a c++ program, not C because in c++ the return value of malloc() must be typecast, but not in C. Here are the corrections -- I had to fake it on the data structure.

int main()
{
struct d data;
const size_t BUFSIZE = 1048576;
size_t datasize = 0, nextpos = 0, freepos=0;
char *constDataArea= (char*)malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
	printf("OUT of memory\n");
	exit(0);				
}
datasize=sizeof(data.Short);
nextpos = freepos + datasize;
memcpy(constDataArea+freepos, (char *)&data.Short,datasize);
freepos=nextpos;
if (nextpos > BUFSIZE)
{  
	printf("OUT of memory\n");
	exit(0);				
}
datasize=sizeof(data.Double);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos, (char *)&data.Double, datasize);
freepos=nextpos;
}

Expected o/p is 1 5 6. but i am getting 1 0 0.. what is the corrections to get the expected o/p???

char *constDataArea;
data.Bool=1;
data.Short=5;
data.Long= 6;


const size_t BUFSIZE = 1048576;
size_t datasize=0, nextpos=0, freepos=0;
constDataArea=(char *)malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Bool);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Bool, datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Short);
nextpos = freepos + datasize;					
memcpy(constDataArea+freepos,(char *)&data.Short,datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}						 
						
datasize=sizeof(data.Long);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Long, datasize);
freepos=nextpos;
								
printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[1], constDataArea[2]);

You need to do some typecasting to remove those errors. Apparently you are compiling a c++ program, not C because in c++ the return value of malloc() must be typecast, but not in C. Here are the corrections -- I had to fake it on the data structure.

I don't understand your reasoning here, Mr. Ancient Dragon. You knew that in C is not necessary to typecast the return of malloc. You knew something must be wrong with it. And then you go ahead and implemented a bad practice version for the OP.

In C code; that is source code file ending in .c, the typecast of malloc is an abused tendency that can hide adversed consequences.
If you include the proper stdlib.h, your compiler will know how to deal with the malloc function.
If you need to typecast after including the said header file something is not correct in your code.
Further reading in the matter here.

> Expected o/p is 1 5 6. but i am getting 1 0 0.. what is the corrections to get the expected o/p???

> printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[1], constDataArea[2]);
Think about what all those sizeof's you did earlier mean.
If in doubt, print the current value of nextpos and freepos after every call to memcpy

> if (nextpos > BUFSIZE)
If this is true, then your code will have already written past the end of the data.
By which time, it will be too late to do anything.

Expected o/p is 1 5 6. but i am getting 1 0 0.. what is the corrections to get the expected o/p???

char *constDataArea;
data.Bool=1;
data.Short=5;
data.Long= 6;


const size_t BUFSIZE = 1048576;
size_t datasize=0, nextpos=0, freepos=0;
constDataArea=(char *)malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Bool);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Bool, datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Short);
nextpos = freepos + datasize;					
memcpy(constDataArea+freepos,(char *)&data.Short,datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}						 
						
datasize=sizeof(data.Long);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Long, datasize);
freepos=nextpos;
								
printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[1], constDataArea[2]);

This code is correct. we will get expected o/p. but the printf of last statement is wrong.
so we have to replace that printf as follows.

printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[4], constDataArea[6]);

because bool is of type int, so it takes 4 bytes. the next elemet will store from constDataArea[4] and again Short variable is of type short(2 bytes). so the next element will start from constDataArea[6].

I don't understand your reasoning here, Mr. Ancient Dragon. You knew that in C is not necessary to typecast the return of malloc. You knew something must be wrong with it. And then you go ahead and implemented a bad practice version for the OP.

C compilers do not complain about the return value of malloc() as the op reported. Only c++ compilers do that, which is why I made that statement. So I assumed the op really intended to compile the code with c++, not c. Otherwise I agree completly with your comments. Had the op compiled the program as *.c file this would not have been an issue.

In the following code, we will get the output, and the data stores as follows.
actually i am getting dynamic input data..
first char is stored at 0th position
second long is stored at 1st position, because size of char is 1
third short is store at 5th position, because size of long is 4.

Actually what i want is
i have to leave 3 spaces after oth position, have to strore at 4th position
i have to leave 1 space after 5th position, have to store at 6th position


i.e
for long & float:
I have to store at 0,4,8,12,16...............

For short
I have to store at 0,2,4,6,8............

for double
i have to store at 0,8,16,24.........

for char
i have to store at 0,1,2,3,4,.........

How to change the code to get the output in the above indexes.......??

char *constDataArea;
data.Short=5;
data.Long= 6;
data.Char=1;

const size_t BUFSIZE = 1048576;
size_t datasize=0, nextpos=0, freepos=0;
constDataArea=(char *)malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Char);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Char, datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Long);
nextpos = freepos + datasize;					
memcpy(constDataArea+freepos,(char *)&data.Long,datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}						 
						
datasize=sizeof(data.Short);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Short, datasize);
freepos=nextpos;
								
printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[1], constDataArea[5]);

In the following code, we will get the output, and the data stores as follows.
actually i am getting dynamic input data..
first char is stored at 0th position
second long is stored at 1st position, because size of char is 1
third short is store at 5th position, because size of long is 4.

Actually what i want is
i have to leave 3 spaces after oth position, have to strore at 4th position
i have to leave 1 space after 5th position, have to store at 6th position


i.e
for long & float:
I have to store at 0,4,8,12,16...............

For short
I have to store at 0,2,4,6,8............

for double
i have to store at 0,8,16,24.........

for char
i have to store at 0,1,2,3,4,.........

How to change the code to get the output in the above indexes.......??

char *constDataArea;
data.Short=5;
data.Long= 6;
data.Char=1;

const size_t BUFSIZE = 1048576;
size_t datasize=0, nextpos=0, freepos=0;
constDataArea=(char *)malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Char);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Char, datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Long);
nextpos = freepos + datasize;					
memcpy(constDataArea+freepos,(char *)&data.Long,datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}						 
						
datasize=sizeof(data.Short);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Short, datasize);
freepos=nextpos;
								
printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[1], constDataArea[5]);

i have changed some corrections in the above code. is the following programming is good or please suggest me some other good idea.

char *constDataArea;
data.Short=5;
data.Long= 6;
data.Char=1;

const size_t BUFSIZE = 1048576;
size_t datasize=0, nextpos=0, freepos=0;
constDataArea=(char *)malloc(BUFSIZE);
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Char);
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Char, datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}
datasize=sizeof(data.Long);
if(freepos==1 || freepos%4==1)
{
     freepos+=3;
}
else if(freepos==2 || freepos%4==2)
	{
	freepos+=2;
	}
	else if(freepos==3 || freepos%4==3)
	{
	freepos+=1;
	}
nextpos = freepos + datasize;					
memcpy(constDataArea+freepos,(char *)&data.Long,datasize);
freepos=nextpos;
	
if (nextpos > BUFSIZE)
{  
printf("Out of memory\n");
exit(0);
}						 
						
datasize=sizeof(data.Short);
if(freepos%2==1)
{
freepos+=1;
}
nextpos=freepos+datasize;
memcpy(constDataArea+freepos,(char *)&data.Short, datasize);
freepos=nextpos;
								
printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[4], constDataArea[6]);

if i wrote for double in the same way, the program becomes too big right.. how minimize the code..

Well the first thing would be to learn some indentation and spacing skills, so that the code is more readable.
Eg

char *constDataArea;
    data.Short=5;
    data.Long= 6;
    data.Char=1;

    const size_t BUFSIZE = 1048576;
    size_t datasize=0, nextpos=0, freepos=0;

    constDataArea=(char *)malloc(BUFSIZE);

    if (nextpos > BUFSIZE)
    {  
        printf("Out of memory\n");
        exit(0);
    }
    datasize=sizeof(data.Char);
    nextpos=freepos+datasize;
    memcpy(constDataArea+freepos,(char *)&data.Char, datasize);
    freepos=nextpos;
    
    if (nextpos > BUFSIZE)
    {  
        printf("Out of memory\n");
        exit(0);
    }
    datasize=sizeof(data.Long);

    if(freepos==1 || freepos%4==1)
    {
        freepos+=3;
    }
    else if(freepos==2 || freepos%4==2)
    {
        freepos+=2;
    }
    else if(freepos==3 || freepos%4==3)
    {
        freepos+=1;
    }

    nextpos = freepos + datasize;					
    memcpy(constDataArea+freepos,(char *)&data.Long,datasize);
    freepos=nextpos;
    
    if (nextpos > BUFSIZE)
    {  
        printf("Out of memory\n");
        exit(0);
    }						 
    
    datasize=sizeof(data.Short);
    if(freepos%2==1)
    {
        freepos+=1;
    }
    nextpos=freepos+datasize;
    memcpy(constDataArea+freepos,(char *)&data.Short, datasize);
    freepos=nextpos;
    
    printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[4], constDataArea[6]);

> How to change the code to get the output in the above indexes.......??
Very easy nextPos = ( currentPos + currentDataSize + nextDataSize ) % nextDataSize

Well the first thing would be to learn some indentation and spacing skills, so that the code is more readable.
Eg

char *constDataArea;
    data.Short=5;
    data.Long= 6;
    data.Char=1;

    const size_t BUFSIZE = 1048576;
    size_t datasize=0, nextpos=0, freepos=0;

    constDataArea=(char *)malloc(BUFSIZE);

    if (nextpos > BUFSIZE)
    {  
        printf("Out of memory\n");
        exit(0);
    }
    datasize=sizeof(data.Char);
    nextpos=freepos+datasize;
    memcpy(constDataArea+freepos,(char *)&data.Char, datasize);
    freepos=nextpos;
    
    if (nextpos > BUFSIZE)
    {  
        printf("Out of memory\n");
        exit(0);
    }
    datasize=sizeof(data.Long);

    if(freepos==1 || freepos%4==1)
    {
        freepos+=3;
    }
    else if(freepos==2 || freepos%4==2)
    {
        freepos+=2;
    }
    else if(freepos==3 || freepos%4==3)
    {
        freepos+=1;
    }

    nextpos = freepos + datasize;					
    memcpy(constDataArea+freepos,(char *)&data.Long,datasize);
    freepos=nextpos;
    
    if (nextpos > BUFSIZE)
    {  
        printf("Out of memory\n");
        exit(0);
    }						 
    
    datasize=sizeof(data.Short);
    if(freepos%2==1)
    {
        freepos+=1;
    }
    nextpos=freepos+datasize;
    memcpy(constDataArea+freepos,(char *)&data.Short, datasize);
    freepos=nextpos;
    
    printf("const area:%d\t%d\t%d\t",constDataArea[0], constDataArea[4], constDataArea[6]);

> How to change the code to get the output in the above indexes.......??
Very easy nextPos = ( currentPos + currentDataSize + nextDataSize ) % nextDataSize

i will not get next datasize, because i used unions. only one data will get at one time..

Huh?

If you don't know the size of the data at the time you want to add the data to the buffer, then you're stuffed.

But since you do, then it's easy to fix the alignment of the current position.

I venture a remark:
what for this long-drawn-out proceedings with 1M buffer, clumsy and non-portable allocations, byte transfer petty intrigues etc?
More effective code? No.
More beautiful code? No.
More clear code? No.
Portability? No.
What's a real program case produced all this storm in a teacap?
The original post theme looks as if binary serialization buffer wanted, but now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.