so I have the following enum method in c:
enum enum_type GetInfo (int socket, unsigned char *data)
{
}
and at the api I can find this:
Received data is written to pointer *data....
So if I'm doing something like this:
unsigned char *data;
enum_type enum1;
enum1 = GetInfo (int socket, data);
I got an segmentation fault.
What's my problem?
Thanks,
Simon
Your problem is that you haven't allocated space for data but try to write to it. Do
unsigned char *data = malloc(sizeof(unsigned char) * MYBUFLENGTH);
and then pass data to GetInfo. At the end do not forget to
free(data);
Alternatively you could allocate space on stack (available in C99, some compilers support it as extension even with earlier versions of the C Standard)
unsigned char data[MYBUFLENGTH];
In this case you should not worry about memory management.
you need to allocate memory to store the data in.
for instance:
unsigned char data[10000]; /* allocate 10000 bytes */
enum_type enum1;
enum1 = GetInfo(socket, data);
If you don't understand what's going on, I recommend spending time to read up on pointers.
not sure how big the info is, but try the following
unsigned char data[512] = {0};
enum_type enum1;
enum1 = GetInfo (socket, data);
this makes sure, that data points to a valid memory address on the stack.
It's maybe because GetInfo wants to write to a buffer pointed by data, and you just pass the pointer without allocating any memory space. Allocate memory and point it to data like this:
// I assume you need 1000 bytes
data = (unsigned char*)malloc(1000*sizeof(unsigned char));
Related
Strait to the point.
I have a struct with a string, char and int.
The struct is created dynamically because i will need it in different parts of my program.
struct A
{
char staticString[20];
char* dynamicString;
char character;
int integer;
};
I know if i want to create a struct i call:
A example = (A)malloc(sizeof(A));
In order to populate the dynamicString and int i used:
example->dynamicString = (char*)malloc(sizeof(char*));
example->integer = (int)malloc(sizeof(int));
Unfourtanetly when i tried to populate staticString and char it didn't worked.
Don't even ask what was my code for those, i tried a lot of combinations from everywhere.
In addition to that can somebody show me examples how to write/read those values?
Thanks in advance.
First things first:
You're using C, and by the way you've defined the structure, you need to declare the pointer like so:
struct A *example;
Next, malloc returns a pointer, so you need to cast to a pointer (and not to a structure):
(struct A *)malloc(sizeof(struct A));
Secondly, I'm not sure why but hey:
- you're trying to dynamically allocate an int in the structure. As I said previously, malloc returns a pointer, so in your structure you need an int pointer like so "int *integer;"
- you're trying to allocate a dynamic string, however you're not doing it properly, here is what I think you want
example->dynamicString = (char *)malloc(sizeof(char) * 10);
Where 10 is the size of your dynamic string.
Edit:
you may also populate the integer in your struct statically or dynamically, but I think you intended the static approach:
example->integer = 123;
The dynamic approach would be (assuming you have int *integer in your struct):
example->integer = (int *)malloc(sizeof(int));
*(example->integer) = 123;
Every time you create a new struct the memory in the heap is set to size of :
sizeof(char)*20 + sizeof(char pointer) +sizeof(char)+ sizeof(int).
If you want to save a string that will be pointed to by your char pointer- then you ask for allocation in heap for the size of that string- and malloc returns the pointer to that memory allocation on heap.
So, you already have a space for your char array, char pointer, char and int that was allocated when you asked to make a new struct and do not need to allocate it again.
also, keep in mind malloc returns a pointer to the allocated place on the heap- so if you malloc(sizeof(int)) you get a pointer to a memory allocation for an int on the heap- which is pointed to by a int pointer Not an int.
good luck!
I have the following struct
struct NETWORK_ENDPOINT {
unsigned char Type;
unsigned char Protocol;
unsigned char IPv4[IPV4SIZE + 1];
unsigned int PortNumber;
unsigned char SocketIndex;
unsigned char RESERVED;
unsigned char *InboundData;
unsigned int InboundDataSize;
unsigned char *OutboundData;
unsigned int OutboundDataSize;
};
In the code I'm allocating with :
struct NETWORK_ENDPOINT *Endpoint = malloc(sizeof(struct NETWORK_ENDPOINT));
Then later in the code I'm allocating the OutboundData with.
Endpoint->OutboundData = malloc(20); // malloc() size may vary,
// but in the problem situation it is 20
Then I do :
memcpy(Endpoint->OutboundData, Data, 20);
Then the problem : From the debugger I can see that Endpoint is given address #0x1fd6, and the OutboundData is given address #0x1fca, so only 12 between. Shouldn't is be atleast 20 ?
The memcpy() function then will fill out in OutboundData ( can see in memory that data is correctly placed ), but once it passes 12 bytes, it will begin overwriting the start of the struct Endpoint, corrupting the Type and Protocol and half the IP, thereby making it useless afterwards.
anyone got any idea what I'm going wrong here ? Been working on this for days now, but whatever I try it does not fix this issue...
Have tried to increase the HEAP size, but it seems to stay at 12 bytes between the two memory locations no matter what I do..
OutboundData is given address #0x1fca, so only 12 between
Why you're interested in the address of OutboundData?
After malloc(), you should be checking the value of OutboundData, however, you won't be knowing the size of allocated memory thr' this.
Just to be clear, you're not copying to the address of OutboundData, rather , you are copying to the address pointed by OutboundData.
Then,
but once it passes 12 bytes, it will begin overwriting the start of the struct Endpoint, corrupting the Type and Protocol and half the IP, thereby making it useless afterwards.
No, it won't. The value and address of OutboundData are different and the value of OutboundData is used in memcpy().
IMHO, as long as
malloc() is success
Data is atleast of size 20 (for this case)
your memcpy() should work fine.
If I want to reduce malloc()s (espacially if the data is small and allocated often) I would like to allocate the pointer and pointee at once.
If you assume something like the following:
struct entry {
size_t buf_len;
char *buf;
int something;
};
I would like to allocate memory in the following way (don't care about error checking here):
size_t buf_len = 4; // size of the buffer
struct entry *e = NULL;
e = malloc( sizeof(*e) + buf_len ); // allocate struct and buffer
e->buf_len = buf_len; // set buffer size
e->buf = e + 1; // the buffer lies behind the struct
This could even be extende, so that a whole array is allocated at once.
How would you assess such a technuique with regard to:
Portability
Maintainability / Extendability
Performance
Readability
Is this reasonable? If it is ok to use, are there any ideas on how to design a possible interface for that?
You could use a flexible array member instead of a pointer:
struct entry {
size_t buf_len;
int something;
char buf[];
};
// ...
struct entry *e = malloc(sizeof *e + buf_len);
e->buf_len = buf_len;
Portability and performance are fine. Readability: not perfect but good enough.
Extendability: you can't use this for more than one member at a time, you'd have to fall back to your explicit pointer version. Also, the explicit pointer version means that you have to muck around to ensure correct alignment if you use it with a type that doesn't have an alignment of 1.
If you are seriously thinking about this I'd consider revisiting your entire data structure's design to see if there is another way of doing it. (Maybe this way is actually the best way, but have a good think about it first).
As to portability, I am unaware of any issues, as long as the sizes are found via suitable calls to sizeof(), as in your code.
Regarding maintainability, extendability and readability, you should certainly wrap allocation and de-allocation in a well-commented function. Calls to...
entry *allocate_entry_with_buffer();
void deallocate_entry_with_buffer(entry **entry_with_buffer);
...do not need to know implementation details of how the memory actually gets handled. People use stranger things like custom allocators and memory pools quite frequently.
As for speed, this is certainly faster than making lots of small allocations. I used to allocate whole 2D matrices with a similar strategy...
It should work, but in fact you are using a pointer for a useless indirection. Windows API (for example) uses another method for variable size structs : the variable size buffer is last in struct and is declared to be char buf[1].
Your struct would become :
struct entry {
size_t buf_len;
int something;
char buf[1];
};
The allocation is (still no error checking) :
size_t buf_len = 4; // size of the buffer
struct entry *e;
e = malloc( sizeof(*e) + buf_len - 1); // struct already has room for 1 char
e->buf_len = buf_len; // set buffer size
That's all e.buf is guaranteed to be a char array of size buf_len.
That way ensures that even if the variable part was not a character array but a int, long, or anything array, the alignement would be given by the last element being a array of proper type and size 1.
For starters, the line:
e->buf = e + sizeof(*e); // the buffer lies behind the struct
Should be:
e->buf = e + 1; // the buffer lies behind the struct
This is because e + 1 will be equal to the address at the end of the structure. As you have it, it will only be the number of bytes into the structure equal to the number of bytes in a pointer.
And, yes, it's reasonable. However, I prefer this approach:
struct entry {
size_t buf_len;
int something;
char buf[1];
};
This way, you don't mess with the pointers. Just append as many bytes as needed, and they will grow the size of your buf array.
Note: I wrote a text editor using an approach similar to this but used a Microsoft c++ extension that allowed me to declare the last member as char buf[]. So it was an empty array that was exactly as long as the number of extra bytes I allocated.
seems fine to me - put comments in though
Or you could do this - which is quite common
struct entry {
size_t buf_len;
int something;
char buf;
};
ie make the struct itself variable length. and do
size_t buf_len = 4; // size of the buffer
struct entry *e = NULL;
// check that it packs right
e = malloc(sizeof(size_t) + sizeof(int) + buf_len ); // allocate struct and buffer
e->buf_len = buf_len; // set buffer size
...... later
printf(&e.buf);
I need to convert an array an place it in a struct that has a void* element and back to another array:
unsigned short array[size];
//do something to the array
typedef struct ck{
void * arg1;
void * arg2;
void * arg3;
} argCookie;
argCookie myCookie;
myCookie.arg2=malloc(sizeof(array));//alloc the necessary space
memcpy(myCookie.arg2,&array,sizeof(array));//copy the entire array there
//later....
unsigned short otherArray[size];
otherArray=*((unsigned short**)aCookie.arg2);
It happens that this last line won't compile...
Why is that? obviously I've messed up somewhere...
Thank you.
You can't copy arrays by assigning it a pointer, arrays are not pointers, and you cannot assign to an array, you can only assign to elements of an array.
You can use memcpy() to copy into your array:
//use array, or &array[0] in memcpy,
//&array is the wrong intent (though it'll likely not matter in this case
memcpy(myCookie.arg2,array,sizeof(array));
//later....
unsigned short otherArray[size];
memcpy(otherArray, myCookie.arg2, size);
That assumes you know size , otherwise you need to place the size in one of your cookies as well.
Depending on what you need, you might not need to copy into otherArray, just use the data from the cookie directly:
unsigned short *tmp = aCookie.arg2;
//use `tmp` instead of otherArray.
You can't assign to arrays. Instead of
otherArray=*((unsigned short**)aCookie.arg2);
just use memcpy again, if you know the size:
memcpy(&otherArray, aCookie.arg2, size*sizeof(unsigned short));
If you don't know the size, you're out of luck.
unsigned short* otherArray = (unsigned short*)aCookie.arg2
Then you can use otherArray[n] to access the elements. Beware of an out-of-bound index.
I am writing a light weight serialization function and need to include two variable sized arrays within this.
How should I track the size of each?
How should I define the struct?
Am I going about this all wrong?
EDIT: the result must be a contiguous block of memory
This resolves to something like
typedef struct
{
size_t arr_size_1, arr_size_2;
char arr_1[0/*arr_size_1 + arr_size_2*/];
} ...;
The size(s) should be in the front of the dynamic sized data, so that it doesn't move when expanding your array.
You cannot have 2 unknown sized arrays in your struct, so you must collapse them into one and then access the data relative from the first pointer.
typedef struct MyStruct_s
{
int variable_one_size;
void* variable_one_buf;
int variable_two_size;
void* variable_two_buf;
} MyStruct;
MyStruct* CreateMyStruct (int size_one, int size_two)
{
MyStruct* s = (MyStruct*)malloc (sizeof (MyStruct));
s->variable_one_size = size_one;
s->variable_one_buf = malloc (size_one);
s->variable_two_size = size_two;
s->variable_two_buf = malloc (size_two);
}
void FreeMyStruct (MyStruct* s)
{
free (s->variable_one_buf);
free (s->variable_two_buf);
free (s);
}
Since the data should be continuous in memory it is necessary to malloc a chunk of memory of the right size and manage it's contents more or less manually. You probably best create a struct that contains the "static" information and related management functions that do the memory management and give access to the "dynamic" members of the struct:
typedef struct _serial {
size_t sz_a;
size_t sz_b;
char data[1]; // "dummy" array as pointer to space at end of the struct
} serial;
serial* malloc_serial(size_t a, size_t b) {
serial *result;
// malloc more memory than just sizeof(serial), so that there
// is enough space "in" the data member for both of the variable arrays
result = malloc(sizeof(serial) - 1 + a + b);
if (result) {
result->sz_a = a;
result->sz_b = b;
}
return result;
}
// access the "arrays" in the struct:
char* access_a(serial *s) {
return &s->data[0];
}
char* access_b(serial *s) {
return &s->data[s->sz_a];
}
Then you could do things like this:
serial *s = ...;
memcpy(access_a(s), "hallo", 6);
access_a(s)[1] = 'e';
Also note that you can't just assign one serial to another one, you need to make sure that the sizes are compatible and copy the data manually.
In order to serialize variably-sized data, you have to have a boundary tag of some sort. The boundary tag can be either a size written right before the data, or it can be a special value that is not allowed to appear in the data stream and is written right after the data.
Which you choose depends on how much data you are storing, and if you are optimizing for size in the output stream. It is often easier to store a size before-hand, because you know how big to make the receiving buffer. If you don't then you have to gradually resize your buffer on load.
In some ways, I'd do things like Dan Olson. However:
1) I'd create the final struct by having two instances of a simpler struct that has just one variable array.
2) I'd declare the array with byte* and use size_t for its length.
Having said this, I'm still not entirely clear on what you're trying to do.
edit
If you want it contiguous in memory, just define a struct with two lengths. Then allocate a block big enough for both blocks that you want to pass, plus the struct itself. Set the two lengths and copy the two blocks immediately after. I think it should be clear how the lengths suffice to make the struct self-describing.