473,748 Members | 6,370 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 1885
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
4121
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
3127
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
1619
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
1880
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
2347
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
1567
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
5944
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
3275
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
7880
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
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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
9372
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...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
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
6796
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
6074
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();...
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.