454,385 Members | 1,737 Online
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

# new and memset

 P: n/a Consider the dynamic allocation of a 2D array: int **temp; temp = new int*[height]; for (y=0; y
9 Replies

 P: n/a [a] No, thankfully. That would add a O(N) overhead cost, needlessly, to every memory allocation. [b] Sure. memset works with any pointer, it doesn't matter if it's pointing to the stack or to the heap. Your syntax looks fine for setting it to 0. Remember that the second parameter is converted to an unsigned char. Dec 25 '05 #2

 P: n/a Luke Meyers wrote: [b] Sure. ... Remember that the second parameter is converted to an unsigned char. What shall be done to convert it to int? Or float, if the 2D array is of floats? thanks in advance, D. Rathore (remove underscores for email ID) Dec 25 '05 #3

 P: n/a di************@gmail.com wrote: Consider the dynamic allocation of a 2D array: int **temp; temp = new int*[height]; for (y=0; y > ? Dec 25 '05 #4

 P: n/a > What shall be done to convert it to int? Or float, if the 2D array is of floats? It's not converted to an int, it's converted to an unsigned char, which is the usual way to refer to an individual byte value. memset only operates on bytes, it will not initialize an array of floats that represent zero value for you. It will set all bytes in the specified range to the exact byte value attained by implicitly converting the supplied int to an unsigned char. I really question what benefit you expect to achieve from this. Relying on default initializations to null values is generally indicative of a problematic design. Luke Dec 26 '05 #5

 P: n/a di************@gmail.com wrote: Consider the dynamic allocation of a 2D array: int **temp; temp = new int*[height]; for (y=0; y

 P: n/a Luke Meyers wrote: What shall be done to convert it to int? Or float, if the 2D array is of floats? It's not converted to an int, it's converted to an unsigned char, which is the usual way to refer to an individual byte value. memset only operates on bytes, it will not initialize an array of floats that represent zero value for you. It will set all bytes in the specified range to the exact byte value attained by implicitly converting the supplied int to an unsigned char. I really question what benefit you expect to achieve from this. Relying on default initializations to null values is generally indicative of a problematic design. Is it? Jonathan Dec 26 '05 #7

 P: n/a Thank you Jonathan and others for the suggestions and the constructive criticism. regards, - D. Rathore -------------------------------- Jonathan Mcdougall wrote: If yes, what would be the syntax? Would it be: memset(temp, 0, width*height*sizeof(int)); That would work. Jonathan Dec 26 '05 #8

 P: n/a "di************@gmail.com" wrote in message news:11*********************@o13g2000cwo.googlegro ups.com... Consider the dynamic allocation of a 2D array: int **temp; temp = new int*[height]; for (y=0; y

 P: n/a Jim Langston wrote: Better is: ... = new int[width](); or for ( y = 0; y < height; y++ ) memset( temp[y], 0, width*sizeof(int) ); Way to go, Jim! That's what I was looking for :) Dec 26 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.