Pointer & Malloc Help Appreciated (sorry if repost)

K

Kevin

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
 
S

santosh

Kevin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top