It,s me having the same problem..
I want to pass some parameters from the main method to some other method. u cannot declare any variable names or pointers. I'm having an array of int with size 12500. if u want any variable or a pointer, use array blocks for it. This my array.

int arr[12500];

Lets assume this is my main method.

int main(){
*(arr+12498)=2
free(??);
}

and this is my method

void free(??){     }

.
I want to pass the value of *(arr+12498) to void free() method without any variable or pointer declaration from outside the array. please help meeeeeee.

Recommended Answers

All 16 Replies

arr looks to be a global array, so you don't need to pass anything, just use it:

#include <stdio.h>

int arr[12500];

void print(void)
{
    printf("%d\n", arr[0]);
}

int main(void)
{
    arr[0] = 123;

    print();

    return 0;
}

Thankx Narue!! Thats what i expected..

I do have another problem..
Lets assume my "void free(){ }" is calling from another method.

void search(){
// some codes here.
free(&size) // can't change this one
}

size is not a global variable.
So how i store the value of size in my int buf[12500] array without any variable or pointer declaration from outside the array.
then what will be my void free method.

void free(??)

assume that here free() is not calling from the main method..

If the address of size is being passed to free and you can't change the call, you have no reasonable option but to add a parameter:

void free(int *psize)
{
    buf[0] = *psize;
}

What kind of idiotic assignment is this, anyway? Do you have some written requirements that can be posted so can I understand what the point of your questions might be?

:$
i'm struggling with my Algorithm.

Write your own replacements for the memory allocation/deallocation routines
malloc() and free(). Name these two new functions NewMalloc() and NewFree().

NewMalloc() allocates memory for each call from an array of fixed size. This array is
an array of 50, bytes. All the data structures that you declare to manage this
50, bytes memory area must reside within that memory area.

The memory allocated using NewMalloc() can be freed with NewFree().

You cannot use malloc() or free() to write these two new functions.

If memory cannot be allocated using the available memory a suitable error value
must be returned.

The funtion prototypes of NewMalloc() and NewFree() should be the same as of
malloc() and free(). Check the man pages for malloc() and free() to get the function
prototypes.

We should be able to replace the calls to malloc() and free() in any program with
NewMalloc() and NewFree() and link it with your implementation and run that
program.

Make sure to put the NewMalloc() and NewFree() in a single source file and name it
"newmalloc.c". DO NOT include the main() function in that file.

This is an individual assignment and you must do this assignment using C language.

You must submit this assignment by 5. PM on January 03, 2011. Upload a gzipped
file containing the source code and a report (in PDF format) that describes your
memory allocation/de-allocation algorithm to LMS by that time.

But Now the path is clear :) Any way Thankx for ur help :)

Sorry for disturbing.. But i do have a problem regards ur solution.
U r using a pointer call

int *psize

I think it came from the outside the array.. Am i right??

Nowhere do your requirements state that you can't declare variables or pointers. You're reading too deeply between the lines and making the problem impossible.

>I think it came from the outside the array.. Am i right??
That's correct. There's no other option. You can't accept a function argument without declaring a corresponding parameter.

Can't we have variables , pointers like

void method(size_t size){
*(buf+12498)=buf;
*(buf+12497)=(buf+size);
//some codes;
search((buf+124900));

}

U can have Variables, pointers..
But all of them must declare within the array.
It's is not mentioned in the paper sheet.
but it is the most needed requirement..

If that's true then your assignment is impossible. There's no way to pass a size to NewMalloc without declaring a size parameter. Likewise there's no way to pass a pointer to NewFree without declaring a pointer parameter. Both of those are variables, which lead me to believe that you simply don't understand the requirements.

The information required to manage blocks of memory must be stored in the array, not all variables everywhere. :icon_rolleyes: For example, I would expect something like this to be an implementation of NewMalloc that meets the requirements:

static char base[50];

void *NewMalloc(size_t size)
{
    char *base_end = base + sizeof base;
    char *block = base;

    /* Find the first unused block */
    while (block < base_end && *(size_t*)block != 0)
        block += (*(size_t*)block + sizeof(size_t));

    if (block < base_end && base_end - block >= size) {
        /* Store the size prior to the returned address */
        *(size_t*)block = size;

        /* Return the usable pointer */
        return block + sizeof(size_t);
    }

    return NULL;
}

Note that I'm using variables, but the actual data for maintaining memory blocks is stored in the array; there are no separate memory manager data structures. That's what the restriction means.

I just got the email from my lecturer that it possible to declare like this.

void newMalloc(size_t size){
}
and
void newFree(void *address){
}

Thats all we can have. if we are going to Using more pointers & variables it must come from the array. thats all. I think now it is possible :)

>I think now it is possible
Send my implementation to your lecturer and see if he has any objections to it. I have a hard time believing that he's that much of a retard.

Sure I'll:)

He Said it was a solution for the last year's assignment. Not for This year.
Only

size_t size and void *address

are allowed. And said use the name of the array as a pointer and if we need any variable use array blocks .thats all. No more variables or pointers outside the array. he is throughly believes that it is possible.

It's certainly possible. Supremely stupid, but possible. I hate it when teachers throw in ridiculous restrictions that have absolutely no bearing on reality.

why can't we use something like this.

static int buf[12500];
void search(){
for(buf[12492]=0;buf[12492]<10;buf[12492]++){
if(buf[100]==buf[12490-*(buf+12492)])
method();
}
}

This gave me a compilation error. If i use buf[12492]+1 instead of buf[12492]++ it gave me an infinite loop. so how do i increment the value of buf[12492] by 1?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.