dynamic-allocation

How to dynamically allocate memory for char** in C

时光总嘲笑我的痴心妄想 提交于 2020-01-01 18:19:03
问题 How would I go about dynamically allocating memory to char** list in this function? Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length. I have to do other stuff with the C-strings but that stuff I should be fine with. Thanks! void readFileAndReplace(int argc, char** argv) { FILE *myFile; char** list; char c; int wordLine = 0, counter = 0, i; int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; myFile

How to dynamically allocate memory for char** in C

浪尽此生 提交于 2020-01-01 18:18:33
问题 How would I go about dynamically allocating memory to char** list in this function? Basically the idea of this program is I have to read in a list of words from a file. I cannot assume max strings or max string length. I have to do other stuff with the C-strings but that stuff I should be fine with. Thanks! void readFileAndReplace(int argc, char** argv) { FILE *myFile; char** list; char c; int wordLine = 0, counter = 0, i; int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0; myFile

What does `new auto` do?

六眼飞鱼酱① 提交于 2019-12-30 03:44:24
问题 What does it mean when I use new auto ? Consider the expression: new auto(5) What is the type of the dynamically allocated object? What is the type of the pointer it returns? 回答1: In this context, auto(5) resolves to int(5) . You are allocating a new int from the heap, initialized to 5 . (So, it's returning an int * ) Quoting Andy Prowl's resourceful answer, with permission: Per Paragraph 5.3.4/2 of the C++11 Standard: If the auto type-specifier appears in the type-specifier-seq of a new-type

What does `new auto` do?

不羁岁月 提交于 2019-12-30 03:43:47
问题 What does it mean when I use new auto ? Consider the expression: new auto(5) What is the type of the dynamically allocated object? What is the type of the pointer it returns? 回答1: In this context, auto(5) resolves to int(5) . You are allocating a new int from the heap, initialized to 5 . (So, it's returning an int * ) Quoting Andy Prowl's resourceful answer, with permission: Per Paragraph 5.3.4/2 of the C++11 Standard: If the auto type-specifier appears in the type-specifier-seq of a new-type

Why do Objective-C objects have to be dynamically allocated?

霸气de小男生 提交于 2019-12-30 01:53:04
问题 Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks. 回答1: the primary reason: not knowing how much stack size to reserve. existing conventions and uses also make lifting the restriction quite difficult. dynamic messaging does not matter in this case, as setting the right 'vtable' at initialization is trivial. in c++, the size of a stack object is always known (and if it's wrong, you know

Get the size (in bytes) of an object on the heap

落花浮王杯 提交于 2019-12-29 04:20:28
问题 I'm aware you can use MemoryLayout<T>.size to get the size of a type T . For example: MemoryLayout<Int32>.size // 4 However, for class instances (objects), MemoryLayout<T>.size returns the size of the reference to the object (8 bytes on 64 bit machines), not the size of the actual objects on the heap. class ClassA { // Objects should be at least 8 bytes let x: Int64 = 0 } class ClassB {// Objects should be at least 16 bytes let x: Int64 = 0 let y: Int64 = 0 } MemoryLayout<ClassA>.size // 8

How do I count words and lines that were input as a cstring array

半腔热情 提交于 2019-12-26 05:58:10
问题 This program should read a paragraph from a text file provided by the user and then store each line in a ragged char array and count the total number of words and lines of the paragraph then display the result. I can't figure out why does the number of lines keep giving me the result of 3 and why the number of words is always missing 2 words. please help and keep in mind that I'm no professional just started learning c++ recently. #include <iostream> #include <fstream> #include <cstring>

Passing pointers (matrix) to a function in c [duplicate]

天涯浪子 提交于 2019-12-25 18:50:44
问题 This question already has answers here : Passing an array as an argument to a function in C (9 answers) Closed 3 years ago . I have dynamically created a matrix using calloc in the usual way: int **matrix; int dim,r; scanf("%d",&dim); matrix=(int **)calloc(dim, sizeof(int *)); for(r=0; r<dim; r++) { matrix[r]=(int *)calloc(dim, sizeof(int)); } Now if I wanted to create a function to just print the elements of this matrix, I should write something like: void stampmatrix(int **matrix, int dim)

Dynamically allocate memory to a char-pointer [closed]

守給你的承諾、 提交于 2019-12-25 02:46:22
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Is this correct code or should I dynamically allocate memory to the member-variable of the String-class that receives the char-pointer? #include <iostream> using namespace std; class String { char *string; public: String(char *ch) { string = ch; } void print() { cout << string; } }; int main() {

How to manage lifetime of dynamically allocated QObject returned to QML?

回眸只為那壹抹淺笑 提交于 2019-12-23 19:24:56
问题 I have this code: QVariant componentFromCode(QString code) { QQmlComponent * component = new QQmlComponent(engine); engine->setObjectOwnership(component, QQmlEngine::JavaScriptOwnership); connect(component, &QQmlComponent::destroyed, this, &Factory::echo); component->setData(code.toUtf8(), QUrl()); return QVariant::fromValue(component); } But Factory::echo() is never called, which means the object gets leaked every time the function is called. This is what I have on the QML side: onClicked: {