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 720403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:45:15+00:00 2026-05-14T05:45:15+00:00

I’m using malloc to make an error check of whether memory can be allocated

  • 0

I’m using malloc to make an error check of whether memory can be allocated or not for the particular array z1. ARRAY_SIZE is a predefined with a numerical value. I use casting as I’ve read it’s safe to do so.

long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE);  
if(z1 == NULL){  
   printf("Out of memory\n");  
   exit(-1);  
}

The above is just a snippet of my code, but when I add the error checking part (contained in the if statement above), I get a lot of compile time errors with visual studio 2008. It is this error checking part that’s generating all the errors. What am I doing wrong?

On a related issue with malloc, I understand that the memory needs to be deallocated/freed after the variable/array z1 has been used. For the array z1, I use:

free(z1);
z1 = NULL;

Is the second line z1 = NULL necessary?

I get 102 errors…well MVS2008 stops the errors at 102. The errors are of type:

error C2143: syntax error : missing ';' before 'type'  
error C2065: 'L' : undeclared identifier
// this error repeats for all my identifiers

and this points right after the closing } in the if statement.

ARRAY_SIZE is quite large. I define it as

#define ARRAY_SIZE 2500001

My full above code is too long. But I have a smaller code which is giving me the same behavior. Sorry for the formatting. I can’t seem to get it right.

#include stdio.h //note I have the actual < > in my code
#include stdlib.h
#include math.h
#define ARRAY_SIZE 11
#define VECTOR_SIZE 5

main()
{
    long double *z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
    if(z == NULL){
        printf("Out of memory\n");
        exit(-1);
    }

    long double *k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE);
    int i;
    long double A, B;
    void f(long double fa[], long double fb[], long double fA, long double fB);

    A = 0.5;
    B = 2;

    for(i = 0; i < VECTOR_SIZE; i++){
        k[i] = 0;
    }

    k[1] = 4;
    k[2] = 8;

    for(i = 0; i < ARRAY_SIZE; i++){
        z[i] = 0;
    }

    z[1] = 5;


    f(k, z, A, B);

    free(z);
    free(k);
    z = NULL;
    k = NULL;
}


void f(fa, fb, fA, fB)
long double fa[], fb[], fA, fB;
{
    fa[0] = fb[1]* fA;
    fa[1] = fa[1] - 1;
    fb[0] = 2* fB - fa[2];
    printf("fa[2] is 8 and is the same as *[fa + 2] and is  %3.3Le\n", *(fa + 2));
    printf("\nAddress of &fa[2] is %x\n", &fa[2]);
    printf("same address is fa + 2 is %x\n", fa + 2);
    return;
}
  • 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-14T05:45:15+00:00Added an answer on May 14, 2026 at 5:45 am

    Problems in your code

    Allright. Now that you’ve provided all the code it’s easier to explain your problems:

    1. You’re trying to define variables "in the middle" of your functions. C doesn’t allow this. you have to define all your variables right at the start. That’s what’s giving you the
      error C2143: syntax error : missing ';' before 'type'
      errors.
    2. Same goes for the function declaration (needs to be at the top of the function).

    Therefore, changing your code to the following makes it work:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define ARRAY_SIZE 11
    #define VECTOR_SIZE 5
    
    main() {
        void f(long double fa[], long double fb[], long double fA, long double fB);
    
        long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
        long double* k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE);
        int i;
        long double A, B;
    
        if (z == NULL) {
            printf("Out of memory\n");
            exit(-1);
        }
    
        A = 0.5;
        B = 2;
    
        for (i = 0; i < VECTOR_SIZE; i++) {
            k[i] = 0;
        }
    
        k[1] = 4;
        k[2] = 8;
        
        for (i = 0; i < ARRAY_SIZE; i++) {
            z[i] = 0;
        }
        
        z[1] = 5;
        
        f(k, z, A, B);
        
        free(z);
        free(k);
        z = NULL;
        k = NULL;
    }
    
    void f(fa, fb, fA, fB)  
    long double fa[], fb[], fA, fB;  
    {
        fa[0] = fb[1]* fA;
        fa[1] = fa[1] - 1;
        fb[0] = 2* fB - fa[2];
        
        printf("fa[2] is 8 and is the same as *[fa + 2] and is  %3.3Le\n", *(fa + 2));
        printf("\nAddress of &fa[2] is %x\n", &fa[2]);
        printf("same address is fa + 2 is %x\n", fa + 2);
        
        return;
    }
    

    A few other points

    Now I’ll add a few more tips, that perhaps aren’t strictly errors (meaning, they still compile…), but aren’t very good coding practices:

    1. As I said before, use consts to define constants rather than #defines.
    2. define main() properly – that is int main() {... rather than just main() without a return type. It works in C, but doesn’t work in C++ and I consider it bad style. (Why the hell am I supposed to assume functions return int if nothing else is said? why not void?)
    3. Following that, you should explicitly return a value from main().
    4. I prefer declaring the void f(long double fa[], long double fb[], long double fA, long double fB); function prototype outside main().
    5. When defining functions use the modern syntax – the one you used in the prototype – rather than the ancient one:
      void f(fa, fb, fA, fB)
      long double fa[], fb[], fA, fB;
      {
      Should become:
      void f(long double fa[], long double fb[], long double fA, long double fB) {.

    This way your code turns to:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void f(long double fa[], long double fb[], long double fA, long double fB);
    
    int main() {
        const int ARRAY_SIZE = 11;
        const int VECTOR_SIZE = 5;
    
        long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
        long double* k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE);
    
        int i;
        long double A, B;
    
        if (z == NULL) {
            printf("Out of memory\n");
            exit(-1);
        }
    
        A = 0.5;
        B = 2;
    
        for (i = 0; i < VECTOR_SIZE; i++) {
            k[i] = 0;
        }
    
        k[1] = 4;
        k[2] = 8;
        
        for (i = 0; i < ARRAY_SIZE; i++) {
            z[i] = 0;
        }
        
        z[1] = 5;
        
        f(k, z, A, B);
        
        free(z);
        free(k);
        z = NULL;
        k = NULL;
    
        return 0;
    }
    
    void f(long double fa[], long double fb[], long double fA, long double fB) {
        fa[0] = fb[1]* fA;
        fa[1] = fa[1] - 1;
        fb[0] = 2* fB - fa[2];
        
        printf("fa[2] is 8 and is the same as *[fa + 2] and is  %3.3Le\n", *(fa + 2));
        printf("\nAddress of &fa[2] is %x\n", &fa[2]);
        printf("same address is fa + 2 is %x\n", fa + 2);
        
        return;
    }  
    

    Which I think is better.

    First Post

    Please provide all your code. I tested the following code on Visual C++ 2008 Express with "language extensions" disabled and level 4 warnings. It works just fine:

    #include <stdlib.h>
    #include <stdio.h>
    
    int main() {
        const int ARRAY_SIZE = 1024*1024;
    
        long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE);
        if (z1 == NULL) {
            printf("Out of memory\r\n");
            exit(-1);
        }
    
        printf("Awesome!\r\n");
    
        return 0;
    }
    

    Maybe you forgot an include, maybe you did something else wrong. The code snippet itself seems perfectly fine. The second error you described seems totally unrelated:
    error C2065: 'L' : undeclared identifier // this error repeats for all my identifiers

    By the way, prefer const over #define.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.