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

Allocating memory to structure.

Hello! It's my first post and my First time browsing this forum.

I am doing a piece of homework for about a week. I'm having problems allocating memory for a structure. Segmentation fault is the error. Code from example is missing. Here are the relevant parts:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct file{
  6.     char *name;
  7.     int uid, gid, size, time;
  8. };
  9.  
  10. int main ( int argc, char *argv[] ){
  11.     int i, j, k, max_name_size, err=0, err0=0, err1=0, err2=0, a=0, R=0, r=0, s=0 ,t=0;
  12.     struct dirent *d;
  13.     struct stat *st;
  14.     struct file*  f;
  15.     DIR *dir;
  16.     f = malloc( 50 * sizeof( struct file ) );
  17.     f->name = malloc ( 50 * sizeof( char ) );
  18.     while( d != NULL ){
  19.         strcpy( f[i].name, (char*)d->d_name );//Segmentation fault.
  20.         f[i].uid = (unsigned)st->st_uid;//Error.
  21.         f[i].gid = (unsigned)st->st_gid;
  22.         f[i].size = (unsigned)st->st_size;
  23.         f[i].time = (unsigned)st->st_mtime;
  24.         max_name_size = strlen( f[0].name );
  25.         for( k=1; k<i-1; ++k)
  26.             if( max_name_size < strlen( f[k].name ) )//Segmentation fault.
  27.                 max_name_size = strlen( f[k].name );//Segmentation fault.
  28. //        qsort1(f->name,i,sizeof(char*),strcmp);//Segmentation fault.
  29.     for( k=0; k<i; ++k ){
  30.         if( !a )
  31.             if ( f[k].name[0] == '.' )//Segmenation fault.
  32.                 continue;
  33.         for ( j=0; j<max_name_size-strlen( f[k].name ); ++j)//Segmentation fault.
  34.             printf(" ");
  35.         printf( "%s\t%u:%u\t%u\t%u\n", f[k].name, f[k].uid, f[k].gid, f[k].size, f[k].time );//Segmentation fault.
  36.     }
  37.     free( f->name );
  38.     free( f );
  39.     return 0;
  40. }
  41.  
Here is the full code: http://pastebin.com/KPdst9YM

I apologize for the many lines of code and I would actually appreciate some help.
Mar 17 '11 #1

✓ answered by Banfa

Line 17, you allocate memory for f[0].name but not for the name pointer in the other 49 structures in the array you allocated at line 16.

Also it is normally good practice to give your variables meaningful names except for loop counters (and some would say not even then) variable names should be > 3 characters long.

1 2471
Banfa
9,065 Expert Mod 8TB
Line 17, you allocate memory for f[0].name but not for the name pointer in the other 49 structures in the array you allocated at line 16.

Also it is normally good practice to give your variables meaningful names except for loop counters (and some would say not even then) variable names should be > 3 characters long.
Mar 18 '11 #2

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

Similar topics

6
by: soni29 | last post by:
hi, i'm reading a c++ book and noticed that the author seems to allocate memory differently when using classes, he writes: (assuming a class called CBox exists, with member function size()): //...
10
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
6
by: toktam | last post by:
Hi guys! I defined a type like this: typedef struct { char *key; int freq; float pr; } TableEntry; and then I have an array of pointers to this struct:
12
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
8
by: Win Sock | last post by:
Hi All, I need one clarification regarding memory allocation for structure. The details are as given below : let us consider one structure struct { uit32 len0; uint8 *pointer0; uit32 len1;
10
by: Chris Saunders | last post by:
Here is the declaration of a struct from WinIoCtl.h: // // Structures for FSCTL_TXFS_READ_BACKUP_INFORMATION // typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT { union { //
2
by: Evolution445 | last post by:
Hi, I have a function that reads 17 bytes starting from &H7C2B84. Each time it's about to read it throws an AccessViolationException, which, if I read posts around the net correctly means that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.