473,396 Members | 1,797 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,396 software developers and data experts.

tables initiated with pointers issues !

Hi all,

I have a serious trouble to which I found no explanation, if someone can help me this would save me really:
In my code I have lots of structures. I try to make tables of structures and then get access to elements of the structures to use them all along my code: here is an example to clarify things
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.  
  3. Fabric **closfabric;
  4.  
  5. }Network;
  6.  
Net is part of another structure called Switch and Fabric itself is a composed structure.
I have done the following
Expand|Select|Wrap|Line Numbers
  1. aSwitch->net.closfabric = (Fabric **)malloc(3* sizeof(Fabric *));
  2.  
  3. aSwitch->net.closfabric[0] = (Fabric *)malloc(m * sizeof(Fabric ));
  4. aSwitch->net.closfabric[1] = (Fabric *)malloc(k * sizeof(Fabric ));
  5. aSwitch->net.closfabric[2] = (Fabric *)malloc(m * sizeof(Fabric ));
  6.  
The compilation returns a segmentation fault because aSwitch->net.closfabric is NULL (0) .

surprisingly, in other codes the same analogy of using tables made of double pointers works perfectly with no problems and I have no problem of core dumped and NULL pointers.

I am lost and I cannot understand why am I getting this problem. It is the case whenever I try to use large dimension tables (tab *** struct for example)

Regards,

Monson
May 23 '14 #1
2 1276
donbock
2,426 Expert 2GB
Is it possible that you are running out of memory, causing malloc to return NULL?

In any case, your code should deal with that possibility - every call to malloc should be followed by a snippet that tests for failure-to-allocate and deals with the situation appropriately.
May 23 '14 #2
weaknessforcats
9,208 Expert Mod 8TB
This compiles:
Expand|Select|Wrap|Line Numbers
  1. Network net;
  2.     Network* aSwitch = &net;
  3.     int m = 3;
  4.     int k = 3;
  5.     aSwitch->closfabric = (Fabric **)malloc(3 * sizeof(Fabric *));
  6.  
  7.     aSwitch->closfabric[0] = (Fabric *)malloc(m * sizeof(Fabric));
  8.     aSwitch->closfabric[1] = (Fabric *)malloc(k * sizeof(Fabric));
  9.     aSwitch->closfabric[2] = (Fabric *)malloc(m * sizeof(Fabric));
but I may not clearly understand what net is.

Also, you have to be certain aSwitch is not NULL.
May 23 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bennett Haselton | last post by:
I'm working on an ASP.Net project where I want to test code on a local machine using a local database as a back-end, and then export it to the production machine where it uses the hosting...
2
by: malcolm | last post by:
Hello, We have a robust (.NET 1.1 c# winforms) client-server application that utilizes many typed DataSets, typed DataTables and typed DataRows. Our application is a series of windows and popup...
16
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid,...
17
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am...
3
by: David Mitchell | last post by:
Apologies for the Subject Heading! I am working on a small invoicing app for a local Tyre and Exhaust Centre. They want to be able to type in the Vehicle Registration Number and it...
1
by: rdemyan via AccessMonster.com | last post by:
My App has 10 or so tables that we provide that contains proprietary data. This data will need to be updated once or twice a year. I would like some comments, suggestions on my proposed strategy...
3
by: Jon Vaughan | last post by:
I have just put together a quick piece of code to update the local dataset data, but it doesn't work. I think my understanding of Tables.Select is incorrect. So what im asking is why doesn't this...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
1
momotaro
by: momotaro | last post by:
this is the part of code that goes wrong... am havin a run time error #include"common.h" voidDisplayMenu(int *); void main() {int*c;
7
by: Chris Morley | last post by:
Hi, i have found out as i have developed the database i've ended up with a number of tables that have the same attributes. For instance, my suppliers and customers have the same fields for the...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.