Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 806829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:18:26+00:00 2026-05-15T00:18:26+00:00

further developing the code from yesterday ( seg fault caused by malloc and sscanf

  • 0

further developing the code from yesterday (seg fault caused by malloc and sscanf in a function), I tried with the help of some tutorials I found on the net to generate a 2-dim vla. But I get a segmentation fault at (*data)[i][j]=atof(p);. The program is supposed to read a matrix out of a text file and load it into a 2d array (cols 1-9) and a 1D array (col 10)

[Example code]

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

const int LENGTH = 1024;

void read_data(float ***data, int **classes, int *nrow,int *ncol, char *filename){
  FILE *pfile = NULL; 
  char line[LENGTH];
  if(!( pfile=fopen(filename,"r"))){
    printf("Error opening %s.", filename);
    exit(1);
  }
  int numlines=0;
  int numcols=0;
  char *p;

  fgets(line,LENGTH,pfile);
  p = strtok (line," ");
  while (p != NULL){
    p = strtok (NULL, ", ");     
    numcols++;
  }
  while(fgets(line,LENGTH,pfile)){
    numlines++;
  }
  rewind(pfile);
  int numfeats=numcols-1;
  *data=(float**) malloc(numlines*sizeof(float*));
  *classes=(int *)malloc(numlines*sizeof(int));
  if(*classes == NULL){
    printf("\nOut of memory.");
    exit(1);
  }

  int i=0;
  while(fgets(line,LENGTH,pfile)){
    p = strtok (line," ");
    for(int j=0;j<numfeats;j++) 
      {
        (data)[i]=malloc(numfeats*sizeof(float));
        printf("%i ",i);
        (*data)[i][j]=atof(p);
        p = strtok (NULL, ", ");
      }
    (*classes)[i]=atoi(p);
    i++;
  }
  fclose(pfile);
  *nrow=numlines;
  *ncol=numfeats;
}  

int main() {
  char *filename="somedatafile.txt";
  float **data2;
  int *classes2;
  int r,c;

  read_data(&data2,&classes2, &r, &c,filename) ;
  for(int i=0;i<r;i++){
    printf("\n");
    for(int j=0;j<c;j++){
      printf("%f",data2[i][j]);
    }
  }
  return 1;
}

[Content of somedatafile.txt]

50 21 77 0 28 0 27 48 22 2
55 0 92 0 0 26 36 92 56 4
53 0 82 0 52 -5 29 30 2 1
37 0 76 0 28 18 40 48 8 1
37 0 79 0 34 -26 43 46 2 1
85 0 88 -4 6 1 3 83 80 5
56 0 81 0 -4 11 25 86 62 4
55 -1 95 -3 54 -4 40 41 2 1
53 8 77 0 28 0 23 48 24 4
37 0 101 -7 28 0 64 73 8 1
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:18:26+00:00Added an answer on May 15, 2026 at 12:18 am

    Did you mean
    (*data)[i]=malloc(numfeats*sizeof(float));

    Instead of
    (data)[i]=malloc(numfeats*sizeof(float))

    ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While developing a navigation based application from scratch, I added some new uisubview Controller
Good evening. I am looking at developing some code that will collect EXIF data
I am trying to further a question I asked yesterday where I wanted to
I've received source code for a Java product to make further changes. The archive
I am developing an application that store some data in a Amazon SimpleDB instance.
I am developing a library for further usage in other libraries or final products.
I'm currently developing a camera application for Android on which some problems have occurred.
I am developing one application which is connecting to server to get some data.
I am new to developing Excel VSTO solutions and need a little help on
I am developing some image processing tools in iOS. Currently, I have a contour

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.