-1

i've got a qestion, i'm using sourgery c++ toolchain, and the compiler leave me to write this sentence:

for(i=0;i<size_of_categories;i++){

    size_t size_of_tmp = sizeof(char) * (HOSTLINK_CONFIG_STRING_MAX_LEN * (categories[i].key_len));

    char tmp[size_of_tmp];
    memset(tmp,0,(size_of_tmp));    
    get_hostlink_count[i]++;

    if(categories[i].time == get_hostlink_count[i]){

        if(format == CSV){

            csv_this_category_values(categories,i,tmp,size_of_tmp);
            strncat(buffer,tmp,buff_len);

        }else if (format == JSON){

            xi_json_this_category_values(categories,i,tmp,size_of_tmp);
            js_this_cat = json_loads(tmp,JSON_DECODE_ANY,NULL);
            json_array_extend(js_arr,js_this_cat);

            json_array_clear(js_this_cat);
            json_decref(js_this_cat);
        }

        get_hostlink_count[i] = 0;

    }
    //Free(tmp);
}

My issue is this sentece alloc memory in stack or in the heap? This could cause memory leak because is made in a for loop? Is this equivalent to make a malloc and at the end of the loop a Free?

size_t size_of_tmp = sizeof(char) * (HOSTLINK_CONFIG_STRING_MAX_LEN * (categories[i].key_len));

    char tmp[size_of_tmp];
trincot
  • 317,000
  • 35
  • 244
  • 286
andrea
  • 381
  • 5
  • 23

1 Answers1

0

Since your not doing any mallocs, there won't be a memory leak in this particular piece of code. However, you can't declare your tmp array with a non-constant size. That is, you can't say char tmp[size_of_tmp] with size_of_tmp not being constant (which it isn't because it depends on categories[i].key_len).

What you can do is to use malloc or calloc to allocate memory for your array on the heap like this

char *tmp = (char*)malloc(size_of_tmp);

or this

char *tmp = (char*)calloc(size_of_tmp, sizeof(char));

calloc already initializes all of the allocated memory to zero, so you don't have to use memset.

If you want to avoid memory leaks you have to free the allocated tmp array at the end of each loop iteration.

PoByBolek
  • 3,775
  • 3
  • 21
  • 22
  • You _can_ declare an array of a variable non-const size in C++. – mah Aug 08 '13 at 11:19
  • and in C++ this use stack? – andrea Aug 08 '13 at 11:33
  • @mah You sure? I mean, it looks like it have been in the standard, but apparently > no C++ standard ever accepted VLAs from C99, so technically the fact of admitting VLAs in C++ is a g++ extension. see [this answer](http://stackoverflow.com/a/5730286/2640170) ... – PoByBolek Aug 08 '13 at 11:33
  • @user2387829 there's never a guarantee of a local being placed on the stack; the compiler is free to place it where it wants (and since it's a variable of automatic scope, the compiler is required to reclaim the memory when it's out of scope). – mah Aug 08 '13 at 11:38
  • @PoByBolek I think you're correct that VLA is just a gnu extension. It's a confusing topic though. As http://stackoverflow.com/questions/8593643/does-c-support-variable-length-arrays states, the standard reads "C++ is a general purpose programming language based on the C programming language as specified in ISO/IEC 9899:1999 (hereinafter referred to as the C standard). In addition to the facilities provided by C, C++ provides...", suggesting that if it's in C it's in C++, however that page also states "... doesnt mean that any and everything in c99 is in c++11". I dunno. – mah Aug 08 '13 at 11:41
  • @mah I didn't even know that there were VLAs in C/C++ until today... I find them kind of creepy though. – PoByBolek Aug 08 '13 at 11:52