473,797 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting data from memory block / pointer issues.

Hi all,

I'm working on a program that reads a structure from a file (1000
records).

The file read part works great, but I'm hitting a roadblock
on howto read the data from the pointer.

I'm aware that *(&WordPtr+(i*5 )) gives me the AddrID at location of i
(0-999)

What is the right way of getting AddrName at for example struct row 50?

Thanks.

// parts of code.. :

struct AddrData {
unsigned int AddrID;
char AddrName[15];
};

struct AddrData Addr;
struct Addr *AddrPtr;
int i;

// allocate memory
AddrPtr = malloc(sizeof(A ddr)*1000);

// open file, read data
fread(&AddrPtr, sizeof(struct AddrData), 1000, filePtr);
Nov 14 '05 #1
3 2181
"FaXiA" <fa***@nospam.l ocalhost> writes:
I'm working on a program that reads a structure from a file (1000
records).

The file read part works great, but I'm hitting a roadblock
on howto read the data from the pointer.

I'm aware that *(&WordPtr+(i*5 )) gives me the AddrID at location of i
(0-999)
I'm not sure why you'd want to express it that way. The natural
way to refer to the (i+1)th AddrID is AddrPtr[i].AddrID. You
seem to want to express that in terms of "words", but that's not
usually a good way to do things in C.
What is the right way of getting AddrName at for example struct row 50?
AddrPtr[50].AddrName (or AddrPtr[49].AddrName, depending on how
you're defining "row 50").
struct AddrData {
unsigned int AddrID;
char AddrName[15];
};

struct AddrData Addr;
struct Addr *AddrPtr;
int i;

// allocate memory
AddrPtr = malloc(sizeof(A ddr)*1000);
When calling malloc(), I recommend using the sizeof operator on
the object you are allocating, not on the type. For instance,
*don't* write this:

int *x = malloc (128 * sizeof (int)); /* Don't do this! */

Instead, write it this way:

int *x = malloc (128 * sizeof *x);

There's a few reasons to do it this way:

* If you ever change the type that `x' points to, it's not
necessary to change the malloc() call as well.

This is more of a problem in a large program, but it's still
convenient in a small one.

* Taking the size of an object makes writing the statement
less error-prone. You can verify that the sizeof syntax is
correct without having to look at the declaration.
// open file, read data
fread(&AddrPtr, sizeof(struct AddrData), 1000, filePtr);


The & is incorrect; remove it.
--
Here's a tip: null pointers don't have to be *dull* pointers!
Nov 14 '05 #2
Got it working.

Thanks for your help!

Nov 14 '05 #3
On Thu, 03 Feb 2005 11:36:34 -0500, "FaXiA" <fa***@nospam.l ocalhost>
wrote:
Hi all,

I'm working on a program that reads a structure from a file (1000
records).

The file read part works great, but I'm hitting a roadblock
on howto read the data from the pointer.

I'm aware that *(&WordPtr+(i*5 )) gives me the AddrID at location of i
(0-999)
Wrong for so many reasons:

1. WordPtr is undefined. Based on your comment about what you want
and Ptr suffix in the name, it looks like it should be a pointer to
struct AddrData.

If it is such a pointer, then using the & operator guarantees that
you are no longer pointing at structures but at memory around the
pointer itself.

If you remove the &, then the correct arithmetic would be
*(WordPtr + i). However, that expression does not have type unsigned
int which your comment implies you want.

2. On the other hand, WordPtr could be the first element of an array
of struct. Then you would need *(&WordPtr + i) but you would still
have the type problem.

3. Maybe WordPtr is a char*. The you would need
*(WordPtr + (i*sizeof(struc t AddrData))). (It should be obvious that
sizeof(struct AddrData) will not be 5 on most systems people here
use.) Of course, there is still the type problem.

What is the right way of getting AddrName at for example struct row 50?

Thanks.

// parts of code.. :

struct AddrData {
unsigned int AddrID;
char AddrName[15];
};

struct AddrData Addr;
struct Addr *AddrPtr;
struct Addr is an undefined type. I assume you meant AddrData.
int i;

// allocate memory
AddrPtr = malloc(sizeof(A ddr)*1000);

// open file, read data
fread(&AddrPtr , sizeof(struct AddrData), 1000, filePtr);


This does not read the data into the allocated memory pointed to by
AddrPtr. It attempts to read the data into AddrPtr itself. sizeof
AddrPtr bytes later it invokes undefined behavior. Lose the &.

<<Remove the del for email>>
Nov 14 '05 #4

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

Similar topics

18
6681
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a hard drive file system. Over time, the more often use call new/delete or alloc/free, there will be gaps and fragments in the heap. This can lead to inefficient use of available memory, as well as cache-hit inefficiencies.
9
3190
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int WORDS_PER_LINE = 4; when counter == 7 is when the string Concatenation fails within the IF block.
5
4806
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e. "payload") using pointers. Examples of these are binary trees, hash tables etc. Thus, the message itself contains only a pointer to the actual data. When the message is sent to the same processor, these pointers point to the original locations, which...
6
2733
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios, every now and then the client would timeout and have to re-initiate the request... TIA!
1
2755
by: tony.fountaine | last post by:
I am working on a project to read a Bosch Measurement Data File (MDF). The file contains a number of blocks that can be read from the file using a baisc structure. For example the ID BLOCK is as follows, (Data Type) (Number of Elements) (Description) CHAR 8 File identifier, CHAR 8 Format identifier, CHAR 8 Program identifier,
94
4784
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 that I found inside of a string. Any ideas?
31
7760
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
12
2572
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs about it. I wanted that on my class delete should not work. Object pointer should be deleted using my function only which is taking care of reference count for particular class. Thanx in advance for your inputs.
22
1818
by: a | last post by:
By previous replies, it seems that the following method somehow solves the problem up to 1000 * 1000 2D data, but when I try 10k * 10k, the segmentation fault problem appears again. Richard Tobin told me there is a system limit that can be changed. But I don't know which file is to be changed. I have modified again and again and hope to find out a solution that can handle 100k * 100k data.
0
9685
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
9537
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
10246
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
10209
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
9066
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
7560
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
5459
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...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2934
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.