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) 9 12325
[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.
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) di************@gmail.com wrote: 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)
ever consider programming safely?
temp = new int*[height]; for (y=0; y<height; y++) temp[y] = new int[width
does you environment incur prohibitive cost using vector<vector<int> >
?
> 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 di************@gmail.com wrote: 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?
No, but
new int[width]();
does.
[b] I want to initialize this to some value not necessarily 0. Is it ok to use 'memset' to this dynamically allocated array?
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.
If yes, what would be the syntax? Would it be:
memset(temp, 0, width*height*sizeof(int));
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
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
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
"di************@gmail.com" <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<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));
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) );
thanks in advance, D. Rathore (remove underscores for email ID)
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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mantorok Redgormor |
last post by:
When is it that memset caused problems?
I recall from posts in the past where someone
used memset in their code that invoked undefined
behavior.
What about the following?
char the_array;
...
|
by: bob_jenkins |
last post by:
{
const void *p;
(void)memset((void *)p, ' ', (size_t)10);
}
Should this call to memset() be legal? Memset is of type
void *memset(void *, unsigned char, size_t)
Also, (void *) is the...
|
by: 69dbb24b2db3daad932c457cccfd6 |
last post by:
Hello,
I have to initialize all elements of a very big float point array to
zero. It seems memset(a, 0, len) is faster than a simple loop. I just
want to know whether it is safe to do so, since I...
|
by: jacob navia |
last post by:
Many compilers check printf for errors, lcc-win32 too. But there are
other functions that would be worth to check, specially memset.
Memset is used mainly to clear a memory zone, receiving a...
|
by: Patrick Kowalzick |
last post by:
Dear all,
I have an existing piece of code with a struct with some PODs.
struct A
{
int x;
int y;
};
|
by: volunteers |
last post by:
I met a question about memset and have no idea right now. Could anybody
give a clue? Thanks
memset is sometimes used to initialize data in a constructor like the
example below. What is the...
|
by: AndersWang |
last post by:
Hi,
dose anybody here explain to me why memset would be faster than a
simple loop. I doubt about it!
In an int array scenario:
int array;
for(int i=0;i<10;i++) //ten loops
|
by: dykeinthebox |
last post by:
Consider the following program:
#include <stdlib.h>
#include <string.h>
int main( void )
{
void *p = malloc( 4 );
if ( p )
{
|
by: Gaijinco |
last post by:
I'm having a headache using memset()
Given:
int v;
memset((void*)v, 1, sizeof(v));
Can I be 100% positive than v = 1 for i 0, or there is something
else I have to do?.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |