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

pointer and memory question

int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?
Thanx
Nov 15 '05 #1
7 1252
Janice wrote:
int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?


Well, the first one is more likely to work. You can fix the second one like
this:

ptr = &i;

I suggest:

fread(&i, sizeof i, 1, fs);

Please note that a 0 for the third argument isn't going to read very many
objects, no matter how often you do it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #2
"Richard Heathfield" <in*****@address.co.uk.invalid> wrote in message
news:db**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
Janice wrote:
int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?
Well, the first one is more likely to work. You can fix the second one

like this:

ptr = &i;

I suggest:

fread(&i, sizeof i, 1, fs);

Please note that a 0 for the third argument isn't going to read very many
objects, no matter how often you do it.


I'd also note that the data stored in the file that you're reading with this
fread is not portable among different platforms.
That's for endianness and sizeof(int) reasons.

Alex
Nov 15 '05 #3

"Janice" <no@mail.com> wrote
int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?

ptr needs to point to some memory.

Generally you don't want to fread / fwrite a single int. It is not portable,
and almost always it is better to reconstruct the integer from a stream.
Nov 15 '05 #4
Janice wrote:
int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?
Thanx


First one doesn't actually declare any buffer space (it is only an
uninitialized pointer).

David.
--
David Lago <dl***@mundo-r.com>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
Nov 15 '05 #5


Alexei A. Frounze wrote:
"Richard Heathfield" <in*****@address.co.uk.invalid> wrote in message
news:db**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
Janice wrote:
int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?


Well, the first one is more likely to work. You can fix the second one

like
this:

ptr = &i;

I suggest:

fread(&i, sizeof i, 1, fs);

Please note that a 0 for the third argument isn't going to read very many
objects, no matter how often you do it.


I'd also note that the data stored in the file that you're reading with this
fread is not portable among different platforms.
That's for endianness and sizeof(int) reasons.

Alex


I agree with Richard Heathfield.
Try POSIX function--read() instead.:-)

Nov 15 '05 #6
Cong Wang wrote:

I agree with Richard Heathfield.
Try POSIX function--read() instead.:-)


But I don't agree with you, I'm afraid. What he needs to do can almost
certainly be done with fread(), which has the advantage over read() that it
does not require a POSIX-conforming implementation.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #7
David Lago wrote on 15/07/05 :
Janice wrote:
int* ptr;
int i;
fread(&i,sizeof(int),0L,fs);
fread(ptr,sizeof(int),0L,fs);

Which way of declaring the buffer for the fread is better?
Thanx


First one doesn't actually declare any buffer space (it is only an
uninitialized pointer).


You meant the second one, don't you ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
Nov 15 '05 #8

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

Similar topics

4
by: trustron | last post by:
Hi all, I have got a pointer question. I was told that a pointer is a variable holding a memory address. SO:
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
5
by: Charles M. Reinke | last post by:
OK, this may be a dumb question, but please bear with me. If I declare a pointer: int *p; the memory space for a pointer is allocated, but is the memory space also reserved for one integer,...
3
by: David Mathog | last post by:
This one is driving me slightly batty. The code in question is buried deep in somebody else's massive package but it boils down to this, two pointers are declared, the first is: char **resname...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
11
by: Brian Gladman | last post by:
A lot of low level cryptographic code and some hardware cryptographic accelerators either fail completely or perform very poorly if their input, output and/or key storage areas in memory are not...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
6
by: Mahendra | last post by:
I have two cases - 1. When I have a pointer A pointing to a heap memory - What happens when I dereference the pointer A using free(A). It deallocated the heap memory the pointer was pointing...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
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
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...

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.