C :: Simple Use Of Malloc And Realloc

Jan 27, 2013

i want to improve my knowledge about the dyn allocation of char pointers... with this code i wanted to type a string and insert the string in a array created dynamically:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char c;
char *test=NULL;
unsigned int len;
}

[code]....

why there are these 3 initial character '' ')' ':' that i didn't have typed...

View 4 Replies


ADVERTISEMENT

C :: Use Realloc To Extend The Memory?

Feb 24, 2013

I need to use realloc to extend the memory when it goes out of bounds but I am not getting a correct output.

this is how i am trying to realloc

Code:

if(strlen(theString) - strlen(searchFor) + strlen(replace) >= size -1) {
size += 80;
// theString = strcpy(theString,theString);

[Code].....

View 4 Replies View Related

C++ :: Dynamically Realloc 2D Array

Dec 18, 2014

I would like to realloc a 2D array. I have a counter, itime, it increases each step. Each step, I would like to reallocate my array, keeping the old values, and add new values to the array. When itime=1, I use only malloc, because it is allocated for the first time. When itime increases (e.q. itime=2), realloc comes into process. In the realloc process the GUI crashes.

int itime;
char ** solpointer;
itime = 1;
do {
if( itime == 1 ) {
solpointer = (char**)malloc(sizeof(char*) * itime);
solpointer[itime-1] = (char*)malloc(sizeof(char) * 32);

[code]....

View 3 Replies View Related

C :: How To Correctly Use Realloc On Array Of Char Arrays

Mar 21, 2013

how to correctly use realloc on an array of char arrays? Say I want to store strings in arrays (=array of char arrays) and double the size of max. strings (y value):

Code:

int x=200;
int y=10;
char *carray[y];
for (int j = 0; j < y; ++j)
carray [j] = malloc (sizeof(char)*x);}
}

[code]...

fix the realloc part of my code?

View 2 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++ :: 2-Dimensional Character Strings And Realloc Function

Jan 19, 2012

The following code fails/crashes in the second loop where it prints the content and frees the memory. But the issue could be with the first loop.

int main(int argc, char *argv[]){
char **a, buf[80];
int x;
a = (char **)realloc(NULL, sizeof(char *));
for(x = 0; x < 5; x++) {

[Code] .....

View 3 Replies View Related

C :: SegFault With Malloc

Nov 2, 2013

I wrote a program to detect if a graph is tree or not. Initially it was using static memory. Later I changed it to use memory dynamically using malloc().My problem is that, my program it works great for case when graph is not tree but fails if it is.

Code:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main()
}

[code]....

View 2 Replies View Related

C :: Adding Two Matrix Using Malloc

Mar 3, 2015

This is my code without malloc. I need to change the array size so there is no max size for each matrix. I must dynamically allocate space for all arrays used. So I need to use malloc to create my arrays. So I cant use int A[rows][cols].

Code:

/* This program asks the user for 2 matrices called A and B, as integers, and displays their sum, C. The max dimension of each matrix is 100. */
#include<stdio.h>
// Construct function
void construct()
{
int m, n, i, j;// Variables
int first[100][100], second[100][100], sum[100][100];// Matrices variables

[Code]...

Im having a hard time understanding/using malloc. Should first, second, and sum be single pointers or double? How do I scan the matrix correctly? And how do I add them properly? Do I have to change my for loops?

Code:

/* This program asks the user for 2 matrices called A and B, as integers, and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct()

[Code]...

View 3 Replies View Related

C :: Malloc Pointer To Arrays

Jan 6, 2014

I am confused whether 'malloc'ing an array of pointers and pointer to a group of arrays are the same.

Suppose I need an integer pointer to a group of arrays of 2 elements each.

int (*LR)[2];

So to dynamically allocate this pointer for M number of arrays suppose, then what is the correct syntax?

int M;
scanf("%d",&M);
int (*LR)[2];
LR = (int*)malloc(2*sizeof(int));

Whether this is correct??

View 14 Replies View Related

C :: Trying To Make A Stack Through Malloc

Oct 27, 2013

I am curious as to what is happening to my pointers and everything once I call malloc to create space for 5 integers to try to make a stack.

Code:

#include <stdio.h>
#include <stdlib.h>
void add(int * TOP, int * stack);
int main() {
int *stack = NULL;
int *TOP = NULL;
stack = (int *)malloc (5 * sizeof(int));

[Code] ....

I am guessing that when I initialize stack to malloc, stack now stores the starting address of where the space is taken out, and also assigns TOP that address too. I get the choice from the user (b) to get the instruction to try to push on the stack. I was told that the if statement in the function checks if the stack has passed the bounds of 5 elements. If not, it assigns the scanned variable and puts it into what TOP is pointing to and increments TOP to the next address. It is not working and am wanting to see where my logic is wrong.

View 4 Replies View Related

C :: Malloc Memory Allocation?

Oct 17, 2014

Code:
int *p, ar[100];
p = (int *)malloc(sizeof ar);

.. *p is a pointer variable, but what means another * here --> (int *)?

View 3 Replies View Related

C :: How Does Malloc Works On Array

Dec 15, 2013

i want to ask how does malloc works on an array for instance can i dao this

Code: p=100;
int array[10];
array = malloc(p * sizeof(int));

then will the size of int be 100 so will i be able to do like

Code:

array[11] = 12 ; ???

also if i have a 2d array how can i use malloc on it and how does this works with pointers????

View 2 Replies View Related

C++ :: Crash Normally But Not When Debugging (SDL Malloc)

Jul 16, 2013

Notes: Im Using SDL, Im using Codeblocks to code

Ive narrowed down my crashing problem ( using printf's ) to a malloc call I had to use printf's because when i ran the program in Codeblocks debugging mode, it did not crash and ran fine, but when i ran it normally, it would crash, giving me this error:

fatal signal segmentation fault (sdl parachute deployed)

Inside my code, I created a malloce function that checks malloc for me ( so i dont have to do it )

( general.h )

void* malloce ( size_t size )
{
printf ( "start malloce %i
", size );

[Code]....

funny thing is that i called this malloce function about 20 times before it hits this part of the code, it is unknown behavior to me!

View 10 Replies View Related

C/C++ :: Stuck On Using Pointers With Malloc

Apr 18, 2015

So this is my first attempt at actually writing code, I have a little basic core functionality set up and I'm 99% sure I'm doing something very fundamentally wrong with pointers.

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Creature {
int pow;
int tou;

[code] ....

Error generated:

magic.c: In function "main":
magic.c:71:26: warning: initialization from incompatible pointer type [enabled by default]
struct Creaturedb *db = Initialize_creaturedb;
^
magic.c:72:22: warning: initialization from incompatible pointer type [enabled by default]
struct Player *pl = Initialize_player;
^

View 3 Replies View Related

C++ :: Web Server Malloc Memory Error

Dec 1, 2014

I am writing a very basic database in C++ and I am accessing the data from a web browser. I am using the opensource Mongoose web server code....

I have an issue...

The way the DB works is this: on starting, the DB loads a json file of all of the data into it. I have a class called DatabaseLoader that does this - it is the class that gets rewritten depending on the data structure of the json.

This is passed to vectors (vector<Node*> and vector<Edge*>) as references from Graph object.

Once the DatabaseLoader has finished it can be destroyed and any memory allocated objects it created (except the ones in those two vectors).

From then on, the Graph object is in charge of all of the elements in the database that are stored in the two vectors. When the user browses to htpp://127.0.0.1:8000 they see the json representing each object in the vectors.

All good so far....

However, when I repeatedly hit refresh in my browser (and call me insane...) at quite a fast speed I get this error:

Code:
main(29855,0x7fff76763310) malloc: *** error for object 0x7f98b2829408: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
[1] 29855 abort ./main testing.json

It seems to me this would be if I tried to "delete" and object twice, or if one of my objects was overwriting memory somewhere. However I am not recreating anything, I am just looping over the vectors and printing out the content. When I refresh slowly, I dont see this happen - i did it quite a lot of times, but when I do it fast I think it is happening.

So is there any possibility of me hitting the c++ web server to quickly and it is trying to process the data twice, causing some sort of memory error - i.e do I need to implement threading or something??

I can paste code, but there is quite a lot now....

View 1 Replies View Related

C :: Qsort With Array Initialized Using Malloc?

Mar 2, 2013

I want to be honest, this is FOR homework, but is NOT homework. I have created this example to work from in order to understand qsort further because the next assignment requires it's use.

Our teacher gave us this small piece of example code and I am trying to expand on it to serve my purpose.
[C] Sorting - Pastebin.com

The code gives me no errors, but does not sort the array. Need to clarify the use of qsort in this instance.

I am imagining that the reason it's not sorting properly ( or at all ) is because of my comparison function. That is really just an assumption. Or perhaps I just don't understand the pointer array i'm using.

View 3 Replies View Related

C :: How To Get Address Returned By Malloc Function

Dec 6, 2013

So, I'm in the midst of implementing my own malloc() and free() functions, but I'm having a hard time with the syntax of getting the address that malloc returns. Whenever I check the address, it's 0 Here's the code:

Code:
char *word = malloc(10);
int address = *word;
printf("%d",address);

The reason I want the address is so that I could store it in a data structure for further usage when I'm dealing with different cases for the free() function. Or is there another way to do this?

View 5 Replies View Related

C :: Using Malloc For Array Of Character Pointers

Mar 30, 2013

I am able to work with n instances of a structure in one mallocated area, but when I try to do the same thing with just character pointers, I get compiler errors about making integer from pointer without a cast. If I create a structure with just a character pointer in it, it works just fine... I am just not seeing something here!!!

This works:

Code:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
struct items {
unsigned int item_1;
unsigned int item_2;

[Code]...

This DOES NOT work!

Code:

#include <stdio.h>
#include <stdlib.h>
int main (void) {
char * items_ptr = NULL;
unsignedint i = 0;
char * one = "one";
char * two = "two";

[Code]...

View 9 Replies View Related

C/C++ :: Can't Concentrate Two Strings Into One Using Strcat And Malloc

Mar 18, 2015

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *str1 = "Hello";

[code]....

it crashes after allocating memory. what's wrong here?

View 4 Replies View Related

C/C++ :: Converting 2D Array Code To Malloc

Mar 2, 2015

I made a program that adds two matrices and displays their sum with a max dimension of 100.

/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int first[100][100], second[100][100], sum[100][100]; // Matrices variables

[Code] ....

Now I need to change it so there is no max size for each matrix. The arrays will be larger than 100x100 so I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers.

/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */

#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int *first = NULL;

[Code] .....

View 8 Replies View Related

C/C++ :: Multiple Malloc Calls On One Variable?

Feb 13, 2013

I'd like to know what happens if I use multiple calls to malloc() on one pointer (without free) in a single function. Here is the example:

void *data_thread(void *sockfd_ptr) {
  int sockfd = *(int *) sockfd_ptr;
  const int BUFSIZE = 5;
  char recvmessage[BUFSIZE];
  char *headerstr = NULL;
  char *newheaderstr = NULL;

[code]....

what happens with newheaderstr every time malloc() is called. There isn't a realloc() or anything. I didn't think it looked right to keep using malloc() like that.

View 4 Replies View Related

C :: Setting Character In Malloc Char Array

Oct 16, 2013

I am attempting to change a character in a character array.In the code below, there are three attempts to do this. Only the first one will succeed. The last two both segfaults. If I understand correctly, str_one is declared in the heap, and could therefore be manipulated; and in contrast, str_two is declared in the stack and is therefore immutable, thus the segfault, when update it is attempted. However, I understand that using malloc, one is able to assign a pointer and allocate space in heap memory. Thus, I should be able to manipulate the assigned variable str_three. Doing so, however, results in a segfault.

Code:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char const* argv[])
{
char str_one[4092] = "This is string number one";
char * str_two = "This is string number two";

[Code]...

View 2 Replies View Related

C :: Malloc Memory Freed When Function Exits?

May 16, 2014

does memory reserved by malloc() get freed when the function it is called in finishes?

View 6 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 :: Get Text From Input And Save Each Word With Malloc

Dec 19, 2014

I want to write a program which takes a text from input and saves each word with malloc. For example for text "Have a nice a day" i want an array for each word, have,a,nice etc.

View 1 Replies View Related

C :: Malloc - Verifying Amount Of Memory Allocated

Sep 7, 2013

How can I view the number of bytes that have been allocated by using the malloc function?I tried:

mem = (float*)malloc(num*sizeof(float));
printf("The amount of memory allocated using malloc is %d.", mem);

Note: The variable "num" in my program is equal to 7.But every time I run the program, this value changes.

View 10 Replies View Related







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