473,803 Members | 3,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structs and malloc

Hi, i have the following type and struct defined

typedef struct hNode {
unsigned char name[1024];
unsigned long hVal;
struct hNode *next;
} HashNode;

what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(H ashNode));

i can be sure of the value of "next" in the aNode (eg. later in the
program i want to compare the value of "next" in aNode to see if it has
been linked to another HashNode) --- can i do this in
some easy way or do i need to write my own memory allocator ???.
Thanks.
Nov 14 '05 #1
15 2186
begin followup to Lars Tackmann:
what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(H ashNode)); ^
Something is missing here.
i can be sure of the value of "next" in the aNode
Well, you can use calloc instead of malloc to set all bytes of the
allocated memory to zero. On most platforms that will get you a
null-pointer (and on the one that uses a different representation
you will get a horrible, hard to track runtime error).
[...] can i do this in some easy way or do i need to write my own
memory allocator ???.


Well, you need to do two things:

HashNode* aNode = malloc(sizeof(* aNode));
aNode->next = 0;

If you want to group these two lines into one function, fine with me.

HashNode* new_HashNode(vo id)
{
HashNode* aNode = malloc(sizeof(* aNode));
aNode->next = 0;
return aNode;
}

--
Für Google, Tux und GPL!
Nov 14 '05 #2
nrk
Alexander Bartolich wrote:
begin followup to Lars Tackmann:
what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(H ashNode)); ^
Something is missing here.

i can be sure of the value of "next" in the aNode


Well, you can use calloc instead of malloc to set all bytes of the
allocated memory to zero. On most platforms that will get you a
null-pointer (and on the one that uses a different representation
you will get a horrible, hard to track runtime error).
[...] can i do this in some easy way or do i need to write my own
memory allocator ???.


Well, you need to do two things:

HashNode* aNode = malloc(sizeof(* aNode));
aNode->next = 0;

If you want to group these two lines into one function, fine with me.

HashNode* new_HashNode(vo id)
{
HashNode* aNode = malloc(sizeof(* aNode));
aNode->next = 0;


ITYM:
if ( aNode ) aNode->next = 0;
return aNode;
}


-nrk.
Nov 14 '05 #3
I think that my example was bad - i need a to be able to allocate a entire
array of hash nodes and be sure that all of the nodes has a sane value
from the start.

eg.

HashNode *hashTable = malloc(sizeof(h ashNode) * 100);

Thanks

Nov 14 '05 #4
Lars Tackmann wrote:
I think that my example was bad - i need a to be able to allocate a entire
array of hash nodes and be sure that all of the nodes has a sane value
from the start.

eg.

HashNode *hashTable = malloc(sizeof(h ashNode) * 100);


I'm confused. If you want an array why do you use a linked list?
Tobias.
Nov 14 '05 #5

"Lars Tackmann" <ro****@diku.dk > wrote in message
i need a to be able to allocate a entire array of hash nodes and be sure
that all of the nodes has a sane value from the start.

eg.

HashNode *hashTable = malloc(sizeof(h ashNode) * 100);

malloc() returns a pointer to memory containing garbage. To help makes bugs
more trackable, some implementations set this to a definite bit-pattern,
whilst others don't.

calloc() returns memory set to all bits zero. It is a false friend. On 99%
of systems a pointer with all bits zero is NULL, but there are a few
implementations where this is not true.

What you need to do is to call malloc(), then iterate over the array. For
instance, to create a linked list.

for(i=0;i<99;i+ +)
hashTable[i].next = &hashtable[i+1];
hashTable[99].next = NULL;
Nov 14 '05 #6
Yep this is what i need - thanks.
On Sun, 21 Dec 2003, Malcolm wrote:

"Lars Tackmann" <ro****@diku.dk > wrote in message
i need a to be able to allocate a entire array of hash nodes and be sure
that all of the nodes has a sane value from the start.

eg.

HashNode *hashTable = malloc(sizeof(h ashNode) * 100);

malloc() returns a pointer to memory containing garbage. To help makes bugs
more trackable, some implementations set this to a definite bit-pattern,
whilst others don't.

calloc() returns memory set to all bits zero. It is a false friend. On 99%
of systems a pointer with all bits zero is NULL, but there are a few
implementations where this is not true.

