473,699 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer & Malloc Help Appreciated (sorry if repost)

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(t ype_a));

for (*some runtime count*)
{
ptr_b = malloc(sizeof(t ype_b));
set_function(pt r_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
1 1883
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(t ype_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(t ype_b));
set_function(pt r_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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4116
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
7
3126
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: /* Allocate 1st dimension */ if((Screen = (Atom ***) malloc(sizeof(Atom **) * Width)) == NULL) perrexit("malloc");
2
1615
by: Yossarian | last post by:
Hi, I'm a bit confused about something, hopefully someone can put me straight. I'd like to be able to call a function which takes a pointer to pointer, have that function allocate memory and return the size. I can't get it to work and I would like to know why this code outputs wierd values (the first mem address is ok i think):
7
1877
by: Frank | last post by:
Sorry that the question I posted few minutes ago didn't correctly describe the problem. So please ignore it. I repost the question as below: ============================================== I am developing an application on PPC405 (Walnut). But somehow in the string in the C function doesn't work. The code is as below: void example() {
7
2344
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of pointers, which seems to go ok. I want to then take it one step further and dynamically create the array (pointers to pointers). I try to print out the data again, retrieve the first 3 values (probably by luck), then maybe a pointer value, and then...
10
1561
by: masood.iqbal | last post by:
The code example below shows the dynamic allocation of a 2D array. I must admit that it took quite a while for me to get there (I already have another posting to that effect), but I am glad that I finally got it working. Now here's the problem: I am able to get the 2D array dynamically allocated correctly as long as I am doing it "in-line" (i.e. without invoking any function). The moment I try to do it in another function, I get a...
42
5918
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is printed will it be all 0's or 0x12345678 int main(void) { char *ptr; ptr = 0;
2
3272
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
29
7874
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real code. #include <stdio.h> #include <stdlib.h>
0
8691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7755
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6536
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.