Link to home
Create AccountLog in
Avatar of learningunix
learningunix

asked on

memset

Is this valid? or am I crossing the boundary of memory here causing memory corruption?
const char* names[256];
   std::memset(names, 0, 256);

for (int i = 0; i < 100; i++) {
   names[i] = "TESTING";
}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

>> const char* names[256];
>>    std::memset(names, 0, 256);

A char* is probably bigger than 1 byte, so this isn't going to write past the bounds of the array (it'll write way less than the total array in fact).

Related though : this is NOT the good way to initialize an array to all NULL pointers. Rather do :

        const char* names[256] = { 0 };
Avatar of learningunix
learningunix

ASKER

so if I do names[0] = "some string"  
if "some string" is greater than 256 in length, is that valid?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
That's why I asked initially if I am corrupting memory or not.

Do I need to malloc just before
names[] = "TESTING";
Or I can also do this
std::string names[256];

for (int i = 0; i < 100; i++) {
   names[i].assign("TESTING");
}

This way I don't have to worry about allocating and delallocating of memory at all

Open in new window

You can, or, assuming no "string" is going to be larger than say 10 chars do something like this (using part of your code, but not using array of pointers, just allocate the space on the stack):

char names[256][10];
   std::memset(&names, 0, sizeof(names));

for (int i = 0; i < 100; i++) {
   strcpy((char*)&names[ i ],"TESTING");
   }

You end with 256 "TESTING" strings here
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
>>  That's why I asked initially if I am corrupting memory or not.

As I said before : the code you showed is not corrupting memory.

>> Do I need to malloc just before

Unless you want to copy the string literal, you don't need to allocate memory.

A string literal (like "TESTING") has its own storage, you can just let a const char* point to it without issues :

        const char* str = "TESTING";       /* <--- no need to allocate memory */
I think so I am planning to go with string class.
This way.

1. I don't have to worry about freeing the memory in the end. Once the class is destroyed the static memory assoicated string is gone.
2. also I don't have to worry about the value of length assigned to each strings.

std::string names[256];
int position = 0;
void insert (const char *inName)
{
   names[position++].assign(inName);
   // I'll add a check to see position is > 256 or not
}

Open in new window

Just one thing I want to clarify.
if std::string names[256]; is member of class A.
When I do delete A, this will free up all names also ?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
>if std::string names[256]; is member of class A

The destructor will be invoked and each name will be destroyed.
PS: But try the vector because it allocates more space if needed.
Thanks!