473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Allocating memory for structs

Hi All,
I need one clarification regarding memory allocation for structure. The
details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .

Oct 6 '07 #1
8 4534
Win Sock wrote:
Hi All,
I need one clarification regarding memory allocation for structure.
The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
Yes, place each pair in a sub-structure and place both the
sub-structures in a union.

Oct 6 '07 #2

"Win Sock" <no****@nospam.comwrote in message
news:sl*******************@nospam.com...
Hi All,
I need one clarification regarding memory allocation for structure. The
details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
union. If you have either a len0 and pointer0, or len1 and pointer1, and you
want to squeeze both into the same space, unions are designed for the task.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 6 '07 #3
Win Sock <no****@nospam.comwrites:
I need one clarification regarding memory allocation for structure. The
details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
Given that declaration, no, not really.

Others have suggested using a union, but what are you really trying to
accomplish? Presumably allocating memory for only a subset of a
structure is a means to an end, not an end in itself. If you tell us
what your actual goal is, we might be able to help (or we might advise
you that it's not worth doing).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 6 '07 #4
Win Sock wrote:
Hi All,
I need one clarification regarding memory allocation for structure.
The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .

This doesn't make very much sense to me. What problem are you trying to
solve?

Brian
Oct 6 '07 #5
Win Sock wrote:
>
I need one clarification regarding memory allocation for structure.
The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0
or len1 and pointer1 .
You probably need a union. However, the types you are using in the
struct seem rather silly. I would suggest replacing uit32 with
size_t, and uint8 with unsigned long. Also don't forget to include
a label for the struct, i.e.:

struct name {
size_t len0;
unsigned long *pointer0;
...

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 7 '07 #6
CBFalconer <cb********@yahoo.comwrites:
Win Sock wrote:
>I need one clarification regarding memory allocation for structure.
The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0
or len1 and pointer1 .

You probably need a union. However, the types you are using in the
struct seem rather silly. I would suggest replacing uit32 with
size_t, and uint8 with unsigned long.
[...]

I agree that size_t is probably a better type for the len0 and len1
members, but how on Earth did you conclude that pointer0 and pointer1
should be pointers to unsigned long rather than to uint8?

(There's no standard type called uint8; perhaps the OP meant uint8_t?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 7 '07 #7
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Win Sock wrote:
>>I need one clarification regarding memory allocation for structure.
The details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0
or len1 and pointer1 .

You probably need a union. However, the types you are using in the
struct seem rather silly. I would suggest replacing uit32 with
size_t, and uint8 with unsigned long.
[...]

I agree that size_t is probably a better type for the len0 and len1
members, but how on Earth did you conclude that pointer0 and pointer1
should be pointers to unsigned long rather than to uint8?
Frankly, I have no idea. Thus proving I can write garbage with the
best of them. At the moment pointer to unsigned char seems more
reasonable.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Oct 7 '07 #8
On Sat, 06 Oct 2007 19:43:45 +0200, Win Sock wrote:
Hi All,
I need one clarification regarding memory allocation for structure. The
details are as given below :

let us consider one structure

struct {
uit32 len0;
uint8 *pointer0;
uit32 len1;
uint8 *pointer1;
);

When i want to allocate memory using malloc() for this structure is
there any way i can allocate memory for only for len0 and pointer0 or
len1 and pointer1 .
Am I having a dejÃ*-vu, or was this identical question already
posted here a few weeks ago?

What are you trying to do?
I think that you could allocate space for len0 and pointer0 by
doing malloc(offsetof(struct foo, len1)), but I don't have the
courage to try it on my DS9K. And anyway, there is probably a
*much* better way to do what you ultimately need.
As for len1 and pointer1, in some cases
unsigned char *realptr = malloc(sizeof struct unnamed
- offsetof(struct unnamed, len1));
struct unnamed *fakeptr = realptr - offsetof(struct unnamed, len1);
could work, but there will be big problems if e.g. realptr happens
to be at the beginning of a segment, or whatever, so *don't do
that*.
I repeat, what you are trying to do? It is *very* likely that you
picked the wrong path towards that goal.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Oct 7 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
3
by: UncleRic | last post by:
Greetings: I'm trying to get the ASCI C syntax correct here. I want to create memory space for a variable-size array of pointers to structs. This is what I found in theScripts' archive: ...
6
by: Amit_Basnak | last post by:
Dear Friends I have two structures as below typedef struct { long_int length; char data; } CI_STRUCT_DATA; typedef CI_STRUCT_DATA *ptr_CiStructData;
5
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
5
by: Bob Altman | last post by:
Hi all, I need to allocate a buffer large enough to store a copy of a null-terminated string that is given to me as an argument. The code that I am using looks a lot like old ugly C code. Is...
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.