I wrote the following C++ constructor, and I get an error - BUFFER too small on strcpy_s
Trace::Trace(const char *str) { if (str) { int len = strlen(str); this->m_name = new char[len+1]; // asking for 'len+1' memory elements of char strcpy_s(m_name, len, str); // **** I get here an error "BUFFER TOO SMALL" ****
# include <stdio.h> # include <math.h> # include <stdlib.h> # include <malloc.h> }
[code]...
I am compiling it on a 64 BIT ubuntu machine having 64GB ram using gcc 4.6 compiler. I am getting the following output Error allocating memory. But (914*866*2724) is approximately 8 GB, Whats wrong with the code?
This is what I'd expect, but I can't find any evidence online in C standards or elsewhere:
1. Works as expected, sets the value of a.num1 to 2. 2. Works as expected, sets the value of b.num1 to 2. 3. Works as expected, sets the value of a.num2 to 2. 4. Works as expected, sets the value of b.num2 to 2. 5. Works as expected, sets the value of b.num1 to 2. 6. Works as expected, sets the value of a.num1 to 2. 7. Works as expected, sets the value of b.num1 to 2. 8. Crashes/Memory Corruption, attempted to alter memory outside struct. 9. Works as expected, * ss1 == * ss2 10. Crashes/Memory Corruption, attempted to alter memory outside struct.
I've tested simular code on my machine (Xubuntu 14.04LTS compiled with gcc on -O3) and it appears to be reliable, given that you stick with acessing the type tagged in the struct or the common initial union struct members (in this case num1).
I'm making some multi-threaded program, but thats not my problem as i've done that already. I have a class with user-functions containing a structure which then contains a two dimensional array for each user with 25 elements. So I dont want to limit the user and make the array for example with just 10 rows, but allocate the needed memory to match the amount of 'users' a potential user of my program would want. The problem is, that i know how i should allocate it using 'new int' but it just doesnt work ! It gives an error:
Then, in some completely other class function inside the file mentioned above: (I know i could do a function in CUsers class which could allocate the memory, but I have this function which is used for some other things and it already has the amount of max users
void OtherClass::somefunction(maxusers) { // This gives an error: Error: no operator "=" matches these operands curUsers->uData.userNumbers = new int*[maxusers]; //maxusers is the int variable of max users specified by the client // However this doesn't for( int i = 0 ; i < maxusers ; i++ ) curUsers->uData.userNumbers[i] = new int[25]; // 25 columns, this doesnt give any error }
I'm not really sure what I'm doing wrong. Doing this in some function from CUsers class works (without curUsers-> or with, doesn't give any error) but doing it from some other class's function doesnt.
I want to be able to dynamically allocate and index an array like the following: vv2d[1][2].x and vv2d[1][2].y. In order to accomplish that I have chosen to use a std::vector of a std::vector of a 2D point class.
Code: /// Here is my templated version of a 2d point class which I have adopted from /// one by Alexander Chernosvitov, Function Graphics, 2001 (see ogview.h) /// http://www.codeguru.com/cpp/g-m/opengl/article.php/c5581/Function-graphics-in-3D.htm template <typename T>
[Code]....
Boundary violation occurs as soon as vv2d[1][0].x is encountered. I believe the problem is my inability to dynamically allocate the size of the (primary) typedef vector. However, eliminating the typedef for the following does not change the result. Further examination shows the vv2d[1][0] size and capacity to be 0.
And now I want to allocate memory, for a variable number (so an array) of example_struct, so I first do: Code:
example struct *all_struct; int total_num = 3; //will be set somehow, but for the example I set it on 3 all_struct = malloc (sizeof(example_struct) * total_num);
And now, as far as I now, I will have to allocate for each field of the structure memory, in order to be able to use it later. But I have problem at this point, a problem of understanding:
- I just allocated memory for 3 structures, but don't I have to allocate then memory for each structure separately, or can I just now allocate the fields like this:
so I have this code that dynamically allocates a pointer array increasing and removing elements of the array as its operated on.then it sorts and prints out the array when the user is finished operation on the array. i get this error message when running the program though.
"Unhandled exception at 0x0F474F98 (msvcr110d.dll) in Lab10_VarArray.exe: 0xC0000005: Access violation reading location 0xCCCCCCC0."
this is my code
#include <iostream> #include <cstdlib> #include "Header.h" using std::cout; using std::endl; using std::cin; int main(void) { char op='x';
Pointers point to an address in memory. What if I used 3 pointers: 2 to mark the first/last nodes, and the third to mark the current node being referenced? I would wrap it in a class (to make the memory management automatic, of course), but is this practical?? maybe some pseudo code will get the juices flowing:
template<class type> class supercondensed_list{ public: supercondensed_list(); ~supercondensed_list();
[code].....
Any things I should take into consideration? I'm not exactly the most experienced with pointers, and manually managing memory, but I think it's worth trying. If this works, then my programs should, in theory, be 100% memory efficient.
I've recently been reading tutorials on arrays and their aquaintance with memory addresses. So, I completely understand how an array's name, when defined, is a constant pointer to its first element's address.
My problem, however, lies with characters, and how they are basically arrays except with a null terminator for the last index. What I've come to undestand, is that, when defining a character variable, each 'character' has a memory address it is associated with.
The first attempt, as I assumed, should print the address of the first element. The second attempt, as I assumed, did not. I figured, &names[0] would print the address of the first element, which should have been the same as &names.
So, this brings me to my question, are characters formed of constant addresses, or are the address of individual characters not reachable?
The results of my code is supposed to be very simple: return the 2 integers and then their sum. However, it's doing returning the first value, then an address in memory(rather than the 2nd value), and then the 2nd value(rather than the sum). Here is the code:
I made a text file. I can do all File I/O functions in c. no problem! except that "I want to get the memory address of the beginning of that File", so that I can access each character of the file by incrementing memory address.
A special hardware unit with some storage in it is connected to your computer and is memory-mapped so that its storage is accessible in the address range 0x55500000 – 0x555fffff. You want to interface this hardware unit to your C++ program so that dynamic memory is allocated in this hardware unit, not in your computer’s memory. Implement a class MyHardwareMemAllocator which has the following function.
Suppose I have two classes, MyClassX and MyClassY, each with two member variables, as defined below. I create an object instance of each class, and then create a pointer to each member variable for each object:
Code: class MyClassX { public: int a; double b; MyClassX(int _a, double _b)
[code]....
After converting the hexadecimal to decimal, it appears that with MyClassX, pxb is 8 bytes from pxa, whereas for MyClassY, pya is only 4 bytes from pyb. This makes sense for MyClassY, because the first member variable to be stored is an int, and so will occupy 4 bytes. However, why should this be any different for MyClassX, which also has an int as the first member variable, so shouldn't this also occupy 4bytes?
The reason I have come across this problem is that I am looking into streaming objects to memory and then loading them again. (I know boost can do this, but I am trying it out myself from scratch.) Therefore, this is causing an issue, because I cannot just assume that the size of memory occupied by an object is just the sum of the sizes of its member variables. MyClassX is 8 bytes larger than MyClassY, even though the intuition is that it should only occupy 4 bytes more due to a double being replaced by an int.
When declaring char array[10], memory is allocated for 10 1-bit memory locations. Is extra memory allocated for storing the address of array[0]? In expressions, is array equivalent to a pointer constant or is it an identifier for a memory cell containing the address of array[0]? In other words, is array a variable or an alias for &array[0]?
Try to implement overloading << operator. If I done it void then everything work fine (see comment out) if I make it class of ostream& then the operator return to me some memory address.
Code: #ifndef Point_HPP // anti multiply including gates #define Point_HPP #include <sstream> class Point { private:// declaration of private data members double x;// X coordinate double y;// Y coordinate
I've got a problem with a piece of code that it doesn't seem to work anymore.
Code: #include <stdio.h> #include <conio.h> main () {
[Code] ..... i
I chose a to be 5 and it displays the following:
"Type a value for a: 5 5 in octal is: 5 5 in hexadecimal is: 5
Process returned 23 <0x17> execution time : 1.031 s".I first saw this when trying to display the address of a pointer. Am i missing something? I used to run this code on dev-c++ successfully but after a day or so of practice, it's not working anymore. I switched from dev-c++ to code blocks.
The StackElement class contains pointers to some dynamic arrays. When I use the assignment, StackElementArray[0] = iStackElement;, it doesn't copy the complete contents and I have to define an 'assignment operator' function to copy all the contents. I am wondering if there is a way I can assign StackElementArray[0] the pointer to the StackElement object. In that case, I will not need to copy the contents of iStackElement into StackElementArray[0] and will just copy the address.
#include <iostream> using namespace std; void myfunc(int* ); // what do i put in these parameters to accept a mem loc of a pointer int main () { int x = 5;
[Code] .....
SOLUTION:
#include <iostream> using namespace std; //Purpose to create a function that returns a pointer to a pointer int** myfunc(int**); int main () { int x = 5;