View Full Version : C problem
I have a struct:
typedef struct Tfiledata Filedata;
struct Tfiledata{
int copy[1024];
};
Bytes will be read from file and stored into the copy array. My problem is that I don't know how to extend the array if there are more than 1024 characters. Any ideas?
Mr. Grapes
07-12-10, 16:01
you can't
since the array 'copy' is defined as 1024 in length in the typedef, you can't dynamically extend it.
('C' is a cruel mistress like that)
Solution coming up
what you need to do is declare copy as an int pointer
and keep a length item in the struct
struct Tfiledata{
int * pCopy;
int iLength;
};
then at implementation, allocate memory as you need it.
http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/
so if you need it 50 wide array
FileData fd;
fd.pCopy = (int*)malloc(sizeof(int) * 50);
fd.iLength = 50;
//do stuff
//remember to deallocate memory when you're done
free(fd.pCopy);
then whenever you need to extend/resize the buffer, just malloc a new buffer of the new size, assign it and reset the length, free ing the old one
I'll leave that as an exercise for you to figure out...
Ok. My suggestion is this. Dont have a fixed array. do the following:
struct TFileData
{
char *ptr_to_data;
};
Then, when you create your object, do this (you need to know the size of the data you are going to use btw)
struct TFileData *CreateFileData(int datasize)
{
struct TFileData *data = (struct TFileData *)malloc(sizeof(struct TFileData) + datasize);
data->ptr_to_data = ((char *)data) + sizeof(struct TFileData);
}
Then you can memcpy into data->ptr_to_data at your heart's desire.
If your datasize is number of elements, remember to multiply by the sizeof the element.
We've been given a header a file to use and thats where the struct is defined so I don't think I can change it.
Mr. Grapes
07-12-10, 16:17
in that case if there are more than 1024 elements, and you can't change the struct, you'll have to use multiple instances of the struct and keep a list of them
Yeah, thats what I've been trying. So looks like I'll continue down that route. Thanks.
Mr. Grapes
07-12-10, 16:20
the only other thing to do, is use the first element as a pointer to another dynamically allocated array (though this is really hacky and not to be used in real world production code. generally int's can be cast as pointers and vice versa, but it would be veeeeery platform dependant and not good practice at all)
something like:
int * pTmp = malloc(sizeof(int) * your_size);
fd.copy[0] = (int)pTmp;
// blah blah
int * pAnother = (int *)fd.copy[0];
//use pAnother as you would any array pointer
use only as an example that it can be done, but should not be.
declaring a new struct would be safer
If this is pure C... hmm. If its C++, inherit from the struct and do cunning things :) I should find out if you CAN amend the struct or not.
Mr. Grapes
07-12-10, 16:28
OP stated C, so guessing pure C.
but yeah, C++ then a load of extra options are open to you.
if you have been shown how to do linked lists, use the struct to make a linked list of the data
(ok, I'm done answering homework questions :) )
Got it working using an array just now (will change this to lists later).
Basically it it checks if the current character number is divisible by 1024 (or mod 1024) then creates a new Filedata to store the next 1024 bytes and adds it to the array.
Powered by vBulletin® Version 4.1.12 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.