What you need to do is to call malloc(), then iterate over the array. For
instance, to create a linked list.

for(i=0;i<99;i+ +)
hashTable[i].next = &hashtable[i+1];
hashTable[99].next = NULL;

Nov 14 '05 #7
On Sun, 21 Dec 2003, Tobias Oed wrote:
Lars Tackmann wrote:

I'm confused. If you want an array why do you use a linked list?
Tobias.


In a hash table you usaly have the problem that more than one items hashes
to the same value -. If this happens you link the ones toghter that have
the same hash value (creating a bucket - that you end up searching linary).

That is why i need an array of items that also can be linked to other items.
Nov 14 '05 #8
On Sun, 21 Dec 2003 19:32:18 UTC, Lars Tackmann <ro****@diku.dk >
wrote:
Hi, i have the following type and struct defined

typedef struct hNode {
unsigned char name[1024];
unsigned long hVal;
struct hNode *next;
} HashNode;

what i want, is to ensure that when doing:

HashNode aNode = malloc(sizeof(H ashNode));


change to

if (!(aNode = malloc(sizeof(* aNode)) {
/* as malloc returns a pointer to undetermied memory */
/* initialise the struct members to meaningfull values */
/* to prevent unspezified or in worst case undefined behavior */
aNode->name[0] = 0; /* name is empty */
aNode->hVal = 0ul; /* no value */
aNode->next = NULL /* no pointer assigned */
} else {
/* malloc was unable to give enough memory for a new HashNode */
/* do the needed error handling */
}
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation

Nov 14 '05 #9

"Lars Tackmann" <ro****@diku.dk > wrote in message
news:Pi******** *************** *******@brok.di ku.dk...
On Sun, 21 Dec 2003, Tobias Oed wrote:
Lars Tackmann wrote:

I'm confused. If you want an array why do you use a linked list?
Tobias.
In a hash table you usaly have the problem that more than one items hashes
to the same value -. If this happens you link the ones toghter that have
the same hash value (creating a bucket - that you end up searching

linary).
That is why i need an array of items that also can be linked to other items.

You may have misunderstood how hashtable is usually put together:

horisontally it's an array, vertically items (duplicate hash values/keys)
are
usually implemented as linked list.

See my post in thread "Hash Function". It contains a link to snippets
collection
hashtable implementation - you can take some ideas from there I guess.

with respect,
Toni Uusitalo
Nov 14 '05 #10

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

Similar topics

11
3406
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
3
2175
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of it. There are lots of pointers and pointers to pointers which makes me confused. First my typedef: typedef struct { double re;
5
2073
by: Grant Austin | last post by:
What would be the correct syntax for setting up a dynamic array of structs? Suppose you have a struct declared: struct relation { FILE * binFile; unsigned int numAttrs; struct attrList * relAttrs; /* definition shown at end of post */ };
10
1996
by: Patricia Van Hise | last post by:
Is it possible to access a field of a struct which is a field of another struct? Ex. struct subStr{ int num1; int num2; }; struct myStr { int num3; subStr *lock; };
10
4134
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; };
6
325
by: Matthew Jakeman | last post by:
If i create a cruct like so : struct test{ char *var1 ; int var2 ; short var3 ; } and i want to allocate it memory, should i allocate enough to hold the max amount of data that will go into the char * plus the size of an int plus the
5
2768
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
15
3843
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h> typedef struct _tnode_t { void *content; struct _tnode_t *kids;
2
11945
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and mallocing space for the pointer 'countPtr' in each struct, but do I need to do anything else? Thanks. typedef struct TwoCounts { int *countPtr;
8
3222
by: kiser89 | last post by:
I'm having a problem with my array of structs and segmentation faults. I have this struct that represents one line of a source file: struct threeTokens { int lineNumber; char* cmd; char* param; }; line; This is the code that tries to fill the array of structs, which is a global variable called program: program = malloc(numLines * sizeof(line)); while(NULL != fgets(buffer, SIZE, fp)){ tokenPtr = strtok(buffer,"...
0
9700
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
9564
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
10310
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
10068
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
9121
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
7603
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
6841
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
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
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.