Results 1 to 5 of 5

Thread: Malloc which covers an area allocated to the compilation

  1. #1
    Join Date
    May 2008
    Posts
    551

    Malloc which covers an area allocated to the compilation

    I have a C source code in which I declare:

    Code:
    main()
    {
       float variable=0;
       float * tab;
       tab=malloc(10000*sizeof(float));
       /* Function where I pass variable & tab that contains a loop assigning values to tab[i] */
    }
    For now I put a malloc (10000) instead of [] because I intend to eventually make the size variable at runtime.

    The problem I encounter is that "variable" is allocated to address 0X0013fe9C, while the address is pointing 0X0013fe0 tab. Result: the execution of my 4th loop (tab [3] points to 0X0013fe9C, because in my case sizeof (float) = 4) crushes "variable".

    What I do not understand is that the memory location of "variable" is reserved for the compilation, while that by pointing tab via the malloc is reserved for the execution. I thought the two were separated. I'm using VS Express 2008.

  2. #2
    Join Date
    May 2008
    Posts
    945

    Re: Malloc which covers an area allocated to the compilation

    In this case, variable and tab spaces are stored on the stack. So the exact address is not determined until run-time.

    If these fields had been declared outside a function, then they would have been declared on the heap and had an address fixed at compile time (which was then adjusted by the loader when loading the program memory).

    A small error is not to cast the return of malloc (), in fact, the compiler should issue a warning. Write:

    tab=(float *)malloc(10000*sizeof(float));

    Then, there is no reason why tab [x], where x between 0 and 99999, is encroaching on other variables, because the notation with the brackets is equivalent to (address found in tab) + x, and not (address tab) + x.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,259

    Re: Malloc which covers an area allocated to the compilation

    thank you, I was correct, of course, "variable" is, therefore, are not static location is reserved for the entry in the block on the stack.

    But I still reserve my malloc, which already used a ... and I really do not understand why.

  4. #4
    Join Date
    May 2008
    Posts
    945

    Re: Malloc which covers an area allocated to the compilation

    tab is equal to 0X0013fe0, which is close to the address of variable. But what is the address in the location 0X0013fe0

  5. #5
    Join Date
    May 2008
    Posts
    3,971

    Re: Malloc which covers an area allocated to the compilation

    @Bull50, you do not used &tab instead of a tab by accident?

    @Lemog, it is not necessary - and many (*) consider it bad style to do - to cast the result of malloc in C (that would be necessary in C++, a warning or an error on this line C is usually the result of another problem, forget to include for example).

    (*) I do not, but I know I am rather in the minority.

Similar Threads

  1. Difference between new and malloc.
    By Bansi_WADIA in forum Software Development
    Replies: 3
    Last Post: 01-12-2009, 05:25 PM
  2. What is the use of malloc and calloc functions
    By Jagadamba in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 10:40 AM
  3. No space allocated in windows xp
    By Drewski in forum Operating Systems
    Replies: 3
    Last Post: 03-04-2009, 10:17 PM
  4. Allocated memory alert
    By hatred in forum Small Business Server
    Replies: 5
    Last Post: 12-12-2007, 05:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,702,456.37215 seconds with 17 queries