Sponsored Content
Full Discussion: Passing pointers to struct
Homework and Emergencies Homework & Coursework Questions Passing pointers to struct Post 302486425 by ignatius3 on Saturday 8th of January 2011 12:31:52 PM
Old 01-08-2011
Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function with a pointer to my struct and write only creates the file and doesn't write anything. I'm pretty sure the problem i have has to do with the pointers because when i execute
Code:
printf("%i",sizeof(*pointer_to_the_struct)

in bwrite function it says 1 and should be 48

Here are the important parts of the code:

Code:
struct superbloque{
    unsigned int firstmbits;
    unsigned int lastmbits
    ...
    unsigned int numbloques;
}

int initSB (unsigned int nbloq){
    struct superbloque SB;
    SB.firstmbits=0;
    SB.lastmbits=TamMB(nbloq)-1;
    ...
    SB.numbloques=nbloq;
    bwrite(0,&SB)
}

int bwrite(unsigned int bloque, void *buf){
    lseek(mifd, bloque*tbloque, SEEK_SET);
    return write(mifd, buf, sizeof(*buf);
}

I know there's a lot of information about this but i've been reading for two days and I can't figure out how to solve the problem.
Thank you very much for your help


Ricardo Galli
Sistemas operativos
Universidad de las Islas Baleares (Palma de Mallorca, Spain)

Last edited by pludi; 01-08-2011 at 01:52 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

Need a simple example of passing FILE pointers

Dear friends, can anybody pls tell me how to pass FILE pointer in c. I am so confused .. :confused: suppose I ve two function 1. file_open() 2. read_line() I want to call these function from main() function and in file_open() function it will open that file and in read_line()... (5 Replies)
Discussion started by: user_prady
5 Replies

2. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies

3. UNIX for Advanced & Expert Users

Passing socket struct between kernel threads - module programming

I write kernel module with kernel threads using linux/kthread.h on 2.6.* kernel I tried to passing data between two kernel threads with data argument of kthread_run( fun, data , NAME ); but this is not work I dont know why. I tried many possibility and nothing works. So I thought that... (0 Replies)
Discussion started by: marcintom
0 Replies

4. Homework & Coursework Questions

C++ struct pointers & functions

Hi All, My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible... (0 Replies)
Discussion started by: pondlife
0 Replies

5. UNIX for Dummies Questions & Answers

Passing struct through unix pipe -solved

EDIT: Nevermind, called a friend who is good at this stuff and he figured it out :D Hi all, So I'm trying to teach myself to write programs for unix in c. I am currently creating a program, and I need to pass a struct through a pipe, but I can't figure out how. The struct I want to pass... (0 Replies)
Discussion started by: twnsfn34
0 Replies

6. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

7. Programming

Passing an instance of struct to functions in other src files

I am trying to work out the best syntax for a relatively simple operation. The goal is to declare an instance of a struct and pass it around to be populated and have the data manipulated. There is an extra wrinkle in that the functions are in different src files. The main is simple, #include... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

8. Programming

Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf. Anyway, here's a... (5 Replies)
Discussion started by: uiop44
5 Replies

9. Programming

Passing Pointers by reference in C++ Problem

Hello All, I am having this issue...where I am actually having hard time understanding the problem: The code is as follows: #include<iostream.h> void fxn(char*** var) { int i =4; *var = (char**)malloc(i*sizeof(char*)); for(int j =0; j<4; j++) { *var = "name"; cout<<*var;... (6 Replies)
Discussion started by: mind@work
6 Replies

10. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies
BUFFERCACHE(9)						   BSD Kernel Developer's Manual					    BUFFERCACHE(9)

NAME
buffercache, bread, breadn, bwrite, bawrite, bdwrite, getblk, geteblk, incore, allocbuf, brelse, biodone, biowait -- buffer cache interfaces SYNOPSIS
#include <sys/buf.h> int bread(struct vnode *vp, daddr_t blkno, int size, struct kauth_cred *cred, int flags, buf_t **bpp); int breadn(struct vnode *vp, daddr_t blkno, int size, daddr_t rablks[], int rasizes[], int nrablks, struct kauth_cred *cred, int flags, buf_t **bpp); int bwrite(buf_t *bp); void bawrite(buf_t *bp); void bdwrite(buf_t *bp); buf_t * getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo); buf_t * geteblk(int size); buf_t * incore(struct vnode *vp, daddr_t blkno); void allocbuf(buf_t *bp, int size, int preserve); void brelse(buf_t *bp, int set); void biodone(buf_t *bp); int biowait(buf_t *bp); DESCRIPTION
The buffercache interface is used by each filesystems to improve I/O performance using in-core caches of filesystem blocks. The kernel memory used to cache a block is called a buffer and described by a buf structure. In addition to describing a cached block, a buf structure is also used to describe an I/O request as a part of the disk driver interface. FUNCTIONS
bread(vp, blkno, size, cred, flags, bpp) Read a block corresponding to vp and blkno. The buffer is returned via bpp. The units of blkno are specifically the units used by the VOP_STRATEGY() routine for the vp vnode. For device special files, blkno is in units of DEV_BSIZE and both blkno and size must be multiples of the underlying device's block size. For other files, blkno is in units chosen by the file system containing vp. If the buffer is not found (i.e. the block is not cached in memory), bread() allocates a buffer with enough pages for size and reads the specified disk block into it using credential cred. The buffer returned by bread() is marked as busy. (The B_BUSY flag is set.) After manipulation of the buffer returned from bread(), the caller should unbusy it so that another thread can get it. If the buffer contents are modified and should be written back to disk, it should be unbusied using one of variants of bwrite(). Otherwise, it should be unbusied using brelse(). breadn(vp, blkno, size, rablks, rasizes, nrablks, cred, flags, bpp) Get a buffer as bread(). In addition, breadn() will start read-ahead of blocks specified by rablks, rasizes, nrablks. breada(vp, blkno, size, rablkno, rabsize, cred, flags, bpp) Same as breadn() with single block read-ahead. This function is for compatibility with old filesystem code and shouldn't be used by new ones. bwrite(bp) Write a block. Start I/O for write using VOP_STRATEGY(). Then, unless the B_ASYNC flag is set in bp, bwrite() waits for the I/O to complete. bawrite(bp) Write a block asynchronously. Set the B_ASYNC flag in bp and simply call VOP_BWRITE(), which results in bwrite() for most filesys- tems. bdwrite(bp) Delayed write. Unlike bawrite(), bdwrite() won't start any I/O. It only marks the buffer as dirty (B_DELWRI) and unbusy it. getblk(vp, blkno, size, slpflag, slptimeo) Get a block of requested size size that is associated with a given vnode and block offset, specified by vp and blkno. If it is found in the block cache, make it busy and return it. Otherwise, return an empty block of the correct size. It is up to the caller to ensure that the cached blocks are of the correct size. If getblk() needs to sleep, slpflag and slptimeo are used as arguments for cv_timedwait(). geteblk(size) Allocate an empty, disassociated block of a given size size. incore(vp, blkno) Determine if a block associated to a given vnode and block offset is in the cache. If it is there, return a pointer to it. Note that incore() doesn't busy the buffer unlike getblk(). allocbuf(bp, size, preserve) Expand or contract the actual memory allocated to a buffer. If preserve is zero, the entire data in the buffer will be lost. Oth- erwise, if the buffer shrinks, the truncated part of the data is lost, so it is up to the caller to have written it out first if needed; this routine will not start a write. If the buffer grows, it is the callers responsibility to fill out the buffer's addi- tional contents. brelse(bp, set) Unbusy a buffer and release it to the free lists. biodone(bp) Mark I/O complete on a buffer. If a callback has been requested by B_CALL, do so. Otherwise, wakeup waiters. biowait(bp) Wait for operations on the buffer to complete. When they do, extract and return the I/O's error value. CODE REFERENCES
The buffer cache subsystem is implemented within the file sys/kern/vfs_bio.c. SEE ALSO
intro(9), vnode(9) Maurice J. Bach, The Design of the UNIX Operating System, Prentice Hall, 1986. Marshall Kirk McKusick, Keith Bostic, Michael J. Karels, and John S. Quarterman, The Design and Implementation of the 4.4BSD Operating System, Addison Wesley, 1996. BUGS
In the current implementation, bread() and its variants don't use a specified credential. Because biodone() and biowait() do not really belong to buffercache, they shouldn't be documented here. BSD
November 11, 2009 BSD
All times are GMT -4. The time now is 10:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy