
December 25th, 2005, 11:25 AM
| | | new and memset
Consider the dynamic allocation of a 2D array:
int **temp;
temp = new int*[height];
for (y=0; y<height; y++) temp[y] = new int[width];
I have 2 Questions:
[a] Does new initializes memory to 0?
[b] I want to initialize this to some value not necessarily 0. Is it
ok to use 'memset' to this dynamically allocated array?
If yes, what would be the syntax? Would it be:
memset(temp, 0, width*height*sizeof(int));
thanks in advance,
D. Rathore
(remove underscores for email ID) | 
December 25th, 2005, 11:45 AM
| | | Re: new and memset
[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. | 
December 25th, 2005, 12:15 PM
| | | Re: new and memset
Luke Meyers wrote:[color=blue]
> [b] Sure. ...
> Remember that the second parameter is converted to an
> unsigned char.[/color]
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) | 
December 25th, 2005, 08:05 PM
| | | Re: new and memset divya_rathore_@gmail.com wrote:[color=blue]
> Consider the dynamic allocation of a 2D array:
>
> int **temp;
> temp = new int*[height];
> for (y=0; y<height; y++) temp[y] = new int[width];
>
> I have 2 Questions:
>
> [a] Does new initializes memory to 0?
>
> [b] I want to initialize this to some value not necessarily 0. Is it
> ok to use 'memset' to this dynamically allocated array?
>
> If yes, what would be the syntax? Would it be:
>
> memset(temp, 0, width*height*sizeof(int));
>
>
> thanks in advance,
> D. Rathore
> (remove underscores for email ID)[/color]
ever consider programming safely?
[color=blue]
> temp = new int*[height];
> for (y=0; y<height; y++) temp[y] = new int[width[/color]
does you environment incur prohibitive cost using vector<vector<int> >
? | 
December 26th, 2005, 05:05 AM
| | | Re: new and memset
> What shall be done to convert it to int? Or float, if the 2D array is[color=blue]
> of floats?[/color]
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 | 
December 26th, 2005, 05:25 AM
| | | Re: new and memset divya_rathore_@gmail.com wrote:[color=blue]
> Consider the dynamic allocation of a 2D array:
>
> int **temp;
> temp = new int*[height];
> for (y=0; y<height; y++) temp[y] = new int[width];
>
> I have 2 Questions:
>
> [a] Does new initializes memory to 0?[/color]
No, but
new int[width]();
does.
[color=blue]
> [b] I want to initialize this to some value not necessarily 0. Is it
> ok to use 'memset' to this dynamically allocated array?[/color]
No. memset() operates on bytes. If sizeof(int) is more than a byte,
you'll get weird results. Usually, memset() is used to zero-initialize
an array or a structure, not to assign an arbitrary value. What's more,
some types (such as floats or non-pods) cannot be memset'ed.
[color=blue]
> If yes, what would be the syntax? Would it be:
>
> memset(temp, 0, width*height*sizeof(int));[/color]
That would work.
If you want to put another value than 0, or if your types are not
integrals, do not use memset. Either use a loop or another container,
such as standard containers.
Jonathan | 
December 26th, 2005, 05:35 AM
| | | Re: new and memset
Luke Meyers wrote:[color=blue][color=green]
> > What shall be done to convert it to int? Or float, if the 2D array is
> > of floats?[/color]
>
> 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.[/color]
Is it?
Jonathan | 
December 26th, 2005, 08:15 AM
| | | Re: new and memset
Thank you Jonathan and others for the suggestions and the constructive
criticism.
regards,
- D. Rathore
--------------------------------
Jonathan Mcdougall wrote:
[color=blue][color=green]
> > If yes, what would be the syntax? Would it be:
> >
> > memset(temp, 0, width*height*sizeof(int));[/color]
>
> That would work.[/color]
[color=blue]
> Jonathan[/color] | 
December 26th, 2005, 09:25 AM
| | | Re: new and memset
"divya_rathore_@gmail.com" <divyarathore@gmail.com> wrote in message
news:1135508989.315514.72960@o13g2000cwo.googlegro ups.com...[color=blue]
> Consider the dynamic allocation of a 2D array:
>
> int **temp;
> temp = new int*[height];
> for (y=0; y<height; y++) temp[y] = new int[width];
>
> I have 2 Questions:
>
> [a] Does new initializes memory to 0?
>
> [b] I want to initialize this to some value not necessarily 0. Is it
> ok to use 'memset' to this dynamically allocated array?
>
> If yes, what would be the syntax? Would it be:
>
> memset(temp, 0, width*height*sizeof(int));[/color]
You are not guaranteed that the different blocks of memory allocated will be
contiguous.
temp[0] and temp[1] may not be width bytes apart. You'll be lucky if they
are, but there is no guarantee.
Some of the reasons they wouldn't be next to each other include, but are not
limited to:
1. Previously you allocated a memory, and some of it was freed that is big
enough for width bytes, so your first new grabs that memory, but there is
allocated memory in the way for the second new, so it grabs it at another
point in memory.
2. Your first block of memory grabed with new is toward the end of a memory
page. The next allocation won't fit in one page so the OS decides to grab
that memory at the start of a new page.
3. Any other reason your OS decides to not grab the memory right after each
other.
It may work for you 99 times out of 100. But that 100th time you're gonna
get weird errors and not be able to track them down. Don't treat your temp
array as a contiguous block of memory unless you actually allocate it that
way.
Better is:
.... = new int[width]();
or
for ( y = 0; y < height; y++ )
memset( temp[y], 0, width*sizeof(int) );
[color=blue]
>
>
> thanks in advance,
> D. Rathore
> (remove underscores for email ID)
>[/color] | 
December 26th, 2005, 09:45 AM
| | | Re: new and memset
Jim Langston wrote:[color=blue]
> Better is:
> ... = new int[width]();
>
> or
>
> for ( y = 0; y < height; y++ )
> memset( temp[y], 0, width*sizeof(int) );[/color]
Way to go, Jim! That's what I was looking for :) |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|