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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Iso c++ forbids variable length array - simple way to solve it
Next
Answered
Lou Roche
  • 15
Lou Roche
Asked: May 18, 20222022-05-18T11:04:29+00:00 2022-05-18T11:04:29+00:00In: cpp

Iso c++ forbids variable length array – simple way to solve it

  • 15

. Advertisement .

..3..

. Advertisement .

..4..

For the problem “iso c++ forbids variable length array.” I tried to fix it, but It doesn’t work and returns the result I want. Here is my program:

#include "utilities.h"
 FILE *open_file(char *filename, const char*extension, const char *access)
 {
  char string[MAX_STR_LEN];
  FILE *strm = NULL;
 
  if(filename[0]=='\0')
  {
  printf("\n INPUT FILENAME (%s) > ",access);
  fgets(string,MAX_STR_LEN,stdin);
  sscanf(string,"%s",filename);
  printf(" FILE %s opened \n", filename);
  }
  int len=strlen(filename);
 
  if( len + strlen(extension) >= MAX_STR_LEN)
  {
  printf("\n ERROR: String Length of %s.%s Exceeds Maximum",
  filename, extension);
  return(NULL);
  } 
 
  // char *filename1 = new(char[len+strlen(extension)+1]);
 
  const int filenameLength = len+strlen(extension)+1;
  char *filename1 = new(char[filenameLength]);
 
  strcpy(filename1,filename); // temp filename for appending extension
 
  /* check if file name has .extension */
  /* if it does not, add .extension to it */
  int i=len-1;
  while(i > 0 && filename[i--] != '.');
  // printf("\n Comparing %s to %s", extension, filename+i+1);
  if(strcmp(extension, filename+i+1) )
  strcat(filename1,extension);
  if( (strm = fopen(filename1, access) ) == NULL )
  {
  printf("\n ERROR OPENING FILE %s (mode %s)", filename1,access);
  }
  delete(filename1);
  return(strm);
 }
 char *filename1 = new(char[filenameLength]);

and

Compiling utilities.cc ...
 src/utilities.cc: In function ‘FILE* open_file(char*, const char*, const char*)’:
 src/utilities.cc:251: error: ISO C++ forbids variable-size array
 gmake: *** [/home/landon/geant4/work/tmp/Linux-g++/exampleN01/utilities.o] Error 1

has occurred. I’ve checked the entire command line but still can’t find the mistake.

forbids variable length array
  • 2 2 Answers
  • 280 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-12T11:46:02+00:00Added an answer on June 12, 2022 at 11:46 am

    The cause: The error: Iso c++ forbids variable length array” happens because VLA (variable size arrays) are not allowed in C++.

    You can’t make an array like this on the stack as a local variable length array:

    char filename1[filenamelength];

    Solution: 

    Instead, try this:

    char *filename1 = new char[filenameLength];

    In addition, when you’ve already given memory for an array, you could perhaps clear memory by using

    delete [] filename1;
    • 7
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Mia Teixeira
    2022-05-25T20:20:35+00:00Added an answer on May 25, 2022 at 8:20 pm

    This error is correct. C++ prohibits variable size arrays (VLA). This is a VLA.

    char filename1char[filenameLength];

    Most likely, you meant this:

    char *filename1 = new char[filenameLength];

    This is not a VLA but an array chars that are allocated to the heap. This pointer should be deleted using operator delete[]

    delete[] filename1;
    • 12
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.