By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Pointer & Malloc Help Appreciated (sorry if repost)

P: n/a
Hi all,

I clearly have an issue with some pointers, structures, and memory
allocation. Its probably pritty basic, but I'm a little stuck. Any
help would be greatly appreciated.
I'd like to instantiate an arbitrary number of arrays of arbitrary
size in function_a, copy the pointers, store the data, and free any
unused memory. My basic structure is as follows:

I have two structs
typedef struct {unsigned char * data;} type_a;
typedef struct {unsigned char * data;} type_b;

void main
{
type_a * ptr_a;
type_b * ptr_b;

ptr_a = malloc(sizeof(type_a));

for (*some runtime count*)
{
ptr_b = malloc(sizeof(type_b));
set_function(ptr_a);
ptr_b->data = ptr_a->data;
store_function(ptr_b);
}
free(ptr_a);
}
void set_function (type_a * ptr)
{
type_a->data = malloc(*some runtime size*);
}
I of course check for the null returns on the malloc calls but am
pritty sure I have more fundamental issue as to why this isn't
working. I am also purposely not freeing ptr_b as this is what is
actually getting stored. Please let me know if what I'm attempting to
do isn't clear. Again, thanks for any help.

Regards,

Kevin

Mar 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Kevin wrote:
Hi all,

I clearly have an issue with some pointers, structures, and memory
allocation. Its probably pritty basic, but I'm a little stuck. Any
help would be greatly appreciated.
I'd like to instantiate an arbitrary number of arrays of arbitrary
size in function_a, copy the pointers, store the data, and free any
unused memory. My basic structure is as follows:

I have two structs
typedef struct {unsigned char * data;} type_a;
typedef struct {unsigned char * data;} type_b;

void main
Very bad form. Always make main return an int.
{
type_a * ptr_a;
type_b * ptr_b;

ptr_a = malloc(sizeof(type_a));
Better yet:

ptr_a = malloc(sizeof *ptr_a);

And don't forget to check malloc's return value.
for (*some runtime count*)
{
ptr_b = malloc(sizeof(type_b));
set_function(ptr_a);
No prototype for set_function in scope. It's good practise to declare
functions before using them.
ptr_b->data = ptr_a->data;
store_function(ptr_b);
You've not shown us the definition for this function.
}
free(ptr_a);
You've not free'ed ptr_a->data and ptr_b.
}
void set_function (type_a * ptr)
{
type_a->data = malloc(*some runtime size*);
ITYM, ptr->data = malloc(SIZE); This may be your mistake.
}
I of course check for the null returns on the malloc calls but am
pritty sure I have more fundamental issue as to why this isn't
working. I am also purposely not freeing ptr_b as this is what is
actually getting stored. Please let me know if what I'm attempting to
do isn't clear. Again, thanks for any help.
What exactly is your problem. As such you're malloc'ing two
structures, and initialising a member of one of them to some block of
memory of unspecified size and copying this value to the corresponding
member of the other structure. Then you say you're"storing" it.

Until you tell us your actual problem and show us the actual code, we
can do little.

Mar 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.