ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Common Board

C/C++ malloc function doesn`t work
Posted by atamachi 12 Jan 2011 23:41
Hello,
I try to solve problem 1209 with allocating memory, but i receive compilation error.

---------------------------------------------------------
e2c33114-a1cd-488e-b50b-9a2950afaee7
e2c33114-a1cd-488e-b50b-9a2950afaee7(8) : error C2143: syntax error : missing ';' before 'type'
e2c33114-a1cd-488e-b50b-9a2950afaee7(19) : error C2065: 'AA' : undeclared identifier
e2c33114-a1cd-488e-b50b-9a2950afaee7(19) : error C2109: subscript requires array or pointer type
e2c33114-a1cd-488e-b50b-9a2950afaee7(24) : error C2065: 'AA' : undeclared identifier
e2c33114-a1cd-488e-b50b-9a2950afaee7(24) : error C2109: subscript requires array or pointer type
e2c33114-a1cd-488e-b50b-9a2950afaee7(30) : error C2065: 'AA' : undeclared identifier
e2c33114-a1cd-488e-b50b-9a2950afaee7(30) : error C2109: subscript requires array or pointer type
e2c33114-a1cd-488e-b50b-9a2950afaee7(32) : error C2065: 'AA' : undeclared identifier
e2c33114-a1cd-488e-b50b-9a2950afaee7(32) : warning C4022: 'free' : pointer mismatch for actual parameter 1
---------------------------------------------------------


Code example:
---------------------------------------------------------
#include <stdio.h>
#include <malloc.h>

int main()
{
        int N;
    scanf("%d", &N);
    int *AA = (int *)malloc(N*sizeof(int));
        ... ... ...
        free(AA);
        return 0;
}
---------------------------------------------------------
What the matter ?

Thank you for answers.

Edited by author 12.01.2011 23:44
Re: C/C++ malloc function doesn`t work
Posted by Evgeniy++ 12 Jan 2011 23:50
This is correct code for C++, not for C, because when using C compiler, only forward var declarations are permited

{
int N = 0;
int* AA = NULL;
// ...
AA = (int*)malloc(..);

// etc.
}
Re: C/C++ malloc function doesn`t work
Posted by atamachi 13 Jan 2011 00:12
Evgeniy++ wrote 12 January 2011 23:50
This is correct code for C++, not for C, because when using C compiler, only forward var declarations are permited

{
int N = 0;
int* AA = NULL;
// ...
AA = (int*)malloc(..);

// etc.
}


I receive this kind of errors again...

Compilation errors.
------------------
311e64be-7d0c-42db-86d6-9276eb18480e(8) : error C2143: syntax error : missing ';' before 'type'
311e64be-7d0c-42db-86d6-9276eb18480e(9) : error C2065: 'AA' : undeclared identifier
311e64be-7d0c-42db-86d6-9276eb18480e(9) : warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
311e64be-7d0c-42db-86d6-9276eb18480e(20) : error C2065: 'AA' : undeclared identifier
311e64be-7d0c-42db-86d6-9276eb18480e(20) : error C2109: subscript requires array or pointer type
311e64be-7d0c-42db-86d6-9276eb18480e(25) : error C2065: 'AA' : undeclared identifier
311e64be-7d0c-42db-86d6-9276eb18480e(25) : error C2109: subscript requires array or pointer type
311e64be-7d0c-42db-86d6-9276eb18480e(31) : error C2065: 'AA' : undeclared identifier
311e64be-7d0c-42db-86d6-9276eb18480e(31) : error C2109: subscript requires array or pointer type
311e64be-7d0c-42db-86d6-9276eb18480e(33) : error C2065: 'AA' : undeclared identifier
311e64be-7d0c-42db-86d6-9276eb18480e(33) : warning C4022: 'free' : pointer mismatch for actual parameter 1
------------------

Full code
------------------
#include <stdio.h>
#include <malloc.h>

int main()
{
    int total_zero, count_zero, i, N, K, s;
    scanf("%d", &N);
    int* AA = NULL;
    AA = (int*)malloc(N*sizeof(int));
    s = 0;
    for( ; N > 0; N--){
        scanf("%d", &K);
        total_zero = count_zero = i = 0;
        while( 1 ){
            i++;
            if(total_zero == count_zero){
                count_zero = 0;
                total_zero++;
                if(i == K){
                    AA[s++] = 1;
                    break;
                }
            }
            if(i == K){
                AA[s++] = 0;
                break;
            }
            count_zero++;
        }
    }
    for(i = 0; i < s; i++) printf("%d ", AA[i]);
    fputc('\n', stdout);
    free(AA);
    return 0;
}
------------------

error C2143: syntax error : missing ';' before 'type'

It is very strange.