Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

malloc #38

Open
zaoqi opened this issue Apr 4, 2019 · 5 comments
Open

malloc #38

zaoqi opened this issue Apr 4, 2019 · 5 comments

Comments

@zaoqi
Copy link

zaoqi commented Apr 4, 2019

malloc is not declared in current scope

@dawsonjon
Copy link
Owner

Hi, Thanks very much for the feedback. Please could you expand a little? What steps should I take to reproduce the problem?

@zaoqi
Copy link
Author

zaoqi commented Apr 4, 2019

http://dawsonjon.pythonanywhere.com/

/*LZSS Compression Component*/
/*Jonathan P Dawson 2014-07.10*/
  /* diff */
#include <stdlib.h>                    
const int N = 1024;
const int LOG2N = 10;

unsigned raw_in = input("raw_in");
unsigned compressed_out = output("compressed_out");

/*Create a to send data of an arbitrary bit length*/
unsigned packed, stored = 0;
void send_bits(unsigned data, unsigned bits){
    unsigned i;
    for(i=0; i<bits; i++){
        packed >>= 1;
        packed |= (data & 1) << 31;
        data >>= 1;
        stored++;
        if(stored == 32){
            fputc(packed, compressed_out);
            stored = 0;
        }
    }
}

/*A function that reads a stream of uncompressed data, 
and creates a stream of compressed data*/

void compress(){
    malloc(1);           /* diff */
  
    unsigned pointer, match, match_length, longest_match, longest_match_length;
    unsigned buffer[N];

    unsigned new_size;

    while(1){
        for(pointer=0; pointer<N; pointer++){
            buffer[pointer] = fgetc(raw_in);
        }

        pointer=0;
        new_size = 0;
        while(pointer<N){

            /*Find the longest matching string already sent*/
            longest_match = 0;
            longest_match_length = 0;
            for(match=0; match<pointer; match++){

                /*match length of 0 indicates no match*/
                match_length = 0;

                /*search through buffer to find a match*/
                while(buffer[match+match_length] == buffer[pointer+match_length]){
                    match_length++;
                }

                /*If this is the longest match, remember it*/
                if(match_length > longest_match_length){
                    longest_match = match;
                    longest_match_length = match_length;
                }

            }

            /*send data*/
            if(longest_match_length >= 3){
                send_bits(0, 1);
                send_bits(longest_match_length, LOG2N);
                send_bits(pointer - longest_match, LOG2N);
                pointer += longest_match_length;
                new_size += LOG2N + LOG2N + 1;
            }
            else{
                send_bits(1, 1);
                send_bits(buffer[pointer], 8);
                pointer++;
                new_size += 9;
            }

        }
    }
}

@zaoqi
Copy link
Author

zaoqi commented Apr 4, 2019

Error in file: /home/dawsonjon/main.c at line: 31
malloc is not declared in current scope

@zaoqi
Copy link
Author

zaoqi commented Apr 5, 2019

@dawsonjon

@dawsonjon
Copy link
Owner

Hi,

Dynamic memory allocation is a fairly experimental feature, but I have done a couple of fixes and I think it is working now. You need to define a couple of macros to get it working. The first enables dynamic memory allocation, and the second determines the amount of memory to reserve for dynamic allocation.

Thanks
Jon

#define ENABLE_DYNAMIC_MEMORY
#define MALLOC_SIZE 1024
#include <stdlib.h>
void main(){

int * a;
a = malloc(1);
free(a);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants