Jump to content
StoneIce

Learning Malloc c/c++ need help.

Recommended Posts

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.

Link to comment
Share on other sites

you can use new char. Its more easy :D

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Edited by S.L.C
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...