C++ :: Not Allocating Enough Memory To Pointer?

Jan 24, 2014

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" ****

[Code] .....

m_name is a private data member of type char* .

View 3 Replies


ADVERTISEMENT

C++ :: Integer Pointer - Get Address Without Allocating Memory

Jun 3, 2013

I have an integer pointer and i want its address without allocating memory,

main() {
int *a;
cout<<a;
}

This is giving 00000000 and its but obvious. Now if i use address of a (&a) along with *a,

main() {
int *a;
cout<<a;
cout<<&a;
}

'cout<<a' gives me a constant address but 'cout<<&a' gives me different address.

what is the reason behind & and why behaviour of 'cout<<a' changes when using with &.

View 8 Replies View Related

C :: Dynamically Allocating Memory?

Sep 18, 2013

I have created a database for music cds:

Code:

#include<stdio.h>
#include<stdlib.h>
#define array
typedef struct cd_database

[Code]....

When I am using malloc instead of arrays the code is not working properly after exit. I have tried alot but can't came up with a way

View 5 Replies View Related

C :: Error Allocating Memory

Jun 1, 2014

Code:

# 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?

View 7 Replies View Related

C :: Under-allocating Memory For A Tagged Union

Mar 6, 2015

What i'm interested in is the behavour of a struct/union constructed like this:

Code:
typedef struct {
uint64_t num1;
uint64_t num2;
} st_a;

typedef struct {
uint64_t num1;
uint32_t num2;

[Code] .....

What kind of behavour could I expect from object, in the following cases:

1. newsomestruct(0)->u.a.num1 = 2;
2. newsomestruct(1)->u.b.num1 = 2;
3. newsomestruct(0)->u.a.num2 = 2;
4. newsomestruct(1)->u.b.num2 = 2;
5. newsomestruct(0)->u.b.num1 = 2;
6. newsomestruct(1)->u.a.num1 = 2;
7. newsomestruct(0)->u.b.num2 = 2;
8. newsomestruct(1)->u.a.num2 = 2;
9. Code:

somestruct* ss1 = newsomestruct(0);
somestruct* ss2 = newsomestruct(1);
* ss1 = * ss2; 10. Code: somestruct* ss1 = newsomestruct(0);
somestruct* ss2 = newsomestruct(1);
* ss2 = * ss1;

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).

View 6 Replies View Related

C :: Allocating Memory To Array Of Struct?

Sep 9, 2013

Do you have to allocate memory(malloc) for an array of structs? Example:

Code:
typedef struct{
char * name;
}First;
struct name{
First fname;
};
struct name Names[10];

View 7 Replies View Related

C/C++ :: Allocating Memory For 2D Array In Other Class?

Jun 10, 2014

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:

Error: no operator "=" matches these operands

UserStuff.h:
struct userDataStruct {
bool* isAdmin;

[Code]...

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.

View 11 Replies View Related

C++ :: Allocating Memory For Vectorized 2D Array?

Mar 2, 2015

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.

Code:
vector<vector<CPoint2D<double>>> vv2d;
vv2d.resize(3);
vv2d[0].resize(3);

View 14 Replies View Related

C :: Allocating And Freeing Memory For Array Of Structures

Jul 27, 2014

I have to allocate memory for an array of structures, and my structure looks as following:

Code:
typedef struct {
char *name;
Uint *start_pos;
Uint len;
}
example_struct;

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:

Code: all_struct[0].name = malloc.....

But if yes, why the hell this works...

View 10 Replies View Related

C/C++ :: Dynamically Allocating Pointer Array

Apr 15, 2014

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';

[Code]...

View 3 Replies View Related

C++ :: Reading Memory In A Pointer

Aug 3, 2013

I am having a problem with a small exercise program. The program works the first time through the loop but not the second. It faults on this line:

cin >> *(pint + i1);, with this error: pint <Unable to read memory>.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int MAX(5);
int count(4);
int value(MAX);

[Code] ....

View 5 Replies View Related

C++ ::  How To Store Memory Address In A Pointer

Apr 29, 2013

What I'm trying to do is:

int *p;
someType memoryLocation;
cout<<"Enter your memory location: ";
cin >> memoryLocation;
p = memoryLocation;
cout << *p;

I was just messing around with some code, and was curious to if this was possible.

View 6 Replies View Related

C++ :: Pointer To Memory At Offset And Length

Aug 26, 2013

I'm working with memory mapped files and I have a block of memory that I've mapped to.

I want to write a function that returns a pointer to a portion of the mapped memory at an offset and length so I can write to it. I've never worked with memory at this level, is what I'm attempting possible?

I know that mapping functions can map to a part of the file at length and offset but I'm not sure if I should make multiple calls to map the memory from the file or just map the memory once and work with the portions I'm interested in using my proposed GetMemory function.

Code:

LPVOID m_lpData;
LPVOID GetMemory(DWORD pos, DWORD length) {
BYTE* buffer = (BYTE*)m_lpData;
buffer += pos;
// how to get a length of the memory?
return ((LPVOID)buffer);
}

View 14 Replies View Related

C :: Malloc Is Used To Allocate Free Memory To A Pointer

Nov 5, 2014

There is a part in the lesson that explains how malloc is used to allocate free memory to a pointer and gives us 2 examples:

Code:

float *ptr = malloc( sizeof(*ptr) ); and Code: float *ptr;
ptr = malloc( sizeof(*ptr) );

From my logic in the first case we allocate the memory to *ptr and in the second to ptr.

It's a bit confusing, am I missing something?

View 14 Replies View Related

C++ :: Does Realloc To Smaller Memory Result In Same Pointer

Aug 10, 2014

If I do this:
void* testPtr = malloc ( 1000 )

And then this:
testPtr = realloc ( testPtr, 500 )

Will the realloc just reduce the allocated size and keep the same pointer, or can there be a chance of it finding another place for that allocation ( Meaning that it will expensively move the memory to another location )?

I am trying to create efficient programs by making my dynamic allocations the least resource hungry as possible during runtime.

View 2 Replies View Related

C++ :: Pointer Incrementing Using Dynamic Memory Allocation?

Feb 13, 2013

I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:

unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);

