tags:

views:

253

answers:

3

Hi, 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  A: 

Try #include-ing stdio.h and stdlib.h to make sure that NULL is actually defined.

And to answer your second question setting z1 to NULL is not necessary, though it will help you to make sure you never inadvertently try to use z1 after it's been free'd, since dereferencing a null pointer will crash. So its a good defensive thing to do, but not required.

cpalmer
Thanks. I've put some additional information in my question up.
yCalleecharan
I have the same errors when including stdio.h.
yCalleecharan
A: 

You may need to check in other places of your code if z1 is allocated. Setting it to NULL is a good way to tell that memory is not allocated for the pointer.

zoli2k
Thanks. z1 is being allocated as my code is running fine without the if statement for the error checking.
yCalleecharan
A: 

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.

conio
Thanks. I'll add my code up in the posting.
yCalleecharan
yes, your little piece of code works fine for me too.
yCalleecharan
Wow, your corrected version works fine. I have just restarted using c after a decade of programming in Matlab. Your tips are very valuable to me and I'm very thankful for them. I'm still waiting for the 2 c programming textbooks that I'm buying. Thanks a lot for your patience in explaining good programming practices to me.
yCalleecharan
By the way on an unrelated issue, what am I doing wrong so that only part of my code appears in the grey window? What's the proper way to get the code well formatted? thanks
yCalleecharan
No problem.The gray code windows is a bit problematic (it took me a few times to get it right in my post), but the best way is to first paste the code from your editor, select it, and click the "101" button.If you look at [your post](http://stackoverflow.com/revisions/eb9810d7-f119-4e07-8609-8828a5319db6/view-source) you'll notice that you only have three spaces before the `#include...`, and you need four spaces for it to get formatted right.
conio