0

I'm trying to create a dynamically allocated array of type unsigned char* in C++. However, when I this I get back a string instead of a bracket enclosed ({}) array which is what I want.

unsigned char* arr = new unsigned char[arrLen];

Code Picture

Picture showing the difference between the two

You see how the latter doesn't just go to nothing after the first character? That's what I want.

How might I go about remedying this?

Thank you for your time.

10
  • 1
    What is the problem ? Commented Apr 22, 2018 at 5:36
  • Also wondering what the problem might be, can you please clarify ? Commented Apr 22, 2018 at 5:47
  • I don't want it to be a string. The function I'm passing this into needs a bracket enclosed list. For context, the contents of arr will be an executable in hex format. Executables contain a lot of null bytes to which adds to the weirdness if interpreted as a string because they strings interpret those as the end of the string. Commented Apr 22, 2018 at 5:47
  • Why do you think it is a string? Commented Apr 22, 2018 at 5:48
  • And what do you think "a bracket enclosed ({}) array` is? Commented Apr 22, 2018 at 5:49

3 Answers 3

2

First, de debugger assumes by default that char represents an ascii character rather than a number. It will display char as such.

arr2 has type const char[3] so the debugger knows there are 3 elements to display.

arr has type const char*. The debugger can't know if it's only one elements or an array with a certain number of elements.

If you are using visual studio for instance, you can hint the debugger to display three char by adding a “variable watch” with the syntax arr,3 in the watch menu.

Sign up to request clarification or add additional context in comments.

Comments

2

I'm not sure if this is what you are looking for, but have you tried using a std::vector? It can handle the dynamic assignment you are looking for at least, and shouldn't treat a NULL character as the end of a string.

 #include <vector>

 std::vector<char> arr = { 0x5A, 0x00, 0x2B };

1 Comment

I actually tried using std::vector when I began creating my program but it acted funny with such huge exe arrrays. Like it would never compile. I left it overnight once and it never did. So I changed it to char arrays then my during compilation it exited "CL.exe" exited with code 2 so I tried making the char arrays static and then it worked. In the question, I didn't bother making them static but you get the point. Thanks anyways though, this would work great for arrays that aren't 10 MBs in size surely.
-1

If you want a list of chars(array) that grows dynamically, what you need is a list of pointers where the list of each segment is a large number-say 1000. A vector container class sacrifices memory usage for the ability to grow.

vector container class allows for dynamic growth but uses a lot of memory

Also, dynamic growth one data element at a time is not recommended for a large list of data. If you want dynamic growth for a large list, create a list in chunks such as the following. Use a large list segment- of say 1000 units. I created 1000 lists in the following example. I do this by creating an array of 1000 pointers. This will create the 1 million chars you are looking for and can grow dynamically. The following example shows how you would do this.

.

void main() {
    unsigned char* listsegment[1000];
int chrn=0;

    int x, y = 0;
    for (int x = 0; x < 1000; x++) {
        listsegment[x] = new unsigned char[1000];
        for (y = 0; y < 1000; y++) {
            *(listsegment[x] + y) = chrn;
if (chrn >=255) chrn=0;
else chrn++;
        }
    }

}

Completing the program- What if more than 1000 segments need to be dynamically allocated?

Then create a list of Segment Sets. It can either be in a linked list or a in a container class.

Since the single set creates a 1000 segments of 1000 characters, a collection of these sets needs probably not be larger than 1000. A thousands sets would equal (1000*1000)*1000 which would equal one billion. Therefore, the collection would only need to be 1000 or less, which can be quickly iterated through-which makes random access for the collection not necessary.

Here is the program redone to support an infinite amount of sets through an infinitely large collection of sets. This also is a good example of segmented dynamic memory allocation in general.

#include <iostream>
#include<queue>

using namespace std;

struct listSegmentSetType {
    unsigned char* listSegment[1000];
    int count=0;
};


void main() {

    listSegmentSetType listSegmentSet;
    queue<listSegmentSetType> listSegmentSetCollection;
    int numberOfListSegmentSets = 0;

    int chrn = 0;

    int x, y = 0;
    listSegmentSet.count = 0;
    for (int x = 0; x < 1000; x++) {

        listSegmentSet.listSegment[x] = new unsigned char[1000];
        for (y = 0; y < 1000; y++) {
            *(listSegmentSet.listSegment[x] + y) = chrn;
            if (chrn >= 255) chrn = 0;
            else chrn++;
        }
        listSegmentSet.count++;
    }

    // add just completely filled out first list segment set to que
    listSegmentSetCollection.push(listSegmentSet);
    numberOfListSegmentSets++;

    // now fill in second set of list segments-

    listSegmentSet.count = 0;

    for (int x = 0; x < 1000; x++) {

            listSegmentSet.listSegment[x] = new unsigned char[1000];
        for (y = 0; y < 1000; y++) {
            *(listSegmentSet.listSegment[x] + y) = chrn;
            if (chrn >= 255) chrn = 0;
            else chrn++;
        }
        listSegmentSet.count++;
    }
    listSegmentSetCollection.push(listSegmentSet);
    numberOfListSegmentSets++;

    // now fill out any more sets of list segments and add to collection 
    // only when count listSegmentSet.count is no
    // longer less than 1000.  
}

2 Comments

-1 This answer does not address the question in any meaningful way. Take a look at std::deque for a similar blocks of data library container.
Yep. I know there is a container class that is a list class. Was also going to mention to do a doubly linked list. So it answers the question in plenty of a meaningful way-but just didn't include a doubly linked list because I thought that would take too much of my time and over answering the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.