where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:

double* Rin = new double[length];

and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.

View 1 Replies View Related

C++ :: Shallow Copy Of A COM Pointer (memory Leak)

Aug 25, 2012

Code:

HRESULT CAllocMeshHierarchy::CreateMeshContainer(PCTSTR Name,
const D3DXMESHDATA* pMeshData, const D3DXMATERIAL* pMaterials,
const D3DXEFFECTINSTANCE* pEffectInstances, DWORD NumMaterials,
const DWORD *pAdjacency, ID3DXSkinInfo* pSkinInfo,
D3DXMESHCONTAINER** ppNewMeshContainer

[Code] ....

When I make a shallow copy of pMeshData to OriginalMesh (for static mesh rendering, 2 objects pointing to one COM interface), memory leaks start to occur.When I don't, everything is fine.How to solve it? I addref() to it once (with pMeshData) and ReleaseCOM it in destroyMeshContainer

Remarks **** MeshData.pMesh is 0x00000000 while OriginalMesh is a pointer of some value whose values are 0xfeeefeee

View 1 Replies View Related

C++ :: Vector Of Int Class - How To Delete Pointer X In Destructor To Free Memory

Feb 25, 2015

An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.

#include<iostream>
using namespace std;
class vectorOfint{
int* x;
int size;
public:
vectorOfint();

[Code] .....

View 6 Replies View Related

C++ :: Virtual Function Table Pointer Misplaced In Object Memory

Jul 29, 2014

I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.

Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.

Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?

///////////////////////////////////////////////////////////////////////////
"MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info
188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114
189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128
190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128

[Code] .....

View 4 Replies View Related

C :: Keep Track Of Size Of Blocks Of Memory That A Pointer Points To - Malloc Is Stuck On 8

Jan 8, 2014

I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.

If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.

Why? I want it to output 1000. If I change that to 500, I want it to output 500.

int *a;
a = malloc(1000 * sizeof(int));

int j = sizeof(&a);
printf("%d", j);

I want to build my skills where I can allocate, inspect and change memory sizes.

View 4 Replies View Related

C :: Pointer Variable Indicating Memory Location - Volatile Type Qualifier

Jun 26, 2014

If I have a pointer variable indicating memory location in which we have stored what user entered and the pointer is of type volatile if the user gives the character 'a' twice , then this character will not be fetched twice from the memory but only when the character is changed???

This is the one meaning of the volatile? the other is that the value will be changed without the program itself change it?

View 4 Replies View Related

C++ :: Allocating Space Only For Two Characters

Jan 15, 2013

I am allocating space only for two characters but it fits all of them, if you run this it will print the whole string literal "hello my friend". How is that possible?

I am using gcc 4.6.3., I know about strncpy().

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char* str = new char[2];
strcpy(str, "hello my friend");
cout << str << endl;
return 0;
}

View 4 Replies View Related

C++ :: Declare Not Allocating Storage?

Jan 22, 2014

decalration won't allocate storage, while definition will. This is a test program:

#include <iostream>
using namespace std;
extern int ei;
int i;

[Code].....

Others are all fine in this program except ei.
compiler error: undefined reference to ei.

I understand ei is only declared so there is no memory address, but when I do ei=1, then ei completed it's definition, why still cannot use pei to get it's address?

View 9 Replies View Related

C++ :: Dynamically Allocating Multidimensional Array?

Nov 24, 2013

Working on this one from the Jumping into c++ book. The book asks that I create a multidimensional array at run time based on user input and fill it with a multiplication table

My code compiles fine but throws an uninitiated error for p when I try and run it.

Code:
void multiDimentionalMultiplication(int x, int y, int z){
int ***p;
**p = new int[x];
std::cout << "Allocating array.

[code]....

View 8 Replies View Related

C++ :: Dynamically Allocating One Dimension Of 3D Array?

Jun 14, 2013

I am trying to figure out the syntax to dynamically allocate a single dimension of a triple dimensional array. Basically i have a 2D array of structs. but each struct is an array (basically rows of the information). The rows of this structure need to be allocated dynamically, but the height and width of the overarching structure are static.

Basically: Struct * ts_FieldInfo[100][100] = new Struct[Class.returndataitems()];

View 2 Replies View Related

C++ :: Allocating Number Of Element On Array

Mar 7, 2013

#include <iostream>
using namespace std;
int main() {
int elm = 0;
int size = 0;
int *array;

[Code] ....

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved