Solved

memset

Posted on 2011-02-11
14
284 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:learningunix
  • 6
  • 4
  • 4
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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 };
0
 

Author Comment

by:learningunix
Comment Utility
so if I do names[0] = "some string"  
if "some string" is greater than 256 in length, is that valid?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
Comment Utility
>> if "some string" is greater than 256 in length, is that valid?

Yes.

The 256 is the AMOUNT of strings that can be stored in the array. It says nothing about the SIZE of each individual string.
0
 
LVL 3

Assisted Solution

by:pasolo
pasolo earned 125 total points
Comment Utility
You declared an array of pointers to char, you have no memory allocated to anything else and memory will likely corrupt within the loop. This is not an array of strings, you have not declared any string.:
Try something like this:

vector <string> myvect;
myvect.reserve(256);
for (int i=0; i<256;i++)
myvect.push_back("TESTING");

You have an array of 256 strings now.
0
 

Author Comment

by:learningunix
Comment Utility
That's why I asked initially if I am corrupting memory or not.

Do I need to malloc just before
names[] = "TESTING";
0
 

Author Comment

by:learningunix
Comment Utility
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

0
 
LVL 3

Expert Comment

by:pasolo
Comment Utility
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Assisted Solution

by:pasolo
pasolo earned 125 total points
Comment Utility
And since you are using std, the vector approach i mentioned earlier is always preferable and cleaner
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>>  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 */
0
 

Author Comment

by:learningunix
Comment Utility
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

0
 

Author Comment

by:learningunix
Comment Utility
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 ?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 125 total points
Comment Utility
>> 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.

String literals don't need to be freed. They will be in memory for the duration of your application. You don't need to manage them. You can just point to them and use them.


>> 2. also I don't have to worry about the value of length assigned to each strings.

Don't know what you mean by that. But the length of a string literal can be optained with strlen, just like any other string.


>> if std::string names[256]; is member of class A.
>> When I do delete A, this will free up all names also ?

Yes.


Note that you could use std::string instead if you wish, but it would no longer behave in the same way as your original code. You'd be working with a COPY of the string, rather than with a string literal. Then again, maybe that's what you wanted all along :)
0
 
LVL 3

Expert Comment

by:pasolo
Comment Utility
>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.
0
 

Author Closing Comment

by:learningunix
Comment Utility
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now