-7

Let's suppose this code:

int i,j=0;
char* block = (char*) (0x9000);
char table[4]= {0x01,0x02,0x03,0x04};
for (i=0; i< 45567; i++) {
    *(block +i)= table[j]; 
    j++; 
    if (j==4)
        j=0;
}

I would like to ask:

  1. Is the memory for block allocated in the stack or in the heap?
  2. What problems could happen with this code?
  3. Can I use free(block) at the end of this code?
Paul
  • 26,170
  • 12
  • 85
  • 119
Zero_One
  • 31
  • 2
  • 3
  • 7
    If you didn't call `malloc()` then why would you call `free()`? – NathanOliver Sep 15 '15 at 13:39
  • 2
    You are setting block to point to memory that you've never allocated. So it points to some random chunk of memory that you don't own.. if you try to read or write from it you'll get undefined behavior. – RyanP Sep 15 '15 at 13:40
  • 4
    You know, many years ago on Apple IIs I would `poke 0` into random memory until the machine crashed. Now that I'm an adult, I try not to use memory not given to me. – crashmstr Sep 15 '15 at 13:41
  • 1
    The assignment `char* block = (char*) (0x9000);` makes only sense in a freestanding implementation. In user-land code, you would crash as soon as you dereference  `block`; at least it is *undefined behavior* – Basile Starynkevitch Sep 15 '15 at 13:41
  • 2
    @crashmstr Poking numbers into page zero on apple ][s was especially fun :-) – Sergey Kalinichenko Sep 15 '15 at 14:01

6 Answers6

11

You didn’t actually allocate any memory, neither on the stack nor on the heap. You’re just pointing your variable at an address and then pretend that the memory there belongs to you.

This is not legal in either C or C++ and will generally not work. And “not work” can really mean anything here. And since the code is illegal, the question of whether the pointer can be freed is moot.

(In very specific settings, if the compiler and the hardware supports it, this is used to write to specific hardware addresses. But this isn’t the case here.)

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Is not possible that the address (0x9000) could belongs to heap or stack memory address range? – Zero_One Sep 15 '15 at 14:06
  • 1
    @Zero_One It’s possible. But as per the C++ specifications, it’s equally possible that the address belongs to the launch command console for the US nuclear missiles. This may sound facetious but the point is that C++ makes no distinction when it comes to undefined behaviour: unless you have a guarantee from elsewhere (i.e. your code is written for a specific platform which guarantees that 0x9000 is a valid and writeable address), all bets are off. – Konrad Rudolph Sep 15 '15 at 14:26
3

does the block memory allocated in the stack or in the heap?

Neither. It is not allocated at all: you assigned an arbitrary number to the pointer, so any attempt to read or write from block is undefined behavior.

Can I use free(block) at the end of this code?

That would be undefined behavior as well. Pointers that you are allowed to pass to free must come from malloc, calloc, or realloc.

Note: Undefined Behavior is the official name for what happens when your program does something that the standard does not allow, and the compiler does not catch. In many instances undefined behavior leads to a crash, but, unfortunately, in some cases your program appears to work.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

you dont need to free anything you didnt ask to get space for , everything you declare without realloc malloc or calloc dont need free , and they store on static storage or the stack .

there are also strdup and some other function you need to free but simple declare dont require free

ameyCU
  • 16,489
  • 2
  • 26
  • 41
Ori Yampolsky
  • 125
  • 13
0

You should never call free on a pointer value that wasn't returned from one of malloc, calloc, or realloc.

Do you know if 0x9000 is a valid, writable address on your platform?

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

The array table is likely on the stack. The pointer block, it is to be hoped, is in some otherwise unoccupied memory, maybe it was allocated, and 0x9000 is some special address, there isn't enough code to determine context. Depending on the architecture, writing blindly to an arbitrary address may or may not be allowed.

So to answer your questions:

  • you should not free without malloc/calloc/realloc, results are undefined; exceptions do exist, for example if a space is allocated by the system prior to your use, but then typically if the system allocates the memory, it will free it itself

  • all kinds of problems can happen with this code, but depends on the architecture; I've written on platforms where this was perfectly legal, others will throw all kinds of exceptions; it might be that it fills 45567 bytes starting at 0x9000 with repeated sequences of 1 to 4, or it might crash your machine; C will happily let you shoot yourself in the foot

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
0

What you did is quite dangerous.you should'nt pick a random address and assign it to a pointer to copy data to memory.it's like dumping nuclear waste in a random place on earth.instead,you should use malloc or new to allocate memory safely.

machine_1
  • 4,266
  • 2
  • 21
  • 42