StoneIce Posted August 25, 2015 Report Posted August 25, 2015 Morning every one Today I started playing around with memory allocation in C /c++. Now I wanted to do something like this, using a function as a character (passing a function to a character to perform assignment) hence I decided to try something like this. I don't see the response, do I appear to be doing this correctly #include <stdio.h>#include <stdlib.h>char* getname(){ char* fname[35] ="Shawn Little"; fname = (char*)malloc(35*sizeof(char));}int main(){ char namez[50]; namez = (char*) malloc(50*sizeof(char));ZeroMemory(namez,50*sizeof(char));namez = getname();return 0; }Need advise here as this is the first time doing this am typing from my BlackBerry. Quote
sparcky01 Posted August 25, 2015 Report Posted August 25, 2015 (edited) .. Edited August 4, 2021 by sparcky01 Quote
StoneIce Posted August 25, 2015 Author Report Posted August 25, 2015 you can use new char. Its more easy char*namez = new char[strlen(namez+1)]Then after that I just assign the namez, to use the getname() function? Quote
Nytro Posted August 25, 2015 Report Posted August 25, 2015 you can use new char. Its more easy char*namez = new char[strlen(namez+1)]No. You declare a pointer, an uninitialized pointer, and allocate space based on "strlen(uninitialized pointer)"? @StoneIce:char fname[35] ="Shawn Little"; NOT char* fname[35] ="Shawn Little";char namez[50]; namez = (char*) malloc(50*sizeof(char));It is either char namez[50] OR char *namez=(char *)malloc(...) but NOT both.Come on, C is not that complicated. Just RTFM. Quote
passfig Posted August 25, 2015 Report Posted August 25, 2015 If you play with memory allocation I recommend you to only use C. There are a lot of mistakes in that code but the most important if you want to learn about memory allocation is this - when you declare an array of chars like this:char arr[10]the memory is already allocated (10 bytes). You need to only declare a pointer and then manually allocate the memory with malloc() (or calloc()).#include <stdio.h>#include <stdlib.h>#include <string.h>char* getname(){ char* fname; fname = (char*)malloc(35 * sizeof(char)); strcpy(fname, "Shawn Little"); return fname;}int main(){ char *namez; namez = getname(); printf("Test: %s\n", namez); free(namez); return 0; }Get a C book and read the memory allocation and pointer chapters. Quote
S.L.C Posted August 25, 2015 Report Posted August 25, 2015 (edited) If you want a cleaner way of doing things and your compiler supports the majority of C++11 concepts. I suppose you could use the allocator that comes with it. Here's an example:#include <iostream>#include <memory>int main(int argc, char **argv){ // Instantiate some allocators std::allocator<char> allocator1; std::allocator<int> allocator2; // See how many you can afford std::cout << "Can allocate a max of " << allocator1.max_size() << " chars \n"; std::cout << "Can allocate a max of " << allocator2.max_size() << " ints \n"; // Allocate the desired number of specified types char * a = allocator1.allocate(35); int * b = allocator2.allocate(20); // Work with your memory // ... // Deallocate/Release it! allocator1.deallocate(a, 35); allocator2.deallocate(b, 20); return 0;}Safer (for you) because you'll get exceptions on failure and no more sizeof calculations or other stuff. You just say how many elements of the specified type you need memory for.Tested with MinGW GCC 4.9.3 but should work with 5.1.0 as well or 5.2.0 for that matter. Not sure about which VS version though Edited August 25, 2015 by S.L.C Quote